add tests for hardcoded compilers in ENV

This commit is contained in:
Gautham Goli 2017-08-14 20:10:45 +05:30
parent 77468fdae3
commit 65ae6bacd8
2 changed files with 62 additions and 14 deletions

View File

@ -108,16 +108,16 @@ module RuboCop
problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\"" problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\""
end end
end end
#
# find_instance_method_call(body_node, :ENV, :[]=) do |m| find_instance_method_call(body_node, "ENV", :[]=) do |m|
# param = parameters(m)[1] param = parameters(m)[1]
# if match = regex_match_group(param, %r{(/usr/bin/)?(gcc|llvm-gcc|clang)\s?}) if match = regex_match_group(param, %r{(/usr/bin/)?(gcc|llvm-gcc|clang)\s?})
# problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[3]}\"" problem "Use \"\#{ENV.cc}\" instead of hard-coding \"#{match[2]}\""
# elsif match = regex_match_group(param, %r{(/usr/bin/)?((g|llvm-g|clang)\+\+)\s?}) elsif match = regex_match_group(param, %r{(/usr/bin/)?((g|llvm-g|clang)\+\+)\s?})
# problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[3]}\"" problem "Use \"\#{ENV.cxx}\" instead of hard-coding \"#{match[2]}\""
# end end
# end end
#
# # Prefer formula path shortcuts in strings # # Prefer formula path shortcuts in strings
# formula_path_strings(body_node, :prefix) do |p| # formula_path_strings(body_node, :prefix) do |p|
# next unless match = regex_match_group(p, %r{(/(man))[/'"]}) # next unless match = regex_match_group(p, %r{(/(man))[/'"]})

View File

@ -817,7 +817,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
class Foo < Formula class Foo < Formula
desc "foo" desc "foo"
url 'http://example.com/foo-1.0.tgz' url 'http://example.com/foo-1.0.tgz'
def test def install
verbose = ARGV.verbose? verbose = ARGV.verbose?
end end
end end
@ -841,7 +841,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
class Foo < Formula class Foo < Formula
desc "foo" desc "foo"
url 'http://example.com/foo-1.0.tgz' url 'http://example.com/foo-1.0.tgz'
def test def install
man1.install man+"man8" => "faad.1" man1.install man+"man8" => "faad.1"
end end
end end
@ -865,7 +865,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
class Foo < Formula class Foo < Formula
desc "foo" desc "foo"
url 'http://example.com/foo-1.0.tgz' url 'http://example.com/foo-1.0.tgz'
def test def install
system "/usr/bin/gcc", "foo" system "/usr/bin/gcc", "foo"
end end
end end
@ -889,7 +889,7 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
class Foo < Formula class Foo < Formula
desc "foo" desc "foo"
url 'http://example.com/foo-1.0.tgz' url 'http://example.com/foo-1.0.tgz'
def test def install
system "/usr/bin/g++", "-o", "foo", "foo.cc" system "/usr/bin/g++", "-o", "foo", "foo.cc"
end end
end end
@ -907,6 +907,54 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do
expect_offense(expected, actual) expect_offense(expected, actual)
end end
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 end
def expect_offense(expected, actual) def expect_offense(expected, actual)
expect(actual.message).to eq(expected[:message]) expect(actual.message).to eq(expected[:message])