Merge pull request #16382 from cho-m/uses_from_macos-linux-required
rubocops/uses_from_macos: audit when `depends_on :linux`
This commit is contained in:
		
						commit
						bc566b5cd0
					
				@ -98,7 +98,12 @@ module RuboCop
 | 
			
		||||
        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)
 | 
			
		||||
 | 
			
		||||
@ -5,16 +5,47 @@ require "rubocops/uses_from_macos"
 | 
			
		||||
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"
 | 
			
		||||
  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
 | 
			
		||||
          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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user