From 77468fdae36ee58580a643d8bc0fdd9f8b6e61c3 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Mon, 14 Aug 2017 19:58:39 +0530 Subject: add tests for hard coded compilers in system calls --- Library/Homebrew/rubocops/lines_cop.rb | 18 ++++----- Library/Homebrew/test/rubocops/lines_cop_spec.rb | 48 ++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 9 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb index cf1ac68c3..4d2d63cce 100644 --- a/Library/Homebrew/rubocops/lines_cop.rb +++ b/Library/Homebrew/rubocops/lines_cop.rb @@ -99,15 +99,15 @@ module RuboCop problem "\"#{m.source}\" should be \"#{match[0]}\"" end - # # Avoid hard-coding compilers - # find_every_method_call_by_name(body_node, :system).each do |m| - # param = parameters(m).first - # 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 + # Avoid hard-coding compilers + find_every_method_call_by_name(body_node, :system).each do |m| + param = parameters(m).first + 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 # # find_instance_method_call(body_node, :ENV, :[]=) do |m| # param = parameters(m)[1] diff --git a/Library/Homebrew/test/rubocops/lines_cop_spec.rb b/Library/Homebrew/test/rubocops/lines_cop_spec.rb index c0cd754b3..7e901f0e2 100644 --- a/Library/Homebrew/test/rubocops/lines_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_cop_spec.rb @@ -859,6 +859,54 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do expect_offense(expected, actual) end end + + it "with hardcoded compiler 1 " do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def test + system "/usr/bin/gcc", "foo" + end + end + EOS + + expected_offenses = [{ message: "Use \"\#{ENV.cc}\" instead of hard-coding \"gcc\"", + severity: :convention, + line: 5, + column: 12, + 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 2 " do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + def test + system "/usr/bin/g++", "-o", "foo", "foo.cc" + end + end + EOS + + expected_offenses = [{ message: "Use \"\#{ENV.cxx}\" instead of hard-coding \"g++\"", + severity: :convention, + line: 5, + column: 12, + 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]) -- cgit v1.2.3