Skip to content

Commit 38c342e

Browse files
committed
Merge pull request #20 from codeclimate/only-include-ruby-files
Removes non-Ruby files from include_path before passing to rubocop.
2 parents b38ad71 + c6485fe commit 38c342e

File tree

3 files changed

+96
-17
lines changed

3 files changed

+96
-17
lines changed

lib/cc/engine/file_list_resolver.rb

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module CC
2+
module Engine
3+
class FileListResolver
4+
def initialize(code:, engine_config: {}, rubocop_config_store:)
5+
@code = code
6+
@exclude_paths = engine_config["exclude_paths"] || []
7+
@include_paths = engine_config["include_paths"]
8+
@rubocop_config_store = rubocop_config_store
9+
end
10+
11+
def expanded_list
12+
if @include_paths
13+
include_based_files_to_inspect
14+
else
15+
exclude_based_files_to_inspect
16+
end
17+
end
18+
19+
private
20+
21+
def exclude_based_files_to_inspect
22+
rubocop_runner.send(:find_target_files, []).reject do |path|
23+
exclude_due_to_config?(path)
24+
end
25+
end
26+
27+
def exclude_due_to_config?(path)
28+
@exclude_paths.include?(local_path(path))
29+
end
30+
31+
def include_based_files_to_inspect
32+
@include_paths.map do |path|
33+
if path =~ %r{/$}
34+
rubocop_runner.send(:find_target_files, [path])
35+
elsif rubocop_file_to_include?(path)
36+
path
37+
end
38+
end.flatten.compact
39+
end
40+
41+
def local_path(path)
42+
realpath = Pathname.new(@code).realpath.to_s
43+
path.gsub(%r|^#{realpath}/|, '')
44+
end
45+
46+
def rubocop_file_to_include?(file)
47+
if file =~ /\.rb$/
48+
true
49+
else
50+
dir, basename = File.split(file)
51+
@rubocop_config_store.for(dir).file_to_include?(basename)
52+
end
53+
end
54+
55+
def rubocop_runner
56+
@rubocop_runner ||= RuboCop::Runner.new({}, @rubocop_config_store)
57+
end
58+
end
59+
end
60+
end

lib/cc/engine/rubocop.rb

+13-17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "rubocop"
44
require "rubocop/cop/method_complexity_patch"
55
require "cc/engine/category_parser"
6+
require "cc/engine/file_list_resolver"
67
require "active_support"
78
require "active_support/core_ext"
89

@@ -26,25 +27,18 @@ def run
2627
private
2728

2829
def files_to_inspect
29-
runner = RuboCop::Runner.new({}, rubocop_config_store)
30-
if @engine_config["include_paths"]
31-
runner.send(:find_target_files, @engine_config["include_paths"])
32-
else
33-
runner.send(:find_target_files, []).reject do |path|
34-
exclude_due_to_config?(path)
35-
end
36-
end
30+
@file_list_resolver = FileListResolver.new(
31+
code: @code,
32+
engine_config: @engine_config,
33+
rubocop_config_store: rubocop_config_store
34+
)
35+
@file_list_resolver.expanded_list
3736
end
3837

3938
def category(cop_name)
4039
CategoryParser.new(cop_name).category
4140
end
4241

43-
def exclude_due_to_config?(path)
44-
exclusions = @engine_config["exclude_paths"] || []
45-
exclusions.include?(local_path(path))
46-
end
47-
4842
def inspect_file(path)
4943
parsed = RuboCop::ProcessedSource.from_file(path)
5044
rubocop_team_for_path(path).inspect_file(parsed).each do |violation|
@@ -60,11 +54,13 @@ def local_path(path)
6054

6155
def rubocop_config_store
6256
@rubocop_config_store ||= begin
63-
config_store = RuboCop::ConfigStore.new
64-
if (config_file = @engine_config["config"])
65-
config_store.options_config = config_file
57+
Dir.chdir(@code) do
58+
config_store = RuboCop::ConfigStore.new
59+
if (config_file = @engine_config["config"])
60+
config_store.options_config = config_file
61+
end
62+
config_store
6663
end
67-
config_store
6864
end
6965
end
7066

spec/cc/engine/rubocop_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,29 @@ def method
239239
assert !includes_check?(output, "Style/AndOr")
240240
end
241241

242+
it "ignores non-Ruby files even when passed in as include_paths" do
243+
config_yml = "foo:\n bar: \"baz\""
244+
create_source_file("config.yml", config_yml)
245+
output = run_engine(
246+
"include_paths" => %w[config.yml]
247+
)
248+
refute (issues(output).detect do |i|
249+
i["description"] == "unexpected token tCOLON"
250+
end)
251+
end
252+
253+
it "includes Ruby files even if they don't end with .rb" do
254+
create_source_file("Rakefile", <<-EORUBY)
255+
def method
256+
unused = "x"
257+
258+
return false
259+
end
260+
EORUBY
261+
output = run_engine("include_paths" => %w(Rakefile))
262+
assert includes_check?(output, "Lint/UselessAssignment")
263+
end
264+
242265
def includes_check?(output, cop_name)
243266
!!issues(output).detect { |i| i["check_name"] =~ /#{cop_name}$/ }
244267
end

0 commit comments

Comments
 (0)