From 997d24f1501e5f98ca57081feb063a59e5464eee Mon Sep 17 00:00:00 2001 From: Will Fleming Date: Tue, 16 Feb 2016 16:46:19 -0500 Subject: [PATCH] Resolve all paths to absolute paths When paths were sent in as relative paths from the CLI, `Exclude` directives within a rubocop config yml were not correctly applied. This is because Rubocop internally turns those excludes into absolute paths, and it seems like it was then trying to match the being-analyzed relative path against the had-been-made-absolute excluded path. --- lib/cc/engine/file_list_resolver.rb | 10 +++++++--- spec/cc/engine/rubocop_spec.rb | 31 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/lib/cc/engine/file_list_resolver.rb b/lib/cc/engine/file_list_resolver.rb index 2db1771a..6bf53bc8 100644 --- a/lib/cc/engine/file_list_resolver.rb +++ b/lib/cc/engine/file_list_resolver.rb @@ -29,13 +29,13 @@ def exclude_due_to_config?(path) end def include_based_files_to_inspect - @include_paths.map do |path| - if path =~ %r{/$} + absolute_include_paths.flat_map do |path| + if Dir.exist?(path) rubocop_runner.send(:find_target_files, [path]) elsif rubocop_file_to_include?(path) path end - end.flatten.compact + end.compact end def local_path(path) @@ -43,6 +43,10 @@ def local_path(path) path.gsub(%r{^#{realpath}/}, '') end + def absolute_include_paths + @include_paths.map { |path| Pathname.new(path).realpath.to_s } + end + def rubocop_file_to_include?(file) if file =~ /\.rb$/ true diff --git a/spec/cc/engine/rubocop_spec.rb b/spec/cc/engine/rubocop_spec.rb index 7965f895..d07bf023 100644 --- a/spec/cc/engine/rubocop_spec.rb +++ b/spec/cc/engine/rubocop_spec.rb @@ -62,6 +62,37 @@ def method expect(includes_check?(output, "Lint/UselessAssignment")).to be false end + it "respects excludes in an inherit_from directive" do + create_source_file("foo.rb", <<-EORUBY) + def method + unused = "x" + return false + end + EORUBY + create_source_file("bar.rb", <<-EORUBY) + def method + unused = 42 + return true + end + EORUBY + + create_source_file( + ".rubocop.yml", + "inherit_from: .rubocop_todo.yml\nAllCops:\n DisabledByDefault: true\nLint/UselessAssignment:\n Enabled: true\n" + ) + create_source_file( + ".rubocop_todo.yml", + "Lint/UselessAssignment:\n Exclude:\n - bar.rb\n" + ) + + output = run_engine("include_paths" => ["foo.rb", "bar.rb"]) + issues = output.split("\0").map { |istr| JSON.parse(istr) } + lint_issues = issues.select { |issue| issue["check_name"] == "Rubocop/Lint/UselessAssignment" } + + expect(lint_issues.detect { |i| i["location"]["path"] == "foo.rb" }).to be_present + expect(lint_issues.detect { |i| i["location"]["path"] == "bar.rb" }).to be_nil + end + it "reads a file with a #!.*ruby declaration at the top" do create_source_file("my_script", <<-EORUBY) #!/usr/bin/env ruby