Skip to content

Commit 3da3102

Browse files
author
Ashley Baldwin-Hunter
committed
Merge pull request qltysh#113 from codeclimate/abh-path
Let users specify engines and paths as options to analyze command for easier debugging
2 parents c88c949 + c01f5ee commit 3da3102

File tree

6 files changed

+160
-13
lines changed

6 files changed

+160
-13
lines changed

lib/cc/analyzer/engines_runner.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ class EnginesRunner
66
InvalidEngineName = Class.new(StandardError)
77
NoEnabledEngines = Class.new(StandardError)
88

9-
def initialize(registry, formatter, source_dir, config, container_label = nil)
9+
def initialize(registry, formatter, source_dir, config, requested_paths = [], container_label = nil)
1010
@registry = registry
1111
@formatter = formatter
1212
@source_dir = source_dir
1313
@config = config
14+
@requested_paths = requested_paths
1415
@container_label = container_label
1516
end
1617

@@ -28,6 +29,8 @@ def run(container_listener = ContainerListener.new)
2829

2930
private
3031

32+
attr_reader :requested_paths
33+
3134
def run_engine(engine, container_listener)
3235
@formatter.engine_running(engine) do
3336
engine.run(@formatter, container_listener)
@@ -72,12 +75,12 @@ def metadata(engine_name)
7275
@registry[engine_name]
7376
end
7477

75-
def exclude_paths
76-
PathPatterns.new(@config.exclude_paths || []).expanded + gitignore_paths
78+
def include_paths
79+
IncludePathsBuilder.new(@config.exclude_paths || [], requested_paths).build
7780
end
7881

79-
def include_paths
80-
IncludePathsBuilder.new(@config.exclude_paths || []).build
82+
def exclude_paths
83+
PathPatterns.new(@config.exclude_paths || []).expanded + gitignore_paths
8184
end
8285

8386
def gitignore_paths

lib/cc/analyzer/include_paths_builder.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,41 @@ def self.relevant_entries(path)
99
end
1010
end
1111

12-
def initialize(cc_exclude_paths)
12+
attr_reader :cc_include_paths
13+
14+
def initialize(cc_exclude_paths, cc_include_paths = [])
1315
@cc_exclude_paths = cc_exclude_paths
16+
@cc_include_paths = cc_include_paths
1417
end
1518

1619
def build
17-
root = Directory.new('.', ignored_files)
18-
paths = root.included_paths
20+
if cc_include_paths.any?
21+
paths = filter_by_cc_includes
22+
else
23+
root = Directory.new(".", ignored_files)
24+
paths = root.included_paths
25+
end
26+
1927
paths.each do |path|
2028
raise_on_unreadable_files(path)
2129
end
2230
end
2331

2432
protected
2533

34+
def filter_by_cc_includes
35+
paths = []
36+
cc_include_paths.each do |path|
37+
if File.directory?(path)
38+
root = Directory.new(path, ignored_files)
39+
paths += root.included_paths
40+
elsif !ignored_files.include?(path)
41+
paths << path
42+
end
43+
end
44+
paths.uniq
45+
end
46+
2647
def ignored_files
2748
Tempfile.open(".cc_gitignore") do |tmp|
2849
tmp.write(File.read(".gitignore")) if File.exist?(".gitignore")

lib/cc/cli/analyze.rb

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ class Analyze < Command
55

66
def initialize(_args = [])
77
super
8+
@engine_options = []
9+
@path_options = []
810

911
process_args
12+
apply_config_options
1013
end
1114

1215
def run
1316
require_codeclimate_yml
1417

1518
Dir.chdir(ENV['FILESYSTEM_DIR']) do
16-
runner = EnginesRunner.new(registry, formatter, source_dir, config)
19+
runner = EnginesRunner.new(registry, formatter, source_dir, config, path_options)
1720
runner.run
1821
end
1922

@@ -25,13 +28,20 @@ def run
2528

2629
private
2730

31+
attr_accessor :config
32+
attr_reader :engine_options, :path_options
33+
2834
def process_args
2935
while arg = @args.shift
3036
case arg
3137
when '-f'
3238
@formatter = Formatters.resolve(@args.shift)
39+
when '-e', '--engine'
40+
@engine_options << @args.shift
3341
when '--dev'
3442
@dev_mode = true
43+
else
44+
@path_options << arg
3545
end
3646
end
3747
rescue Formatters::Formatter::InvalidFormatterError => e
@@ -51,7 +61,34 @@ def source_dir
5161
end
5262

