From 65ae6bacd8c92d718b259f7efd50fc3fe9f0838b Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Mon, 14 Aug 2017 20:10:45 +0530 Subject: [PATCH] add tests for hardcoded compilers in ENV --- Library/Homebrew/rubocops/lines_cop.rb | 20 +++---- .../Homebrew/test/rubocops/lines_cop_spec.rb | 56 +++++++++++++++++-- 2 files changed, 62 insertions(+), 14 deletions(-) diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb index 4d2d63cce2..919b212438 100644 --- a/Library/Homebrew/rubocops/lines_cop.rb +++ b/Library/Homebrew/rubocops/lines_cop.rb @@ -108,16 +108,16 @@ module RuboCop problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\"" end end - # - # find_instance_method_call(body_node, :ENV, :[]=) do |m| - # param = parameters(m)[1] - # if match = regex_match_group(param, %r{(/usr/bin/)?(gcc|llvm-gcc|clang)\s?}) - # problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[3]}\"" - # elsif match = regex_match_group(param, %r{(/usr/bin/)?((g|llvm-g|clang)\+\+)\s?}) - # problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[3]}\"" - # end - # end - # + + find_instance_method_call(body_node, "ENV", :[]=) do |m| + param = parameters(m)[1] + if match = regex_match_group(param, %r{(/usr/bin/)?(gcc|llvm-gcc|clang)\s?}) + problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\"" + elsif match = regex_match_group(param, %r{(/usr/bin/)?((g|llvm-g|clang)\+\+)\s?}) + problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\"" + end + end + # # Prefer formula path shortcuts in strings # formula_path_strings(body_node, :prefix) do |p| # next unless match = regex_match_group(p, %r{(/(man))[/'"]}) diff --git a/Library/Homebrew/test/rubocops/lines_cop_spec.rb b/Library/Homebrew/test/rubocops/lines_cop_spec.rb index 7e901f0e21..afe65e9aca 100644 --- a/Library/Homebrew/test/rubocops/lines_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_cop_spec.rb @@ -817,7 +817,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do class Foo < Formula desc "foo" url 'http://example.com/foo-1.0.tgz' - def test + def install verbose = ARGV.verbose? end end @@ -841,7 +841,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do class Foo < Formula desc "foo" url 'http://example.com/foo-1.0.tgz' - def test + def install man1.install man+"man8" => "faad.1" end end @@ -865,7 +865,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do class Foo < Formula desc "foo" url 'http://example.com/foo-1.0.tgz' - def test + def install system "/usr/bin/gcc", "foo" end end @@ -889,7 +889,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do class Foo < Formula desc "foo" url 'http://example.com/foo-1.0.tgz' - def test + def install system "/usr/bin/g++", "-o", "foo", "foo.cc" end end @@ -907,6 +907,54 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do expect_offense(expected, actual) end end + + it "with hardcoded compiler 3 " do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def install + ENV["COMPILER_PATH"] = "/usr/bin/llvm-g++" + end + end + EOS + + expected_offenses = [{ message: "Use \"\#{ENV.cxx}\" instead of hard-coding \"llvm-g++\"", + severity: :convention, + line: 5, + column: 28, + source: source }] + + inspect_source(cop, source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + + it "with hardcoded compiler 4 " do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def install + ENV["COMPILER_PATH"] = "/usr/bin/gcc" + end + end + EOS + + expected_offenses = [{ message: "Use \"\#{ENV.cc}\" instead of hard-coding \"gcc\"", + severity: :convention, + line: 5, + column: 28, + source: source }] + + inspect_source(cop, source) + + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end end def expect_offense(expected, actual) expect(actual.message).to eq(expected[:message])