Skip to content

Commit a6046a2

Browse files
committed
Add default_config key with nested values
This adds the `default_config` option which is used to add default configuration options to an engine. This also marks the duplication engine as a default engine when ruby files are present.
1 parent dcac893 commit a6046a2

File tree

7 files changed

+123
-35
lines changed

7 files changed

+123
-35
lines changed

.codeclimate.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
engines:
33
rubocop:
44
enabled: true
5-
bundler-audit:
5+
duplication:
66
enabled: true
7+
config:
8+
languages:
9+
- ruby
710
ratings:
811
paths:
912
- "**.rb"
10-
exclude_paths:
11-
- config/**/*
12-
- spec/**/*
13+
exclude_paths: []

Gemfile.lock

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
codeclimate (0.6.4)
4+
codeclimate (0.6.3)
55
activesupport (~> 4.2, >= 4.2.1)
66
codeclimate-yaml (~> 0.2.3)
77
faraday (~> 0.9.1)
@@ -49,7 +49,7 @@ GEM
4949
metaclass (~> 0.0.1)
5050
multipart-post (2.0.0)
5151
posix-spawn (0.3.11)
52-
pry (0.10.1)
52+
pry (0.10.2)
5353
coderay (~> 1.1.0)
5454
method_source (~> 0.8.1)
5555
slop (~> 3.4)
@@ -76,6 +76,3 @@ DEPENDENCIES
7676
minitest-reporters
7777
mocha
7878
rack-test
79-
80-
BUNDLED WITH
81-
1.10.6

config/engines.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ rubocop:
1717
- \.rb$
1818
default_ratings_paths:
1919
- "**.rb"
20+
duplication:
21+
image: codeclimate/codeclimate-duplication
22+
description: Find similar structures in your code
23+
community: false
24+
default_config:
25+
languages:
26+
- ruby
27+
enable_regexps:
28+
- \.rb$
29+
default_ratings_paths:
30+
- "**.rb"
2031
gofmt:
2132
image: codeclimate/codeclimate-gofmt
2233
description: gofmt

lib/cc/cli/config.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module CC
2+
module CLI
3+
class Config
4+
delegate :to_yaml, to: :config
5+
def initialize
6+
@config = {
7+
"engines" => {},
8+
"ratings" => { "paths" => [] }
9+
}
10+
end
11+
12+
def add_engine(engine_name, engine_config)
13+
config["engines"][engine_name] = { "enabled" => true }
14+
15+
if engine_config["default_config"].present?
16+
config["engines"][engine_name]["config"] = engine_config["default_config"]
17+
end
18+
19+
config["ratings"]["paths"] |= engine_config["default_ratings_paths"]
20+
end
21+
22+
def add_exclude_paths(paths)
23+
config["exclude_paths"] ||= []
24+
config["exclude_paths"] += paths.map { |path| "#{path}/**/*" }
25+
end
26+
27+
private
28+
29+
attr_reader :config
30+
end
31+
end
32+
end

lib/cc/cli/init.rb

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'cc/cli/config'
2+
13
module CC
24
module CLI
35
class Init < Command
@@ -18,19 +20,13 @@ def run
1820
private
1921

2022
def create_codeclimate_yaml
21-
config = { "engines" => {} }
22-
eligible_engines.each do |engine_name, engine_config|
23-
config["engines"][engine_name] = {
24-
"enabled" => true
25-
}
26-
config["ratings"] ||= {}
27-
config["ratings"]["paths"] ||= []
23+
config = CC::CLI::Config.new
2824

29-
config["ratings"]["paths"] |= engine_config["default_ratings_paths"]
25+
eligible_engines.each do |(engine_name, engine_config)|
26+
config.add_engine(engine_name, engine_config)
3027
end
3128

32-
config["exclude_paths"] = exclude_paths(AUTO_EXCLUDE_PATHS)
33-
29+
config.add_exclude_paths(auto_exclude_paths)
3430
filesystem.write_path(CODECLIMATE_YAML, config.to_yaml)
3531
end
3632

