From 70451ea7b14f1305bc149601e8ed74f7467c1753 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Tue, 4 Apr 2023 19:55:15 -0700 Subject: [PATCH] Add cop to stop bin in service block The preferred method is opt_bin because that works with the API and is more portable (works between versions). Also removed the last example from the docs of `bin/"name"` from the service block section --- Library/Homebrew/rubocops/all.rb | 1 + Library/Homebrew/rubocops/service.rb | 31 +++++++++++++ .../Homebrew/test/rubocops/service_spec.rb | 43 +++++++++++++++++++ docs/Formula-Cookbook.md | 2 +- 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 Library/Homebrew/rubocops/service.rb create mode 100644 Library/Homebrew/test/rubocops/service_spec.rb diff --git a/Library/Homebrew/rubocops/all.rb b/Library/Homebrew/rubocops/all.rb index ec0fe92957..5986732a9b 100644 --- a/Library/Homebrew/rubocops/all.rb +++ b/Library/Homebrew/rubocops/all.rb @@ -24,6 +24,7 @@ require_relative "lines" require_relative "livecheck" require_relative "options" require_relative "patches" +require_relative "service" require_relative "text" require_relative "urls" require_relative "uses_from_macos" diff --git a/Library/Homebrew/rubocops/service.rb b/Library/Homebrew/rubocops/service.rb new file mode 100644 index 0000000000..165af30f26 --- /dev/null +++ b/Library/Homebrew/rubocops/service.rb @@ -0,0 +1,31 @@ +# typed: true +# frozen_string_literal: true + +require "rubocops/extend/formula_cop" + +module RuboCop + module Cop + module FormulaAudit + # This cop audits the service block. + # + # @api private + class Service < FormulaCop + extend AutoCorrector + + 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") + end + end + end + end + end + end +end diff --git a/Library/Homebrew/test/rubocops/service_spec.rb b/Library/Homebrew/test/rubocops/service_spec.rb new file mode 100644 index 0000000000..86a4ed47ed --- /dev/null +++ b/Library/Homebrew/test/rubocops/service_spec.rb @@ -0,0 +1,43 @@ +# typed: false +# frozen_string_literal: true + +require "rubocops/service" + +describe RuboCop::Cop::FormulaAudit::Service do + subject(:cop) { described_class.new } + + it "reports an offense when a formula's service block uses `bin`" 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"] + ^^^ Use `opt_bin` instead of `bin` in service blocks. + end + end + RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url "https://brew.sh/foo-1.0.tgz" + + service do + run [opt_bin/"foo", "run", "-config", etc/"foo/config.json"] + end + end + RUBY + end + + it "reports no offenses when a formula's service block uses `opt_bin`" 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"] + end + end + RUBY + end +end diff --git a/docs/Formula-Cookbook.md b/docs/Formula-Cookbook.md index 1c3aad60ce..e4e3743bb1 100644 --- a/docs/Formula-Cookbook.md +++ b/docs/Formula-Cookbook.md @@ -882,7 +882,7 @@ There are two ways to add `launchd` plists and `systemd` services to a formula, ```ruby service do - run bin/"script" + run opt_bin/"script" end ```