Merge pull request #7280 from issyl0/audit-uses-from-macos

audit: Check if `uses_from_macos` usage is correct
This commit is contained in:
Mike McQuaid 2020-04-07 12:47:30 +01:00 committed by GitHub
commit bd082011c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 0 deletions

View File

@ -18,5 +18,6 @@ require "rubocops/options"
require "rubocops/urls"
require "rubocops/lines"
require "rubocops/class"
require "rubocops/uses_from_macos"
require "rubocops/rubocop-cask"

View File

@ -0,0 +1,60 @@
# frozen_string_literal: true
require "rubocops/extend/formula"
module RuboCop
module Cop
module FormulaAudit
# This cop audits `uses_from_macos` dependencies in formulae
class UsesFromMacos < FormulaCop
ALLOWED_USES_FROM_MACOS_DEPS = %w[
bison
bzip2
curl
expat
expect
flex
groff
icu4c
krb5
libedit
libffi
libpcap
libxml2
libxslt
llvm
ncurses
m4
openldap
openssl
perl
php
ruby
sqlite
tcl-tk
texinfo
unzip
vim
xz
zlib
zip
zsh
].freeze
def audit_formula(_node, _class_node, _parent_class_node, body_node)
find_method_with_args(body_node, :uses_from_macos, /^"(.+)"/).each do |method|
dep = if parameters(method).first.class == RuboCop::AST::StrNode
parameters(method).first
elsif parameters(method).first.class == RuboCop::AST::HashNode
parameters(method).first.keys.first
end
next if ALLOWED_USES_FROM_MACOS_DEPS.include?(string_content(dep))
problem "`uses_from_macos` should only be used for macOS dependencies, not #{string_content(dep)}."
end
end
end
end
end
end

View File

@ -49,6 +49,7 @@ RSpec/FilePath:
- 'rubocops/options_spec.rb'
- 'rubocops/patches_spec.rb'
- 'rubocops/text_spec.rb'
- 'rubocops/uses_from_macos_spec.rb'
- 'search_spec.rb'
- 'string_spec.rb'
- 'system_command_result_spec.rb'

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
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"
uses_from_macos "postgresql"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `uses_from_macos` should only be used for macOS dependencies, not postgresql.
end
RUBY
end
end