Skip to content

Commit

Permalink
Merge pull request #11129 from SMillerDev/feature/audit/service_command
Browse files Browse the repository at this point in the history
  • Loading branch information
SMillerDev authored Apr 27, 2021
2 parents 1157a97 + 2ec4125 commit afa99b4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
10 changes: 10 additions & 0 deletions Library/Homebrew/formula_cellar_checks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,22 @@ def check_python_symlinks(name, keg_only)
"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))
Expand Down
17 changes: 9 additions & 8 deletions Library/Homebrew/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,23 @@ def std_service_path_env
"#{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
Expand All @@ -148,16 +152,13 @@ def to_plist
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 = []
Expand Down
61 changes: 61 additions & 0 deletions Library/Homebrew/test/dev-cmd/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,67 @@ class Foo < Formula
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"
Expand Down
12 changes: 12 additions & 0 deletions Library/Homebrew/test/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,16 @@
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

0 comments on commit afa99b4

Please sign in to comment.