Skip to content

Commit 9f1e178

Browse files
committed
Merge pull request #18 from codeclimate/handle-includes-or-excludes
Support both include_paths and exclude_paths in config.json.
2 parents 85f348f + d5f4b20 commit 9f1e178

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

lib/cc/engine/rubocop.rb

+28-14
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,45 @@ def initialize(code, engine_config, io)
1717

1818
def run
1919
Dir.chdir(@code) do
20-
runner = RuboCop::Runner.new({}, rubocop_config_store)
21-
paths = runner.send :find_target_files, []
22-
paths.each do |path|
23-
realpath = Pathname.new(@code).realpath.to_s
24-
local_path = path.gsub(%r|^#{realpath}/|, '')
25-
unless exclude?(local_path)
26-
parsed = RuboCop::ProcessedSource.from_file(path)
27-
rubocop_team_for_path(path).inspect_file(parsed).each do |violation|
28-
json = violation_json(violation, local_path)
29-
@io.print "#{json}\0"
30-
end
31-
end
20+
files_to_inspect.each do |path|
21+
inspect_file(path)
3222
end
3323
end
3424
end
3525

3626
private
3727

28+
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
37+
end
38+
3839
def category(cop_name)
3940
CategoryParser.new(cop_name).category
4041
end
4142

42-
def exclude?(local_path)
43+
def exclude_due_to_config?(path)
4344
exclusions = @engine_config["exclude_paths"] || []
44-
exclusions.include?(local_path)
45+
exclusions.include?(local_path(path))
46+
end
47+
48+
def inspect_file(path)
49+
parsed = RuboCop::ProcessedSource.from_file(path)
50+
rubocop_team_for_path(path).inspect_file(parsed).each do |violation|
51+
json = violation_json(violation, local_path(path))
52+
@io.print "#{json}\0"
53+
end
54+
end
55+
56+
def local_path(path)
57+
realpath = Pathname.new(@code).realpath.to_s
58+
path.gsub(%r|^#{realpath}/|, '')
4559
end
4660

4761
def rubocop_config_store

spec/cc/engine/rubocop_spec.rb

+33-2
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,35 @@ def method(a,b,c,d,e,f,g)
210210
assert includes_content_for?(output, "Metrics/ClassLength")
211211
end
212212

213+
it "uses only include_paths when they're passed in via the config hash" do
214+
okay_contents = <<-EORUBY
215+
#!/usr/bin/env ruby
216+
217+
puts "Hello world"
218+
EORUBY
219+
create_source_file("included_root_file.rb", okay_contents)
220+
create_source_file("subdir/subdir_file.rb", okay_contents)
221+
create_source_file("ignored_root_file.rb", <<-EORUBY)
222+
def method
223+
unused = "x" and "y"
224+
225+
return false
226+
end
227+
EORUBY
228+
create_source_file("ignored_subdir/subdir_file.rb", <<-EORUBY)
229+
def method
230+
unused = "x"
231+
232+
return false
233+
end
234+
EORUBY
235+
output = run_engine(
236+
"include_paths" => %w(included_root_file.rb subdir/)
237+
)
238+
assert !includes_check?(output, "Lint/UselessAssignment")
239+
assert !includes_check?(output, "Style/AndOr")
240+
end
241+
213242
def includes_check?(output, cop_name)
214243
!!issues(output).detect { |i| i["check_name"] =~ /#{cop_name}$/ }
215244
end
@@ -221,11 +250,13 @@ def includes_content_for?(output, cop_name)
221250
end
222251

223252
def issues(output)
224-
issues = output.split("\0").map { |x| JSON.parse(x) }
253+
output.split("\0").map { |x| JSON.parse(x) }
225254
end
226255

227256
def create_source_file(path, content)
228-
File.write(File.join(@code, path), content)
257+
abs_path = File.join(@code, path)
258+
FileUtils.mkdir_p(File.dirname(abs_path))
259+
File.write(abs_path, content)
229260
end
230261

231262
def run_engine(config = nil)

0 commit comments

Comments
 (0)