@@ -55,28 +51,27 @@ def available_configs
5551
all_paths.reject { |path| ['.', '..'].include?(File.basename(path)) }
5652
end
5753

58-
def exclude_paths(paths)
59-
expanded_paths = []
60-
paths.each do |dir|
61-
if filesystem.exist?(dir)
62-
expanded_paths << "#{dir}/**/*"
63-
end
54+
def engine_eligible?(engine)
55+
!engine["community"] && engine["enable_regexps"].present? && has_files?(engine)
56+
end
57+
58+
def has_files?(engine)
59+
filesystem.any? do |path|
60+
engine["enable_regexps"].any? { |re| Regexp.new(re).match(path) }
6461
end
65-
expanded_paths
6662
end
6763

68-
def engine_eligible?(engine)
69-
!engine["community"] &&
70-
engine["enable_regexps"].present? &&
71-
filesystem.any? do |path|
72-
engine["enable_regexps"].any? { |re| Regexp.new(re).match(path) }
73-
end
64+
def auto_exclude_paths
65+
AUTO_EXCLUDE_PATHS.select { |path| filesystem.exist?(path) }
7466
end
7567

7668
def eligible_engines
77-
@eligible_engines ||= engine_registry.list.each_with_object({}) do |(engine_name, config), result|
69+
return @eligible_engines if @eligible_engines
70+
71+
engines = engine_registry.list
72+
@eligible_engines = engines.each_with_object({}) do |(name, config), result|
7873
if engine_eligible?(config)
79-
result[engine_name] = config
74+
result[name] = config
8075
end
8176
end
8277
end

spec/cc/cli/config_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require "spec_helper"
2+
require "cc/cli/config"
3+
4+
module CC::CLI
5+
describe CC::CLI::Config do
6+
describe "#add_engine" do
7+
it "enables the passed in engine" do
8+
config = CC::CLI::Config.new()
9+
engine_config = {
10+
"default_ratings_paths" => ["foo"]
11+
}
12+
13+
config.add_engine("foo", engine_config)
14+
15+
engine = YAML.load(config.to_yaml)["engines"]["foo"]
16+
engine.must_equal({ "enabled" => true })
17+
end
18+
19+
it "copies over default configuration" do
20+
config = CC::CLI::Config.new()
21+
engine_config = {
22+
"default_config" => { "awesome" => true },
23+
"default_ratings_paths" => ["foo"]
24+
}
25+
26+
config.add_engine("foo", engine_config)
27+
28+
engine = YAML.load(config.to_yaml)["engines"]["foo"]
29+
engine.must_equal({
30+
"enabled" => true,
31+
"config" => {
32+
"awesome" => true
33+
}
34+
})
35+
end
36+
end
37+
38+
describe "#add_exclude_paths" do
39+
it "adds exclude paths to config with glob" do
40+
config = CC::CLI::Config.new()
41+
config.add_exclude_paths(["foo"])
42+
43+
exclude_paths = YAML.load(config.to_yaml)["exclude_paths"]
44+
exclude_paths.must_equal(["foo/**/*"])
45+
end
46+
end
47+
end
48+
end

spec/cc/cli/init_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ module CC::CLI
3636
"engines" => {
3737
"rubocop" => { "enabled"=>true },
3838
"eslint" => { "enabled"=>true },
39-
"csslint"=> { "enabled"=>true },
39+
"csslint" => { "enabled"=>true },
40+
"duplication" => {
41+
"enabled" => true,
42+
"config" => { "languages" => ["ruby"] }
43+
}
4044
},
4145
"ratings" => { "paths" => ["**.rb", "**.js", "**.jsx", "**.css"] },
4246
"exclude_paths" => ["config/**/*", "spec/**/*", "vendor/**/*"],

0 commit comments

Comments
 (0)