diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb index 0491845b5d..7e434d7550 100644 --- a/Library/Homebrew/rubocops/lines_cop.rb +++ b/Library/Homebrew/rubocops/lines_cop.rb @@ -54,6 +54,87 @@ module RuboCop end end + class AssertStatements < FormulaCop + def audit_formula(_node, _class_node, _parent_class_node, body_node) + find_every_method_call_by_name(body_node, :assert).each do |method| + if method_called_ever?(method, :include?) && !method_called_ever?(method, :!) + problem "Use `assert_match` instead of `assert ...include?`" + end + + if method_called_ever?(method, :exist?) && !method_called_ever?(method, :!) + problem "Use `assert_predicate , :exist?` instead of `#{method.source}`" + end + + if method_called_ever?(method, :exist?) && method_called_ever?(method, :!) + problem "Use `refute_predicate , :exist?` instead of `#{method.source}`" + end + + if method_called_ever?(method, :executable?) && !method_called_ever?(method, :!) + problem "Use `assert_predicate , :executable?` instead of `#{method.source}`" + end + end + end + end + + class OptionDeclarations < FormulaCop + def audit_formula(_node, _class_node, _parent_class_node, body_node) + if find_method_def(body_node, :options) + problem "Use new-style option definitions" + end + + find_instance_method_call(body_node, :build, :without?) do |method| + next unless unless_modifier?(method.parent) + correct = method.source.gsub("out?", "?") + problem "Use if #{correct} instead of unless #{method.source}" + end + + find_instance_method_call(body_node, :build, :with?) do |method| + next unless unless_modifier?(method.parent) + correct = method.source.gsub("?", "out?") + problem "Use if #{correct} instead of unless #{method.source}" + end + + find_instance_method_call(body_node, :build, :with?) do |method| + next unless negated?(method.parent) + problem "Don't negate 'build.with?': use 'build.without?'" + end + + find_instance_method_call(body_node, :build, :without?) do |method| + next unless negated?(method.parent) + problem "Don't negate 'build.without?': use 'build.with?'" + end + + find_instance_method_call(body_node, :build, :without?) do |method| + arg = parameters(method).first + next unless match = regex_match_group(arg, /-?-?without-(.*)/) + problem "Don't duplicate 'without': Use `build.without? \"#{match[1]}\"` to check for \"--without-#{match[1]}\"" + end + + find_instance_method_call(body_node, :build, :with?) do |method| + arg = parameters(method).first + next unless match = regex_match_group(arg, /-?-?with-(.*)/) + problem "Don't duplicate 'with': Use `build.with? \"#{match[1]}\"` to check for \"--with-#{match[1]}\"" + end + + find_instance_method_call(body_node, :build, :include?) do |method| + arg = parameters(method).first + next unless match = regex_match_group(arg, /with(out)?-(.*)/) + problem "Use build.with#{match[1]}? \"#{match[2]}\" instead of build.include? 'with#{match[1]}-#{match[2]}'" + end + + find_instance_method_call(body_node, :build, :include?) do |method| + arg = parameters(method).first + next unless match = regex_match_group(arg, /\-\-(.*)/) + problem "Reference '#{match[1]}' without dashes" + end + end + + def unless_modifier?(node) + return false unless node.if_type? + node.modifier_form? && node.unless? + end + end + class Miscellaneous < FormulaCop def audit_formula(_node, _class_node, _parent_class_node, body_node) # FileUtils is included in Formula @@ -115,7 +196,7 @@ module RuboCop # Prefer formula path shortcuts in strings formula_path_strings(body_node, :share) do |p| next unless match = regex_match_group(p, %r{(/(man))/?}) - problem "\"\#\{share}#{match[1]}\" should be \"\#{#{match[2]}}\"" + problem "\"\#{share}#{match[1]}\" should be \"\#{#{match[2]}}\"" end formula_path_strings(body_node, :prefix) do |p| @@ -215,10 +296,6 @@ module RuboCop problem "Use new-style test definitions (test do)" end - if find_method_def(body_node, :options) - problem "Use new-style option definitions" - end - find_method_with_args(body_node, :skip_clean, :all) do problem <<-EOS.undent.chomp `skip_clean :all` is deprecated; brew no longer strips symbols @@ -226,6 +303,10 @@ module RuboCop EOS end + if find_method_def(@processed_source.ast) + problem "Define method #{method_name(@offensive_node)} in the class body, not at the top-level" + end + find_instance_method_call(body_node, :build, :universal?) do next if @formula_name == "wine" problem "macOS has been 64-bit only since 10.6 so build.universal? is deprecated." @@ -239,24 +320,6 @@ module RuboCop problem 'Use "depends_on :x11" instead of "ENV.x11"' end - find_every_method_call_by_name(body_node, :assert).each do |method| - if method_called_ever?(method, :include?) && !method_called_ever?(method, :!) - problem "Use `assert_match` instead of `assert ...include?`" - end - - if method_called_ever?(method, :exist?) && !method_called_ever?(method, :!) - problem "Use `assert_predicate , :exist?` instead of `#{method.source}`" - end - - if method_called_ever?(method, :exist?) && method_called_ever?(method, :!) - problem "Use `refute_predicate , :exist?` instead of `#{method.source}`" - end - - if method_called_ever?(method, :executable?) && !method_called_ever?(method, :!) - problem "Use `assert_predicate , :executable?` instead of `#{method.source}`" - end - end - find_every_method_call_by_name(body_node, :depends_on).each do |method| next unless method_called?(method, :new) problem "`depends_on` can take requirement classes instead of instances" @@ -282,61 +345,6 @@ module RuboCop next unless match = regex_match_group(param, fileutils_methods) problem "Use the `#{match}` Ruby method instead of `#{method.source}`" end - - if find_method_def(@processed_source.ast) - problem "Define method #{method_name(@offensive_node)} in the class body, not at the top-level" - end - - find_instance_method_call(body_node, :build, :without?) do |method| - next unless unless_modifier?(method.parent) - correct = method.source.gsub("out?", "?") - problem "Use if #{correct} instead of unless #{method.source}" - end - - find_instance_method_call(body_node, :build, :with?) do |method| - next unless unless_modifier?(method.parent) - correct = method.source.gsub("?", "out?") - problem "Use if #{correct} instead of unless #{method.source}" - end - - find_instance_method_call(body_node, :build, :with?) do |method| - next unless negated?(method.parent) - problem "Don't negate 'build.with?': use 'build.without?'" - end - - find_instance_method_call(body_node, :build, :without?) do |method| - next unless negated?(method.parent) - problem "Don't negate 'build.without?': use 'build.with?'" - end - - find_instance_method_call(body_node, :build, :without?) do |method| - arg = parameters(method).first - next unless match = regex_match_group(arg, /-?-?without-(.*)/) - problem "Don't duplicate 'without': Use `build.without? \"#{match[1]}\"` to check for \"--without-#{match[1]}\"" - end - - find_instance_method_call(body_node, :build, :with?) do |method| - arg = parameters(method).first - next unless match = regex_match_group(arg, /-?-?with-(.*)/) - problem "Don't duplicate 'with': Use `build.with? \"#{match[1]}\"` to check for \"--with-#{match[1]}\"" - end - - find_instance_method_call(body_node, :build, :include?) do |method| - arg = parameters(method).first - next unless match = regex_match_group(arg, /with(out)?-(.*)/) - problem "Use build.with#{match[1]}? \"#{match[2]}\" instead of build.include? 'with#{match[1]}-#{match[2]}'" - end - - find_instance_method_call(body_node, :build, :include?) do |method| - arg = parameters(method).first - next unless match = regex_match_group(arg, /\-\-(.*)/) - problem "Reference '#{match[1]}' without dashes" - end - end - - def unless_modifier?(node) - return false unless node.if_type? - node.modifier_form? && node.unless? end def modifier?(node) diff --git a/Library/Homebrew/test/rubocops/lines_cop_spec.rb b/Library/Homebrew/test/rubocops/lines_cop_spec.rb index df40857216..9705e82789 100644 --- a/Library/Homebrew/test/rubocops/lines_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_cop_spec.rb @@ -7,7 +7,7 @@ describe RuboCop::Cop::FormulaAudit::Lines do subject(:cop) { described_class.new } context "When auditing lines" do - it "with correctable deprecated dependencies" do + it "correctable deprecated dependencies usage" do formulae = [{ "dependency" => :automake, "correct" => "automake", @@ -36,11 +36,11 @@ describe RuboCop::Cop::FormulaAudit::Lines do else offense = ":#{formula["dependency"]} is deprecated" end - expected_offenses = [{ message: offense, - severity: :convention, - line: 3, - column: 2, - source: source }] + expected_offenses = [{ message: offense, + severity: :convention, + line: 3, + column: 2, + source: source }] inspect_source(source) @@ -56,7 +56,7 @@ describe RuboCop::Cop::FormulaAudit::ClassInheritance do subject(:cop) { described_class.new } context "When auditing lines" do - it "with no space in class inheritance" do + it "inconsistent space in class inheritance" do source = <<-EOS.undent class Foo, :exist?` instead of `assert File.exist? "default.ini"`', + severity: :convention, + line: 4, + column: 9, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "assert ...exist? with a negation" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + assert !File.exist?("default.ini") + end + EOS + + expected_offenses = [{ message: 'Use `refute_predicate , :exist?` instead of `assert !File.exist?("default.ini")`', + severity: :convention, + line: 4, + column: 9, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "assert ...executable? without a negation" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + assert File.executable? f + end + EOS + + expected_offenses = [{ message: "Use `assert_predicate , :executable?` instead of `assert File.executable? f`", + severity: :convention, + line: 4, + column: 9, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end +end + +describe RuboCop::Cop::FormulaAudit::OptionDeclarations do + subject(:cop) { described_class.new } + + it "unless build.without? conditional" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def post_install + return unless build.without? "bar" + end + end + EOS + + expected_offenses = [{ message: 'Use if build.with? "bar" instead of unless build.without? "bar"', + severity: :convention, + line: 5, + column: 18, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "unless build.with? conditional" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def post_install + return unless build.with? "bar" + end + end + EOS + + expected_offenses = [{ message: 'Use if build.without? "bar" instead of unless build.with? "bar"', + severity: :convention, + line: 5, + column: 18, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "negated build.with? conditional" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def post_install + return if !build.with? "bar" + end + end + EOS + + expected_offenses = [{ message: "Don't negate 'build.with?': use 'build.without?'", + severity: :convention, + line: 5, + column: 14, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "negated build.without? conditional" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def post_install + return if !build.without? "bar" + end + end + EOS + + expected_offenses = [{ message: "Don't negate 'build.without?': use 'build.with?'", + severity: :convention, + line: 5, + column: 14, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "unnecessary build.without? conditional" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def post_install + return if build.without? "--without-bar" + end + end + EOS + + expected_offenses = [{ message: "Don't duplicate 'without': Use `build.without? \"bar\"` to check for \"--without-bar\"", + severity: :convention, + line: 5, + column: 30, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "unnecessary build.with? conditional" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def post_install + return if build.with? "--with-bar" + end + end + EOS + + expected_offenses = [{ message: "Don't duplicate 'with': Use `build.with? \"bar\"` to check for \"--with-bar\"", + severity: :convention, + line: 5, + column: 27, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "build.include? conditional" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def post_install + return if build.include? "without-bar" + end + end + EOS + + expected_offenses = [{ message: "Use build.without? \"bar\" instead of build.include? 'without-bar'", + severity: :convention, + line: 5, + column: 30, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "build.include? with dashed args conditional" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def post_install + return if build.include? "--bar" + end + end + EOS + + expected_offenses = [{ message: "Reference 'bar' without dashes", + severity: :convention, + line: 5, + column: 30, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "def options usage" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + + def options + [["--bar", "desc"]] + end + end + EOS + + expected_offenses = [{ message: "Use new-style option definitions", + severity: :convention, + line: 5, + column: 2, + source: source }] + + inspect_source(source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end +end + describe RuboCop::Cop::FormulaAudit::Miscellaneous do subject(:cop) { described_class.new } context "When auditing formula" do - it "with FileUtils" do + it "FileUtils usage" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -164,11 +477,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Don't need 'FileUtils.' before mv", - severity: :convention, - line: 4, - column: 2, - source: source }] + expected_offenses = [{ message: "Don't need 'FileUtils.' before mv", + severity: :convention, + line: 4, + column: 2, + source: source }] inspect_source(source) @@ -177,7 +490,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with long inreplace block vars" do + it "long inreplace block vars" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -188,11 +501,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "\"inreplace do |s|\" is preferred over \"|longvar|\".", - severity: :convention, - line: 4, - column: 2, - source: source }] + expected_offenses = [{ message: "\"inreplace do |s|\" is preferred over \"|longvar|\".", + severity: :convention, + line: 4, + column: 2, + source: source }] inspect_source(source) @@ -201,7 +514,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with invalid rebuild" do + it "invalid rebuild statement" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -213,11 +526,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "'rebuild 0' should be removed", - severity: :convention, - line: 5, - column: 4, - source: source }] + expected_offenses = [{ message: "'rebuild 0' should be removed", + severity: :convention, + line: 5, + column: 4, + source: source }] inspect_source(source) @@ -226,7 +539,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with OS.linux? check" do + it "OS.linux? check" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -240,11 +553,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Don't use OS.linux?; Homebrew/core only supports macOS", - severity: :convention, - line: 5, - column: 7, - source: source }] + expected_offenses = [{ message: "Don't use OS.linux?; Homebrew/core only supports macOS", + severity: :convention, + line: 5, + column: 7, + source: source }] inspect_source(source, "/homebrew-core/") @@ -253,7 +566,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with fails_with :llvm" do + it "fails_with :llvm block" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -268,11 +581,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "'fails_with :llvm' is now a no-op so should be removed", - severity: :convention, - line: 7, - column: 2, - source: source }] + expected_offenses = [{ message: "'fails_with :llvm' is now a no-op so should be removed", + severity: :convention, + line: 7, + column: 2, + source: source }] inspect_source(source) @@ -281,7 +594,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with def test" do + it "def test deprecated usage" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -293,11 +606,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use new-style test definitions (test do)", - severity: :convention, - line: 5, - column: 2, - source: source }] + expected_offenses = [{ message: "Use new-style test definitions (test do)", + severity: :convention, + line: 5, + column: 2, + source: source }] inspect_source(source) @@ -306,32 +619,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with def options" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - - def options - [["--bar", "desc"]] - end - end - EOS - - expected_offenses = [{ message: "Use new-style option definitions", - severity: :convention, - line: 5, - column: 2, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "with deprecated skip_clean call" do + it "deprecated skip_clean call" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -356,7 +644,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with build.universal?" do + it "build.universal? deprecated usage" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -367,11 +655,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "macOS has been 64-bit only since 10.6 so build.universal? is deprecated.", - severity: :convention, - line: 4, - column: 5, - source: source }] + expected_offenses = [{ message: "macOS has been 64-bit only since 10.6 so build.universal? is deprecated.", + severity: :convention, + line: 4, + column: 5, + source: source }] inspect_source(source) @@ -380,7 +668,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with build.universal? exempted formula" do + it "build.universal? deprecation exempted formula" do source = <<-EOS.undent class Wine < Formula desc "foo" @@ -395,7 +683,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do expect(cop.offenses).to eq([]) end - it "with ENV.universal_binary" do + it "deprecated ENV.universal_binary usage" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -406,11 +694,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "macOS has been 64-bit only since 10.6 so ENV.universal_binary is deprecated.", - severity: :convention, - line: 5, - column: 5, - source: source }] + expected_offenses = [{ message: "macOS has been 64-bit only since 10.6 so ENV.universal_binary is deprecated.", + severity: :convention, + line: 5, + column: 5, + source: source }] inspect_source(source) @@ -419,7 +707,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with ENV.universal_binary 2" do + it "deprecated ENV.x11 usage" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -430,11 +718,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: 'Use "depends_on :x11" instead of "ENV.x11"', - severity: :convention, - line: 5, - column: 5, - source: source }] + expected_offenses = [{ message: 'Use "depends_on :x11" instead of "ENV.x11"', + severity: :convention, + line: 5, + column: 5, + source: source }] inspect_source(source) @@ -443,7 +731,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with ruby-macho alternatives" do + it "ruby-macho alternative to install_name_tool usage" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -452,11 +740,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: 'Use ruby-macho instead of calling "install_name_tool"', - severity: :convention, - line: 4, - column: 10, - source: source }] + expected_offenses = [{ message: 'Use ruby-macho instead of calling "install_name_tool"', + severity: :convention, + line: 4, + column: 10, + source: source }] inspect_source(source) @@ -465,7 +753,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with ruby-macho alternatives audit exempted formula" do + it "ruby-macho alternatives audit exempted formula" do source = <<-EOS.undent class Cctools < Formula desc "foo" @@ -478,7 +766,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do expect(cop.offenses).to eq([]) end - it "with npm install without language::Node args" do + it "npm install without language::Node args" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -487,11 +775,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use Language::Node for npm install args", - severity: :convention, - line: 4, - column: 2, - source: source }] + expected_offenses = [{ message: "Use Language::Node for npm install args", + severity: :convention, + line: 4, + column: 2, + source: source }] inspect_source(source) @@ -500,7 +788,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with npm install without language::Node args in kibana" do + it "npm install without language::Node args in kibana(exempted formula)" do source = <<-EOS.undent class KibanaAT44 < Formula desc "foo" @@ -513,95 +801,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do expect(cop.offenses).to eq([]) end - it "with assert include" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - assert File.read("inbox").include?("Sample message 1") - end - EOS - - expected_offenses = [{ message: "Use `assert_match` instead of `assert ...include?`", - severity: :convention, - line: 4, - column: 9, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "assert ...exist? without a negation" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - assert File.exist? "default.ini" - end - EOS - - expected_offenses = [{ message: 'Use `assert_predicate , :exist?` instead of `assert File.exist? "default.ini"`', - severity: :convention, - line: 4, - column: 9, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "assert ...exist? with a negation" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - assert !File.exist?("default.ini") - end - EOS - - expected_offenses = [{ message: 'Use `refute_predicate , :exist?` instead of `assert !File.exist?("default.ini")`', - severity: :convention, - line: 4, - column: 9, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "assert ...executable? without a negation" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - assert File.executable? f - end - EOS - - expected_offenses = [{ message: "Use `assert_predicate , :executable?` instead of `assert File.executable? f`", - severity: :convention, - line: 4, - column: 9, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "depends_on with an instance as an argument" do + it "depends_on with an instance as argument" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -610,11 +810,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "`depends_on` can take requirement classes instead of instances", - severity: :convention, - line: 4, - column: 13, - source: source }] + expected_offenses = [{ message: "`depends_on` can take requirement classes instead of instances", + severity: :convention, + line: 4, + column: 13, + source: source }] inspect_source(source) @@ -623,7 +823,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with old style OS check" do + it "old style OS check" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -632,11 +832,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "\"MacOS.snow_leopard?\" is deprecated, use a comparison to MacOS.version instead", - severity: :convention, - line: 4, - column: 21, - source: source }] + expected_offenses = [{ message: "\"MacOS.snow_leopard?\" is deprecated, use a comparison to MacOS.version instead", + severity: :convention, + line: 4, + column: 21, + source: source }] inspect_source(source) @@ -645,7 +845,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with non glob DIR" do + it "non glob DIR usage" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -655,11 +855,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Dir([\"src/snapshot.pyc\"]) is unnecessary; just use \"src/snapshot.pyc\"", - severity: :convention, - line: 5, - column: 13, - source: source }] + expected_offenses = [{ message: 'Dir(["src/snapshot.pyc"]) is unnecessary; just use "src/snapshot.pyc"', + severity: :convention, + line: 5, + column: 13, + source: source }] inspect_source(source) @@ -668,7 +868,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with system call to fileUtils Method" do + it "system call to fileUtils Method" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -677,11 +877,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use the `mkdir` Ruby method instead of `system \"mkdir\", \"foo\"`", - severity: :convention, - line: 4, - column: 10, - source: source }] + expected_offenses = [{ message: 'Use the `mkdir` Ruby method instead of `system "mkdir", "foo"`', + severity: :convention, + line: 4, + column: 10, + source: source }] inspect_source(source) @@ -690,7 +890,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with a top-level function def " do + it "top-level function def outside class body" do source = <<-EOS.undent def test nil @@ -701,11 +901,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Define method test in the class body, not at the top-level", - severity: :convention, - line: 1, - column: 0, - source: source }] + expected_offenses = [{ message: "Define method test in the class body, not at the top-level", + severity: :convention, + line: 1, + column: 0, + source: source }] inspect_source(source) @@ -714,199 +914,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with unless build.without?" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - def post_install - return unless build.without? "bar" - end - end - EOS - - expected_offenses = [{ message: 'Use if build.with? "bar" instead of unless build.without? "bar"', - severity: :convention, - line: 5, - column: 18, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "with unless build.with?" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - def post_install - return unless build.with? "bar" - end - end - EOS - - expected_offenses = [{ message: 'Use if build.without? "bar" instead of unless build.with? "bar"', - severity: :convention, - line: 5, - column: 18, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "with negated build.with?" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - def post_install - return if !build.with? "bar" - end - end - EOS - - expected_offenses = [{ message: "Don't negate 'build.with?': use 'build.without?'", - severity: :convention, - line: 5, - column: 14, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "with negated build.with?" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - def post_install - return if !build.without? "bar" - end - end - EOS - - expected_offenses = [{ message: "Don't negate 'build.without?': use 'build.with?'", - severity: :convention, - line: 5, - column: 14, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "with duplicated build.without?" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - def post_install - return if build.without? "--without-bar" - end - end - EOS - - expected_offenses = [{ message: "Don't duplicate 'without': Use `build.without? \"bar\"` to check for \"--without-bar\"", - severity: :convention, - line: 5, - column: 30, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "with duplicated build.with?" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - def post_install - return if build.with? "--with-bar" - end - end - EOS - - expected_offenses = [{ message: "Don't duplicate 'with': Use `build.with? \"bar\"` to check for \"--with-bar\"", - severity: :convention, - line: 5, - column: 27, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "with build.include?" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - def post_install - return if build.include? "without-bar" - end - end - EOS - - expected_offenses = [{ message: "Use build.without? \"bar\" instead of build.include? 'without-bar'", - severity: :convention, - line: 5, - column: 30, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "with build.include? with dashed args" do - source = <<-EOS.undent - class Foo < Formula - desc "foo" - url 'http://example.com/foo-1.0.tgz' - def post_install - return if build.include? "--bar" - end - end - EOS - - expected_offenses = [{ message: "Reference 'bar' without dashes", - severity: :convention, - line: 5, - column: 30, - source: source }] - - inspect_source(source) - - expected_offenses.zip(cop.offenses).each do |expected, actual| - expect_offense(expected, actual) - end - end - - it "with using ARGV to check options" do + it "Using ARGV to check options" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -917,11 +925,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use build instead of ARGV to check options", - severity: :convention, - line: 5, - column: 14, - source: source }] + expected_offenses = [{ message: "Use build instead of ARGV to check options", + severity: :convention, + line: 5, + column: 14, + source: source }] inspect_source(source) @@ -930,7 +938,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with man+ " do + it 'man+"man8" usage' do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -941,11 +949,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "\"man+\"man8\"\" should be \"man8\"", - severity: :convention, - line: 5, - column: 22, - source: source }] + expected_offenses = [{ message: '"man+"man8"" should be "man8"', + severity: :convention, + line: 5, + column: 22, + source: source }] inspect_source(source) @@ -954,7 +962,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with hardcoded compiler 1 " do + it "hardcoded gcc compiler" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -965,11 +973,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use \"\#{ENV.cc}\" instead of hard-coding \"gcc\"", - severity: :convention, - line: 5, - column: 12, - source: source }] + expected_offenses = [{ message: "Use \"\#{ENV.cc}\" instead of hard-coding \"gcc\"", + severity: :convention, + line: 5, + column: 12, + source: source }] inspect_source(source) @@ -978,7 +986,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with hardcoded compiler 2 " do + it "hardcoded g++ compiler" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -989,11 +997,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use \"\#{ENV.cxx}\" instead of hard-coding \"g++\"", - severity: :convention, - line: 5, - column: 12, - source: source }] + expected_offenses = [{ message: "Use \"\#{ENV.cxx}\" instead of hard-coding \"g++\"", + severity: :convention, + line: 5, + column: 12, + source: source }] inspect_source(source) @@ -1002,7 +1010,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with hardcoded compiler 3 " do + it "hardcoded llvm-g++ compiler" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1013,11 +1021,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use \"\#{ENV.cxx}\" instead of hard-coding \"llvm-g++\"", - severity: :convention, - line: 5, - column: 28, - source: source }] + expected_offenses = [{ message: "Use \"\#{ENV.cxx}\" instead of hard-coding \"llvm-g++\"", + severity: :convention, + line: 5, + column: 28, + source: source }] inspect_source(source) @@ -1026,7 +1034,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with hardcoded compiler 4 " do + it "hardcoded gcc compiler" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1037,11 +1045,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use \"\#{ENV.cc}\" instead of hard-coding \"gcc\"", - severity: :convention, - line: 5, - column: 28, - source: source }] + expected_offenses = [{ message: "Use \"\#{ENV.cc}\" instead of hard-coding \"gcc\"", + severity: :convention, + line: 5, + column: 28, + source: source }] inspect_source(source) @@ -1050,7 +1058,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with formula path shortcut long form" do + it "formula path shortcut : man" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1061,11 +1069,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "\"\#\{share}/man\" should be \"\#{man}\"", - severity: :convention, - line: 5, - column: 17, - source: source }] + expected_offenses = [{ message: '"#{share}/man" should be "#{man}"', + severity: :convention, + line: 5, + column: 17, + source: source }] inspect_source(source) @@ -1074,7 +1082,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with formula path shortcut long form 1" do + it "formula path shortcut : libexec" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1085,11 +1093,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "\"\#\{prefix}/libexec\" should be \"\#{libexec}\"", - severity: :convention, - line: 5, - column: 18, - source: source }] + expected_offenses = [{ message: "\"\#\{prefix}/libexec\" should be \"\#{libexec}\"", + severity: :convention, + line: 5, + column: 18, + source: source }] inspect_source(source) @@ -1098,7 +1106,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with formula path shortcut long form 2" do + it "formula path shortcut : info" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1109,11 +1117,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "\"\#\{prefix}/share/info\" should be \"\#{info}\"", - severity: :convention, - line: 5, - column: 47, - source: source }] + expected_offenses = [{ message: "\"\#\{prefix}/share/info\" should be \"\#{info}\"", + severity: :convention, + line: 5, + column: 47, + source: source }] inspect_source(source) @@ -1122,7 +1130,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with formula path shortcut long form 3" do + it "formula path shortcut : man8" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1133,11 +1141,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "\"\#\{prefix}/share/man/man8\" should be \"\#{man8}\"", - severity: :convention, - line: 5, - column: 46, - source: source }] + expected_offenses = [{ message: "\"\#\{prefix}/share/man/man8\" should be \"\#{man8}\"", + severity: :convention, + line: 5, + column: 46, + source: source }] inspect_source(source) @@ -1146,7 +1154,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with dependecies which have to vendored" do + it "dependecies which have to vendored" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1155,11 +1163,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "lua modules should be vendored rather than use deprecated depends_on \"lpeg\" => :lua51`", - severity: :convention, - line: 4, - column: 24, - source: source }] + expected_offenses = [{ message: "lua modules should be vendored rather than use deprecated depends_on \"lpeg\" => :lua51`", + severity: :convention, + line: 4, + column: 24, + source: source }] inspect_source(source) @@ -1168,7 +1176,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with setting env manually" do + it "manually setting env" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1177,11 +1185,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use ENV instead of invoking 'export' to modify the environment", - severity: :convention, - line: 4, - column: 10, - source: source }] + expected_offenses = [{ message: "Use ENV instead of invoking 'export' to modify the environment", + severity: :convention, + line: 4, + column: 10, + source: source }] inspect_source(source) @@ -1190,7 +1198,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with dependencies with invalid options" do + it "dependencies with invalid options" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1199,11 +1207,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Dependency foo should not use option with-bar", - severity: :convention, - line: 4, - column: 13, - source: source }] + expected_offenses = [{ message: "Dependency foo should not use option with-bar", + severity: :convention, + line: 4, + column: 13, + source: source }] inspect_source(source) @@ -1212,7 +1220,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with inspecting version" do + it "inspecting version manually" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1223,11 +1231,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use 'build.head?' instead of inspecting 'version'", - severity: :convention, - line: 4, - column: 5, - source: source }] + expected_offenses = [{ message: "Use 'build.head?' instead of inspecting 'version'", + severity: :convention, + line: 4, + column: 5, + source: source }] inspect_source(source) @@ -1236,7 +1244,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with ENV.fortran" do + it "deprecated ENV.fortran usage" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1247,11 +1255,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use `depends_on :fortran` instead of `ENV.fortran`", - severity: :convention, - line: 5, - column: 4, - source: source }] + expected_offenses = [{ message: "Use `depends_on :fortran` instead of `ENV.fortran`", + severity: :convention, + line: 5, + column: 4, + source: source }] inspect_source(source) @@ -1260,7 +1268,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with ARGV.include? (--HEAD)" do + it "deprecated ARGV.include? (--HEAD) usage" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1271,11 +1279,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use \"if build.head?\" instead", - severity: :convention, - line: 5, - column: 26, - source: source }] + expected_offenses = [{ message: 'Use "if build.head?" instead', + severity: :convention, + line: 5, + column: 26, + source: source }] inspect_source(source) @@ -1284,7 +1292,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with MACOS_VERSION const" do + it "deprecated MACOS_VERSION const usage" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1295,11 +1303,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: "Use MacOS.version instead of MACOS_VERSION", - severity: :convention, - line: 5, - column: 14, - source: source }] + expected_offenses = [{ message: "Use MacOS.version instead of MACOS_VERSION", + severity: :convention, + line: 5, + column: 14, + source: source }] inspect_source(source) @@ -1308,7 +1316,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with if conditional dep" do + it "deprecated if build.with? conditional dependency" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1317,11 +1325,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: 'Replace depends_on "foo" if build.with? "with-foo" with depends_on "foo" => :optional', - severity: :convention, - line: 4, - column: 2, - source: source }] + expected_offenses = [{ message: 'Replace depends_on "foo" if build.with? "with-foo" with depends_on "foo" => :optional', + severity: :convention, + line: 4, + column: 2, + source: source }] inspect_source(source) @@ -1330,7 +1338,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with unless conditional dep and symbol" do + it "unless conditional dependency with build.without?" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1339,11 +1347,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: 'Replace depends_on :foo unless build.without? "foo" with depends_on :foo => :recommended', - severity: :convention, - line: 4, - column: 2, - source: source }] + expected_offenses = [{ message: 'Replace depends_on :foo unless build.without? "foo" with depends_on :foo => :recommended', + severity: :convention, + line: 4, + column: 2, + source: source }] inspect_source(source) @@ -1352,7 +1360,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end end - it "with unless conditional dep with build.include?" do + it "unless conditional dependency with build.include?" do source = <<-EOS.undent class Foo < Formula desc "foo" @@ -1361,11 +1369,11 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do end EOS - expected_offenses = [{ message: 'Replace depends_on :foo unless build.include? "without-foo" with depends_on :foo => :recommended', - severity: :convention, - line: 4, - column: 2, - source: source }] + expected_offenses = [{ message: 'Replace depends_on :foo unless build.include? "without-foo" with depends_on :foo => :recommended', + severity: :convention, + line: 4, + column: 2, + source: source }] inspect_source(source)