5363
def config
54-
CC::Yaml.parse(filesystem.read_path(CODECLIMATE_YAML))
64+
@config ||= CC::Yaml.parse(filesystem.read_path(CODECLIMATE_YAML))
65+
end
66+
67+
def apply_config_options
68+
if engine_options.any? && config.engines?
69+
filter_by_engine_options
70+
elsif engine_options.any?
71+
config["engines"] = CC::Yaml::Nodes::EngineList.new(config).with_value({})
72+
end
73+
add_engine_options
74+
end
75+
76+
def filter_by_engine_options
77+
config.engines.keys.each do |engine|
78+
unless engine_options.include?(engine)
79+
config.engines.delete(engine)
80+
end
81+
end
82+
end
83+
84+
def add_engine_options
85+
engine_options.each do |engine|
86+
if config.engines.include?(engine)
87+
config.engines[engine].enabled = true
88+
else
89+
config.engines[engine] = CC::Yaml::Nodes::Engine.new(config.engines).with_value({ "enabled" => true })
90+
end
91+
end
5592
end
5693
end
5794
end

spec/cc/analyzer/engines_runner_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ module CC::Analyzer
9595
end
9696

9797
it "gets include_paths from IncludePathBuilder" do
98-
IncludePathsBuilder.expects(:new).with([]).returns(mock(build: ['.']))
98+
IncludePathsBuilder.expects(:new).with([], []).returns(mock(build: ['.']))
9999
expected_config = {
100100
"enabled" => true,
101101
:exclude_paths => [],

spec/cc/analyzer/include_paths_builder_spec.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ module CC::Analyzer
88
within_temp_dir { test.call }
99
end
1010

11-
let(:builder) { CC::Analyzer::IncludePathsBuilder.new(cc_excludes) }
11+
let(:builder) { CC::Analyzer::IncludePathsBuilder.new(cc_excludes, cc_includes) }
1212
let(:cc_excludes) { [] }
13+
let(:cc_includes) { [] }
1314
let(:result) { builder.build }
1415

1516
before do
@@ -221,5 +222,26 @@ module CC::Analyzer
221222
result.include?("subdir/subdir/").must_equal(false)
222223
end
223224
end
225+
226+
describe "when cc_include_paths are passed in addition to excludes" do
227+
let(:cc_excludes) { ["untrackable.rb"] }
228+
let(:cc_includes) { ["untrackable.rb", "subdir"] }
229+
230+
before do
231+
make_file("untrackable.rb")
232+
make_file("trackable.rb")
233+
make_file("subdir/subdir_trackable.rb")
234+
make_file("subdir/foo.rb")
235+
make_file("subdir/baz.rb")
236+
end
237+
238+
it "includes requested paths" do
239+
result.include?("subdir/").must_equal(true)
240+
end
241+
242+
it "omits requested paths that are excluded by .codeclimate.yml" do
243+
result.include?("untrackable.rb").must_equal(false)
244+
end
245+
end
224246
end
225247
end

spec/cc/cli/analyze_spec.rb

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,77 @@ module CC::CLI
3030
EOYAML
3131

3232
_, stderr = capture_io do
33-
lambda { Analyze.new.run }
33+
Analyze.new.run
3434
end
3535

3636
stderr.must_match("")
3737
end
3838
end
3939
end
40+
41+
describe "when user passes engine options to command" do
42+
it "uses only the engines provided" do
43+
within_temp_dir do
44+
create_yaml(<<-EOYAML)
45+
engines:
46+
rubocop:
47+
enabled: true
48+
EOYAML
49+
50+
args = ["-e", "eslint"]
51+
52+
analyze = Analyze.new(args)
53+
qualified_config = analyze.send(:config)
54+
55+
qualified_config.engines.must_equal("eslint" => { "enabled" => true })
56+
end
57+
end
58+
end
59+
60+
describe "when user passes path args to command" do
61+
it "captures the paths provided as path_options" do
62+
within_temp_dir do
63+
create_yaml(<<-EOYAML)
64+
engines:
65+
rubocop:
66+
enabled: true
67+
eslint:
68+
enabled: true
69+
EOYAML
70+
71+
args = ["-e", "eslint", "foo.rb"]
72+
paths = ["foo.rb"]
73+
74+
analyze = Analyze.new(args)
75+
76+
analyze.send(:path_options).must_equal(paths)
77+
end
78+
end
79+
end
80+
81+
describe "when user passes path args to command" do
82+
it "passes the paths provided" do
83+
within_temp_dir do
84+
create_yaml(<<-EOYAML)
85+
engines:
86+
rubocop:
87+
enabled: true
88+
eslint:
89+
enabled: true
90+
EOYAML
91+
92+
args = ["-e", "eslint", "foo.rb"]
93+
paths = ["foo.rb"]
94+
95+
analyze = Analyze.new(args)
96+
engines_runner = stub(run: "peace")
97+
98+
CC::Analyzer::EnginesRunner.expects(:new).with(anything, anything, anything, anything, paths).returns(engines_runner)
99+
100+
analyze.run
101+
end
102+
end
103+
end
40104
end
41105
end
42106

0 commit comments

Comments
 (0)