diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 2059ebbeba..d1601f924d 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -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)) diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb index 5f76cd6daf..37b3216178 100644 --- a/Library/Homebrew/service.rb +++ b/Library/Homebrew/service.rb @@ -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 = [] diff --git a/Library/Homebrew/test/dev-cmd/audit_spec.rb b/Library/Homebrew/test/dev-cmd/audit_spec.rb index 209dfddaf4..40bdfa2026 100644 --- a/Library/Homebrew/test/dev-cmd/audit_spec.rb +++ b/Library/Homebrew/test/dev-cmd/audit_spec.rb @@ -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" diff --git a/Library/Homebrew/test/service_spec.rb b/Library/Homebrew/test/service_spec.rb index 881128e20d..39c0e7031f 100644 --- a/Library/Homebrew/test/service_spec.rb +++ b/Library/Homebrew/test/service_spec.rb @@ -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