From 24873454609a66c45ffdde7eb5931a7c6c55c921 Mon Sep 17 00:00:00 2001 From: Jonathan Chang Date: Wed, 15 Aug 2018 19:29:22 -0400 Subject: [PATCH] audit: fixes for test checks --- Library/Homebrew/dev-cmd/audit.rb | 6 +----- Library/Homebrew/rubocops/class_cop.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 8818bf2474..de9a156c04 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -772,7 +772,7 @@ module Homebrew end bin_names.each do |name| ["system", "shell_output", "pipe_output"].each do |cmd| - if text =~ %r{(def test|test do).*(#{Regexp.escape(HOMEBREW_PREFIX)}/bin/)?#{cmd}[\(\s]+['"]#{Regexp.escape(name)}[\s'"]}m + if text =~ /test do.*#{cmd}[\(\s]+['"]#{Regexp.escape(name)}[\s'"]/m problem %Q(fully scope test #{cmd} calls e.g. #{cmd} "\#{bin}/#{name}") end end @@ -803,10 +803,6 @@ module Homebrew problem "Use separate make calls" if line.include?("make && make") - if line =~ /shell_output\(['"].+['"], 0\)/ - problem "Passing 0 to shell_output() is redundant" - end - if line =~ /JAVA_HOME/i && !formula.requirements.map(&:class).include?(JavaRequirement) problem "Use `depends_on :java` to set JAVA_HOME" end diff --git a/Library/Homebrew/rubocops/class_cop.rb b/Library/Homebrew/rubocops/class_cop.rb index 8e0df3cbc8..0976104752 100644 --- a/Library/Homebrew/rubocops/class_cop.rb +++ b/Library/Homebrew/rubocops/class_cop.rb @@ -22,6 +22,27 @@ module RuboCop end end end + + class TestCalls < FormulaCop + def audit_formula(_node, _class_node, _parent_class_node, body_node) + test_calls(find_block(body_node, :test)) do |node, params| + p1, p2 = params + if match = string_content(p1).match(%r{(/usr/local/(s?bin))}) + offending_node(p1) + problem "use \#{#{match[2]}} instead of #{match[1]} in #{node}" + end + + if node == :shell_output && node_equals?(p2, 0) + offending_node(p2) + problem "Passing 0 to shell_output() is redundant" + end + end + end + + def_node_search :test_calls, <<~EOS + (send nil? ${:system :shell_output :pipe_output} $...) + EOS + end end module FormulaAuditStrict