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/uses_from_macos: audit when depends_on :linux #16382

Merged
merged 1 commit into from Dec 31, 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
5 changes: 5 additions & 0 deletions Library/Homebrew/rubocops/uses_from_macos.rb
Expand Up @@ -98,7 +98,12 @@ class UsesFromMacos < FormulaCop
def audit_formula(_node, _class_node, _parent_class_node, body_node)
return if body_node.nil?

depends_on_linux = depends_on?(:linux)

find_method_with_args(body_node, :uses_from_macos, /^"(.+)"/).each do |method|
@offensive_node = method
problem "`uses_from_macos` should not be used when Linux is required." if depends_on_linux

dep = if parameters(method).first.instance_of?(RuboCop::AST::StrNode)
parameters(method).first
elsif parameters(method).first.instance_of?(RuboCop::AST::HashNode)
Expand Down
51 changes: 41 additions & 10 deletions Library/Homebrew/test/rubocops/uses_from_macos_spec.rb
Expand Up @@ -5,16 +5,47 @@
describe RuboCop::Cop::FormulaAudit::UsesFromMacos do
subject(:cop) { described_class.new }

it "when auditing uses_from_macos dependencies" do
expect_offense(<<~RUBY)
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"

uses_from_macos "postgresql"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/UsesFromMacos: `uses_from_macos` should only be used for macOS dependencies, not postgresql.
end
RUBY
context "when auditing `uses_from_macos` dependencies" do
it "reports an offense when used on non-macOS dependency" do
expect_offense(<<~RUBY)
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"

uses_from_macos "postgresql"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/UsesFromMacos: `uses_from_macos` should only be used for macOS dependencies, not postgresql.
end
RUBY
end

it "reports offenses for multiple non-macOS dependencies and none for valid macOS dependencies" do
expect_offense(<<~RUBY)
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"

uses_from_macos "boost"
^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/UsesFromMacos: `uses_from_macos` should only be used for macOS dependencies, not boost.
uses_from_macos "bzip2"
uses_from_macos "postgresql"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/UsesFromMacos: `uses_from_macos` should only be used for macOS dependencies, not postgresql.
uses_from_macos "zlib"
end
RUBY
end

it "reports an offense when used in `depends_on :linux` formula" do
expect_offense(<<~RUBY)
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"

depends_on :linux
uses_from_macos "zlib"
^^^^^^^^^^^^^^^^^^^^^^ FormulaAudit/UsesFromMacos: `uses_from_macos` should not be used when Linux is required.
end
RUBY
end
end

include_examples "formulae exist", described_class::ALLOWED_USES_FROM_MACOS_DEPS
Expand Down