From 44bfbd0112e690a5c94528085543b92fd720f706 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Fri, 27 Nov 2020 13:13:09 -0500 Subject: [PATCH] Migrate uses_from_macos style lists --- Library/Homebrew/rubocops/extend/formula.rb | 7 +- Library/Homebrew/rubocops/uses_from_macos.rb | 89 +++---------------- .../test/rubocops/uses_from_macos_spec.rb | 2 - 3 files changed, 18 insertions(+), 80 deletions(-) diff --git a/Library/Homebrew/rubocops/extend/formula.rb b/Library/Homebrew/rubocops/extend/formula.rb index d3e75ea34f..e985631beb 100644 --- a/Library/Homebrew/rubocops/extend/formula.rb +++ b/Library/Homebrew/rubocops/extend/formula.rb @@ -473,8 +473,9 @@ module RuboCop match_obj[1] end - # Returns whether the current formula exists in the given style exception list - def tap_style_exception?(list) + # Returns whether the given formula exists in the given style exception list. + # Defaults to the current formula being checked. + def tap_style_exception?(list, formula = nil) if @tap_style_exceptions.blank? && formula_tap.present? @tap_style_exceptions = {} @@ -495,7 +496,7 @@ module RuboCop return false if @tap_style_exceptions.blank? return false unless @tap_style_exceptions.key? list - @tap_style_exceptions[list].include? @formula_name + @tap_style_exceptions[list].include?(formula || @formula_name) end private diff --git a/Library/Homebrew/rubocops/uses_from_macos.rb b/Library/Homebrew/rubocops/uses_from_macos.rb index 77804e677b..539de517fb 100644 --- a/Library/Homebrew/rubocops/uses_from_macos.rb +++ b/Library/Homebrew/rubocops/uses_from_macos.rb @@ -6,82 +6,20 @@ require "rubocops/extend/formula" module RuboCop module Cop module FormulaAudit + # This cop audits formulae that are keg-only because they are provided by macos. + class ProvidedByMacos < FormulaCop + def audit_formula(_node, _class_node, _parent_class_node, body_node) + find_method_with_args(body_node, :keg_only, :provided_by_macos) do + unless tap_style_exception? :provided_by_macos_formulae + problem "Formulae that are `keg_only :provided_by_macos` should be added to "\ + "`style_exceptions/provided_by_macos_formulae.json`" + end + end + end + end + # This cop audits `uses_from_macos` dependencies in formulae. class UsesFromMacos < FormulaCop - # Generate with: - # - # ``` - # brew ruby -e 'puts Formula.select {|f| f.keg_only_reason&.provided_by_macos? }.map(&:name).sort.join("\n")' - # ``` - # - # Not done at runtime as it's too slow and RuboCop doesn't have access. - PROVIDED_BY_MACOS_FORMULAE = %w[ - apr - bc - bison - bzip2 - cups - curl - dyld-headers - ed - expat - file-formula - flex - gcore - gnu-getopt - icu4c - krb5 - libarchive - libedit - libffi - libiconv - libpcap - libressl - libxml2 - libxslt - llvm - lsof - m4 - ncompress - ncurses - net-snmp - openldap - openlibm - pod2man - rpcgen - ruby - sqlite - ssh-copy-id - swift - tcl-tk - texinfo - unifdef - unzip - zip - zlib - ].freeze - - # These formulae aren't `keg_only :provided_by_macos` but are provided by - # macOS (or very similarly, e.g. OpenSSL where system provides LibreSSL). - # TODO: consider making some of these keg-only. - ALLOWED_USES_FROM_MACOS_DEPS = (PROVIDED_BY_MACOS_FORMULAE + %w[ - bash - cpio - expect - groff - gzip - openssl - openssl@1.1 - perl - php - python - python@3 - rsync - vim - xz - 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.instance_of?(RuboCop::AST::StrNode) @@ -90,7 +28,8 @@ module RuboCop parameters(method).first.keys.first end - next if ALLOWED_USES_FROM_MACOS_DEPS.include?(string_content(dep)) + next if tap_style_exception? :provided_by_macos_formulae, string_content(dep) + next if tap_style_exception? :non_keg_only_provided_by_macos_formulae, string_content(dep) problem "`uses_from_macos` should only be used for macOS dependencies, not #{string_content(dep)}." end diff --git a/Library/Homebrew/test/rubocops/uses_from_macos_spec.rb b/Library/Homebrew/test/rubocops/uses_from_macos_spec.rb index 6a12800505..7495d680b2 100644 --- a/Library/Homebrew/test/rubocops/uses_from_macos_spec.rb +++ b/Library/Homebrew/test/rubocops/uses_from_macos_spec.rb @@ -17,6 +17,4 @@ describe RuboCop::Cop::FormulaAudit::UsesFromMacos do end RUBY end - - include_examples "formulae exist", described_class::ALLOWED_USES_FROM_MACOS_DEPS end