Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rubocops/service: check for more cellar paths #15337

Merged
merged 1 commit into from
Apr 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 18 additions & 6 deletions Library/Homebrew/rubocops/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,28 @@ module FormulaAudit
class Service < FormulaCop
extend AutoCorrector

CELLAR_PATH_AUDIT_CORRECTIONS = {
bin: :opt_bin,
libexec: :opt_libexec,
pkgshare: :opt_pkgshare,
prefix: :opt_prefix,
sbin: :opt_sbin,
share: :opt_share,
}.freeze

def audit_formula(_node, _class_node, _parent_class_node, body_node)
service_node = find_block(body_node, :service)
return if service_node.blank?

# This check ensures that `bin` is not referenced because
# `opt_bin` is more portable and works with the API.
find_every_method_call_by_name(service_node, :bin).each do |bin_node|
offending_node(bin_node)
problem "Use `opt_bin` instead of `bin` in service blocks." do |corrector|
corrector.replace(bin_node.source_range, "opt_bin")
# This check ensures that cellar paths like `bin` are not referenced
# because their `opt_` variants are more portable and work with the
# API.
CELLAR_PATH_AUDIT_CORRECTIONS.each do |path, opt_path|
find_every_method_call_by_name(service_node, path).each do |node|
offending_node(node)
problem "Use `#{opt_path}` instead of `#{path}` in service blocks." do |corrector|
corrector.replace(node.source_range, opt_path)
end
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions Library/Homebrew/test/rubocops/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
describe RuboCop::Cop::FormulaAudit::Service do
subject(:cop) { described_class.new }

it "reports an offense when a formula's service block uses `bin`" do
it "reports offenses when a formula's service block uses cellar paths" do
expect_offense(<<~RUBY)
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"

service do
run [bin/"foo", "run", "-config", etc/"foo/config.json"]
^^^ FormulaAudit/Service: Use `opt_bin` instead of `bin` in service blocks.
working_dir libexec
^^^^^^^ FormulaAudit/Service: Use `opt_libexec` instead of `libexec` in service blocks.
end
end
RUBY
Expand All @@ -23,18 +25,20 @@ class Foo < Formula

service do
run [opt_bin/"foo", "run", "-config", etc/"foo/config.json"]
working_dir opt_libexec
end
end
RUBY
end

it "reports no offenses when a formula's service block uses `opt_bin`" do
it "reports no offenses when a formula's service block only uses opt paths" do
expect_no_offenses(<<~RUBY)
class Bin < Formula
url "https://brew.sh/foo-1.0.tgz"

service do
run [opt_bin/"bin", "run", "-config", etc/"bin/config.json"]
working_dir opt_libexec
end
end
RUBY
Expand Down