From 7dfe09ccae5b1a8309326e5a5cb3172fbcd795d3 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Mon, 14 Aug 2017 01:09:06 +0530 Subject: [PATCH] Add tests for fileUtils call in system --- Library/Homebrew/rubocops/lines_cop.rb | 15 ++++++------- .../Homebrew/test/rubocops/lines_cop_spec.rb | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/rubocops/lines_cop.rb b/Library/Homebrew/rubocops/lines_cop.rb index 7a0e08ba29..1b7a1935e3 100644 --- a/Library/Homebrew/rubocops/lines_cop.rb +++ b/Library/Homebrew/rubocops/lines_cop.rb @@ -263,13 +263,14 @@ module RuboCop problem "Dir([\"#{string_content(path)}\"]) is unnecessary; just use \"#{match[0]}\"" end - # - # fileUtils_methods= FileUtils.singleton_methods(false).map { |m| Regexp.escape(m) }.join "|" - # find_method_with_args(body_node, :system, /fileUtils_methods/) do |m| - # method = string_content(@offensive_node) - # problem "Use the `#{method}` Ruby method instead of `#{m.source}`" - # end - # + + fileUtils_methods= Regexp.new(FileUtils.singleton_methods(false).map { |m| Regexp.escape(m) }.join "|") + find_every_method_call_by_name(body_node, :system).each do |m| + param = parameters(m).first + next unless match = regex_match_group(param, fileUtils_methods) + problem "Use the `#{match}` Ruby method instead of `#{m.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 diff --git a/Library/Homebrew/test/rubocops/lines_cop_spec.rb b/Library/Homebrew/test/rubocops/lines_cop_spec.rb index 203f8a7d97..1af5d2e727 100644 --- a/Library/Homebrew/test/rubocops/lines_cop_spec.rb +++ b/Library/Homebrew/test/rubocops/lines_cop_spec.rb @@ -571,6 +571,27 @@ describe RuboCop::Cop::FormulaAudit::Miscellaneous do inspect_source(cop, source) + expected_offenses.zip(cop.offenses).each do |expected, actual| + expect_offense(expected, actual) + end + end + it "with system call to fileUtils Method" do + source = <<-EOS.undent + class Foo < Formula + desc "foo" + url 'http://example.com/foo-1.0.tgz' + system "mkdir", "foo" + end + EOS + + expected_offenses = [{ message: "Use the `mkdir` Ruby method instead of `system \"mkdir\", \"foo\"`", + severity: :convention, + line: 4, + column: 10, + source: source }] + + inspect_source(cop, source) + expected_offenses.zip(cop.offenses).each do |expected, actual| expect_offense(expected, actual) end