Audit: make sure service commands exist

This commit is contained in:
Sean Molenaar 2021-04-13 11:25:56 +02:00
parent 1157a97d8b
commit 2ec4125178
No known key found for this signature in database
GPG Key ID: 6BF5D8DF0D34FAAE
4 changed files with 92 additions and 8 deletions

View File

@ -275,12 +275,22 @@ module FormulaCellarChecks
"Python formulae that are keg-only should not create `pip3` and `wheel3` symlinks."
end
def check_service_command(formula)
return unless formula.prefix.directory?
return unless formula.service?
return "Service command blank" if formula.service.command.blank?
"Service command does not exist" unless File.exist?(formula.service.command.first)
end
def audit_installed
@new_formula ||= false
problem_if_output(check_manpages)
problem_if_output(check_infopages)
problem_if_output(check_jars)
problem_if_output(check_service_command(formula))
problem_if_output(check_non_libraries) if @new_formula
problem_if_output(check_non_executables(formula.bin))
problem_if_output(check_generic_executables(formula.bin))

View File

@ -118,19 +118,23 @@ module Homebrew
"#{HOMEBREW_PREFIX}/bin:#{HOMEBREW_PREFIX}/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
end
sig { returns(T::Array[String]) }
def command
instance_eval(&@service_block)
@run.select { |i| i.is_a?(Pathname) }
.map(&:to_s)
end
# Returns a `String` plist.
# @return [String]
sig { returns(String) }
def to_plist
instance_eval(&@service_block)
clean_command = @run.select { |i| i.is_a?(Pathname) }
.map(&:to_s)
base = {
Label: @formula.plist_name,
RunAtLoad: @run_type == RUN_TYPE_IMMEDIATE,
ProgramArguments: clean_command,
ProgramArguments: command.join,
}
base[:KeepAlive] = @keep_alive if @keep_alive == true
@ -148,16 +152,13 @@ module Homebrew
def to_systemd_unit
instance_eval(&@service_block)
clean_command = @run.select { |i| i.is_a?(Pathname) }
.map(&:to_s)
unit = <<~EOS
[Unit]
Description=Homebrew generated unit for #{@formula.name}
[Service]
Type=simple
ExecStart=#{clean_command.join}
ExecStart=#{command.join}
EOS
options = []

View File

@ -488,6 +488,67 @@ module Homebrew
end
end
describe "#check_service_command" do
specify "Not installed" do
fa = formula_auditor "foo", <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"
service do
run []
end
end
RUBY
expect(fa.check_service_command(fa.formula)).to match nil
end
specify "No service" do
fa = formula_auditor "foo", <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"
end
RUBY
mkdir_p fa.formula.prefix
expect(fa.check_service_command(fa.formula)).to match nil
end
specify "No command" do
fa = formula_auditor "foo", <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"
service do
run []
end
end
RUBY
mkdir_p fa.formula.prefix
expect(fa.check_service_command(fa.formula)).to match "Service command blank"
end
specify "Invalid command" do
fa = formula_auditor "foo", <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"
service do
run [HOMEBREW_PREFIX/"bin/something"]
end
end
RUBY
mkdir_p fa.formula.prefix
expect(fa.check_service_command(fa.formula)).to match "Service command does not exist"
end
end
describe "#audit_github_repository" do
specify "#audit_github_repository when HOMEBREW_NO_GITHUB_API is set" do
ENV["HOMEBREW_NO_GITHUB_API"] = "1"

View File

@ -123,4 +123,16 @@ describe Homebrew::Service do
expect(unit).not_to include("Environment=\"PATH=#{std_path}\"")
end
end
describe "#command" do
it "returns @run data" do
f.class.service do
run opt_bin/"beanstalkd"
run_type :immediate
end
command = f.service.command
expect(command).to eq(["#{HOMEBREW_PREFIX}/opt/#{name}/bin/beanstalkd"])
end
end
end