From 3f8722c971cedf8b2c66d918fc4dd608bf439009 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Tue, 18 Apr 2017 08:17:24 +0100 Subject: [PATCH] audit: allow skipping audit methods. Add `--only` and `--except` methods which can be used to selectively enable or disable audit groups. --- Library/Homebrew/dev-cmd/audit.rb | 62 ++++++++++--------- .../extend/os/mac/formula_cellar_checks.rb | 8 +-- Library/Homebrew/formula_cellar_checks.rb | 22 +++---- Library/Homebrew/formula_installer.rb | 6 +- Library/Homebrew/test/dev-cmd/audit_spec.rb | 26 ++++---- docs/Manpage.md | 6 +- manpages/brew.1 | 8 ++- 7 files changed, 77 insertions(+), 61 deletions(-) diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 32ae5362c1..8910c6af3a 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -1,4 +1,4 @@ -#: * `audit` [`--strict`] [`--fix`] [`--online`] [`--new-formula`] [`--display-cop-names`] [`--display-filename`] []: +#: * `audit` [`--strict`] [`--fix`] [`--online`] [`--new-formula`] [`--display-cop-names`] [`--display-filename`] [`--only=`|`--except=`]: #: Check for Homebrew coding style violations. This should be #: run before submitting a new formula. #: @@ -23,6 +23,10 @@ #: If `--display-filename` is passed, every line of output is prefixed with the #: name of the file or formula being audited, to make the output easy to grep. #: +#: If `--only` is passed, only the methods named `audit_` will be run. +#: +#: If `--except` is passed, the methods named `audit_` will not be run. +#: #: `audit` exits with a non-zero status if any errors are found. This is useful, #: for instance, for implementing pre-commit hooks. @@ -728,7 +732,7 @@ class FormulaAuditor } end - spec.patches.each { |p| audit_patch(p) if p.external? } + spec.patches.each { |p| patch_problems(p) if p.external? } end %w[Stable Devel].each do |name| @@ -864,10 +868,10 @@ class FormulaAuditor return if legacy_patches.empty? problem "Use the patch DSL instead of defining a 'patches' method" - legacy_patches.each { |p| audit_patch(p) } + legacy_patches.each { |p| patch_problems(p) } end - def audit_patch(patch) + def patch_problems(patch) case patch.url when /raw\.github\.com/, %r{gist\.github\.com/raw}, %r{gist\.github\.com/.+/raw}, %r{gist\.githubusercontent\.com/.+/raw} @@ -939,7 +943,13 @@ class FormulaAuditor problem "require \"language/go\" is unnecessary unless using `go_resource`s" end - def audit_line(line, _lineno) + def audit_lines + text.without_patch.split("\n").each_with_index do |line, lineno| + line_problems(line, lineno+1) + end + end + + def line_problems(line, _lineno) if line =~ /<(Formula|AmazonWebServicesFormula|ScriptFileFormula|GithubGistFormula)/ problem "Use a space in class inheritance: class Foo < #{$1}" end @@ -1142,11 +1152,11 @@ class FormulaAuditor end if line =~ /depends_on :(.+) (if.+|unless.+)$/ - audit_conditional_dep($1.to_sym, $2, $&) + conditional_dep_problems($1.to_sym, $2, $&) end if line =~ /depends_on ['"](.+)['"] (if.+|unless.+)$/ - audit_conditional_dep($1, $2, $&) + conditional_dep_problems($1, $2, $&) end if line =~ /(Dir\[("[^\*{},]+")\])/ @@ -1234,7 +1244,7 @@ class FormulaAuditor EOS end - def audit_conditional_dep(dep, condition, line) + def conditional_dep_problems(dep, condition, line) quoted_dep = quote_dep(dep) dep = Regexp.escape(dep.to_s) @@ -1250,30 +1260,26 @@ class FormulaAuditor dep.is_a?(Symbol) ? dep.inspect : "'#{dep}'" end - def audit_check_output(output) + def problem_if_output(output) problem(output) if output end def audit - audit_file - audit_formula_name - audit_class - audit_specs - audit_revision_and_version_scheme - audit_homepage - audit_bottle_spec - audit_github_repository - audit_deps - audit_conflicts - audit_options - audit_legacy_patches - audit_text - audit_caveats - text.without_patch.split("\n").each_with_index { |line, lineno| audit_line(line, lineno+1) } - audit_installed - audit_prefix_has_contents - audit_reverse_migration - audit_style + only_audits = ARGV.value("only").to_s.split(",") + except_audits = ARGV.value("except").to_s.split(",") + if !only_audits.empty? && !except_audits.empty? + odie "--only and --except cannot be used simulataneously!" + end + + methods.map(&:to_s).grep(/^audit_/).each do |audit_method_name| + name = audit_method_name.gsub(/^audit_/, "") + if !only_audits.empty? + next unless only_audits.include?(name) + elsif !except_audits.empty? + next if except_audits.include?(name) + end + send(audit_method_name) + end end private diff --git a/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb b/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb index 5b1c648bff..10379c9810 100644 --- a/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb +++ b/Library/Homebrew/extend/os/mac/formula_cellar_checks.rb @@ -67,7 +67,7 @@ module FormulaCellarChecks checker = LinkageChecker.new(keg, formula) return unless checker.broken_dylibs? - audit_check_output <<-EOS.undent + problem_if_output <<-EOS.undent The installation was broken. Broken dylib links found: #{checker.broken_dylibs.to_a * "\n "} @@ -76,9 +76,9 @@ module FormulaCellarChecks def audit_installed generic_audit_installed - audit_check_output(check_shadowed_headers) - audit_check_output(check_openssl_links) - audit_check_output(check_python_framework_links(formula.lib)) + problem_if_output(check_shadowed_headers) + problem_if_output(check_openssl_links) + problem_if_output(check_python_framework_links(formula.lib)) check_linkage end end diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index 7db5e748b8..c4d5992967 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -154,17 +154,17 @@ module FormulaCellarChecks end def audit_installed - audit_check_output(check_manpages) - audit_check_output(check_infopages) - audit_check_output(check_jars) - audit_check_output(check_non_libraries) - audit_check_output(check_non_executables(formula.bin)) - audit_check_output(check_generic_executables(formula.bin)) - audit_check_output(check_non_executables(formula.sbin)) - audit_check_output(check_generic_executables(formula.sbin)) - audit_check_output(check_easy_install_pth(formula.lib)) - audit_check_output(check_elisp_dirname(formula.share, formula.name)) - audit_check_output(check_elisp_root(formula.share, formula.name)) + problem_if_output(check_manpages) + problem_if_output(check_infopages) + problem_if_output(check_jars) + problem_if_output(check_non_libraries) + problem_if_output(check_non_executables(formula.bin)) + problem_if_output(check_generic_executables(formula.bin)) + problem_if_output(check_non_executables(formula.sbin)) + problem_if_output(check_generic_executables(formula.sbin)) + problem_if_output(check_easy_install_pth(formula.lib)) + problem_if_output(check_elisp_dirname(formula.share, formula.name)) + problem_if_output(check_elisp_root(formula.share, formula.name)) end alias generic_audit_installed audit_installed diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index 017be51dc6..0bdcb16611 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -858,15 +858,15 @@ class FormulaInstaller tab.write end - def audit_check_output(output) + def problem_if_output(output) return unless output opoo output @show_summary_heading = true end def audit_installed - audit_check_output(check_env_path(formula.bin)) - audit_check_output(check_env_path(formula.sbin)) + problem_if_output(check_env_path(formula.bin)) + problem_if_output(check_env_path(formula.sbin)) super end diff --git a/Library/Homebrew/test/dev-cmd/audit_spec.rb b/Library/Homebrew/test/dev-cmd/audit_spec.rb index a6bb22837d..8a8096849e 100644 --- a/Library/Homebrew/test/dev-cmd/audit_spec.rb +++ b/Library/Homebrew/test/dev-cmd/audit_spec.rb @@ -303,7 +303,7 @@ describe FormulaAuditor do end end - describe "#audit_line" do + describe "#line_problems" do specify "pkgshare" do fa = formula_auditor "foo", <<-EOS.undent, strict: true class Foo < Formula @@ -311,25 +311,25 @@ describe FormulaAuditor do end EOS - fa.audit_line 'ohai "#{share}/foo"', 3 + fa.line_problems 'ohai "#{share}/foo"', 3 expect(fa.problems.shift).to eq("Use \#{pkgshare} instead of \#{share}/foo") - fa.audit_line 'ohai "#{share}/foo/bar"', 3 + fa.line_problems 'ohai "#{share}/foo/bar"', 3 expect(fa.problems.shift).to eq("Use \#{pkgshare} instead of \#{share}/foo") - fa.audit_line 'ohai share/"foo"', 3 + fa.line_problems 'ohai share/"foo"', 3 expect(fa.problems.shift).to eq('Use pkgshare instead of (share/"foo")') - fa.audit_line 'ohai share/"foo/bar"', 3 + fa.line_problems 'ohai share/"foo/bar"', 3 expect(fa.problems.shift).to eq('Use pkgshare instead of (share/"foo")') - fa.audit_line 'ohai "#{share}/foo-bar"', 3 + fa.line_problems 'ohai "#{share}/foo-bar"', 3 expect(fa.problems).to eq([]) - fa.audit_line 'ohai share/"foo-bar"', 3 + fa.line_problems 'ohai share/"foo-bar"', 3 expect(fa.problems).to eq([]) - fa.audit_line 'ohai share/"bar"', 3 + fa.line_problems 'ohai share/"bar"', 3 expect(fa.problems).to eq([]) end @@ -344,11 +344,11 @@ describe FormulaAuditor do end EOS - fa.audit_line 'ohai "#{share}/foolibc++"', 3 + fa.line_problems 'ohai "#{share}/foolibc++"', 3 expect(fa.problems.shift) .to eq("Use \#{pkgshare} instead of \#{share}/foolibc++") - fa.audit_line 'ohai share/"foolibc++"', 3 + fa.line_problems 'ohai share/"foolibc++"', 3 expect(fa.problems.shift) .to eq('Use pkgshare instead of (share/"foolibc++")') end @@ -360,7 +360,7 @@ describe FormulaAuditor do end EOS - fa.audit_line "class Foo\fR will be run\. +. +.IP +If \fB\-\-except\fR is passed, the methods named \fBaudit_\fR will not be run\. +. +.IP \fBaudit\fR exits with a non\-zero status if any errors are found\. This is useful, for instance, for implementing pre\-commit hooks\. . .TP