Skip to content

Commit f95e11f

Browse files
committed
Merge pull request #4 from codeclimate/gd-config
Read config from json file
2 parents b4690ea + 627ba7d commit f95e11f

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

bin/codeclimate-rubocop

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "../lib"))
33

44
require 'cc/engine/rubocop'
55

6-
CC::Engine::Rubocop.new("/code", ENV["ENGINE_CONFIG"], STDOUT).run
6+
if File.exists?("/config.json")
7+
engine_config = JSON.parse(File.read("/config.json"))
8+
else
9+
engine_config = {}
10+
end
11+
12+
CC::Engine::Rubocop.new("/code", engine_config, STDOUT).run

lib/cc/engine/rubocop.rb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
module CC
77
module Engine
88
class Rubocop
9-
def initialize(code, config, io)
9+
def initialize(code, engine_config, io)
1010
@code = code
11-
@config = config
11+
@engine_config = engine_config || {}
1212
@io = io
1313
end
1414

@@ -36,23 +36,15 @@ def category(cop_name)
3636
CategoryParser.new(cop_name).category
3737
end
3838

39-
def engine_config
40-
if @config
41-
@engine_config ||= JSON.parse(@config)
42-
else
43-
{}
44-
end
45-
end
46-
4739
def exclude?(local_path)
48-
exclusions = engine_config["exclude_paths"] || []
40+
exclusions = @engine_config["exclude_paths"] || []
4941
exclusions.include?(local_path)
5042
end
5143

5244
def rubocop_config_store
5345
@rubocop_config_store ||= begin
5446
config_store = RuboCop::ConfigStore.new
55-
if (config_file = engine_config["config"])
47+
if (config_file = @engine_config["config"])
5648
config_store.options_config = config_file
5749
end
5850
config_store

spec/cc/engine/rubocop_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def method
2626
def method
2727
unused = "x" and "y"
2828
29-
return false
29+
return false
3030
end
3131
EORUBY
3232

@@ -35,7 +35,7 @@ def method
3535
"Lint/UselessAssignment:\n Enabled: false\n"
3636
)
3737

38-
config = { config: "rubocop.yml" }.to_json
38+
config = { "config" => "rubocop.yml" }
3939
output = run_engine(config)
4040

4141
assert includes_check?(output, "Style/AndOr")
@@ -70,7 +70,7 @@ def method
7070
"rubocop.yml",
7171
"AllCops:\n Exclude:\n - \"my_script\"\n"
7272
)
73-
config = { config: "rubocop.yml" }.to_json
73+
config = { "config" => "rubocop.yml" }
7474
output = run_engine(config)
7575
assert !includes_check?(output, "Lint/UselessAssignment")
7676
end
@@ -85,7 +85,7 @@ def method
8585
return false
8686
end
8787
EORUBY
88-
config = {exclude_paths: ['my_script']}.to_json
88+
config = { "exclude_paths" => ["my_script"] }
8989
output = run_engine(config)
9090
assert !includes_check?(output, "Lint/UselessAssignment")
9191
end
@@ -107,14 +107,14 @@ def method
107107
"rubocop.yml",
108108
"AllCops:\n Exclude:\n - \"foo.rb\"\n"
109109
)
110-
config = {config: 'rubocop.yml', exclude_paths: ['bar.rb']}.to_json
110+
config = { "config" => "rubocop.yml", "exclude_paths" => ["bar.rb"] }
111111
output = run_engine(config)
112112
assert !includes_check?(output, "Lint/UselessAssignment")
113113
end
114114

115115
it "handles different locations properly" do
116116
RuboCop::Cop::Team.any_instance.expects(:inspect_file).returns([OpenStruct.new(
117-
location: RuboCop::Cop::Lint::Syntax::PseudoSourceRange.new(1, 0, ''),
117+
location: RuboCop::Cop::Lint::Syntax::PseudoSourceRange.new(1, 0, ""),
118118
cop_name: "fake",
119119
message: "message"
120120
)])
@@ -151,9 +151,9 @@ def create_source_file(path, content)
151151
File.write(File.join(@code, path), content)
152152
end
153153

154-
def run_engine(config_json = nil)
154+
def run_engine(config = nil)
155155
io = StringIO.new
156-
rubocop = Rubocop.new(@code, config_json, io)
156+
rubocop = Rubocop.new(@code, config, io)
157157
rubocop.run
158158

159159
io.string

0 commit comments

Comments
 (0)