Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit e2535bc

Browse files
pointlessonegdiggs
authored andcommitted
Custom patterns (#157)
* Add custom patterns support * Update README with a section about file patterns
1 parent 0c1dd8c commit e2535bc

File tree

5 files changed

+133
-1
lines changed

5 files changed

+133
-1
lines changed

‎README.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ engines:
105105
- examples/
106106
```
107107

108+
### Custom file name patterns
109+
110+
All engines check only appropriate files but you can override default set of
111+
patterns. Patterns are ran aginast the project root direcory so you have to use
112+
`**` to match files in nested directories. Also note that you have to specify
113+
all patterns, not only the one you want to add.
114+
115+
```yml
116+
engines:
117+
duplication:
118+
enabled: true
119+
config:
120+
languages:
121+
ruby:
122+
patterns:
123+
- "**/*.rb
124+
- "**/*.rake"
125+
- "Rakefile"
126+
- "**/*.ruby"
127+
```
128+
129+
130+
108131
[codeclimate]: https://codeclimate.com/dashboard
109132
[what-is-duplication]: https://docs.codeclimate.com/docs/duplication-concept
110133
[flay]: https://github.com/seattlerb/flay

‎lib/cc/engine/analyzers/analyzer_base.rb‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ def process_file(path)
7070
def file_list
7171
@_file_list ||= ::CC::Engine::Analyzers::FileList.new(
7272
engine_config: engine_config,
73-
patterns: self.class::PATTERNS,
73+
patterns: engine_config.patterns_for(
74+
self.class::LANGUAGE,
75+
self.class::PATTERNS,
76+
),
7477
)
7578
end
7679

‎lib/cc/engine/analyzers/engine_config.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def fetch_language(language)
5555
end
5656
end
5757

58+
def patterns_for(language, fallbacks)
59+
Array(fetch_language(language).fetch("patterns", fallbacks))
60+
end
61+
5862
private
5963

6064
attr_reader :config
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
require 'spec_helper'
2+
require 'cc/engine/analyzers/analyzer_base'
3+
require 'cc/engine/analyzers/engine_config'
4+
require 'cc/engine/analyzers/file_list'
5+
6+
module CC::Engine::Analyzers
7+
RSpec.describe Base, in_tmpdir: true do
8+
class DummyAnalyzer < Base
9+
LANGUAGE = "dummy"
10+
PATTERNS = [
11+
'**/*.a',
12+
'**/*.b'
13+
]
14+
end
15+
16+
include AnalyzerSpecHelpers
17+
18+
let(:engine_config) { EngineConfig.new({}) }
19+
let(:analyzer) { DummyAnalyzer.new(engine_config: engine_config) }
20+
21+
before(:each) do
22+
create_source_file("foo.a", "")
23+
create_source_file("foo.b", "")
24+
create_source_file("foo.c", "")
25+
end
26+
27+
it "lists files according to the default patterns" do
28+
expect(analyzer.files).to match_array(['./foo.a', './foo.b'])
29+
end
30+
31+
context "with custom patterns" do
32+
let(:engine_config) do
33+
EngineConfig.new({
34+
"config" => {
35+
"languages" => {
36+
"dummy" => {
37+
"patterns" => [
38+
"**/*.c"
39+
],
40+
},
41+
},
42+
},
43+
})
44+
end
45+
46+
it "lists files according to the config patterns" do
47+
expect(analyzer.files).to match_array(['./foo.c'])
48+
end
49+
end
50+
51+
context "exact pattern" do
52+
let(:engine_config) do
53+
EngineConfig.new({
54+
"config" => {
55+
"languages" => {
56+
"dummy" => {
57+
"patterns" => [
58+
"*.c"
59+
],
60+
},
61+
},
62+
},
63+
})
64+
end
65+
66+
before(:each) do
67+
Dir.mkdir("nested")
68+
create_source_file("nested/foo.c", "")
69+
end
70+
71+
it "lists files exactly according to the config patterns" do
72+
expect(analyzer.files).to match_array(['./foo.c'])
73+
end
74+
end
75+
end
76+
end

‎spec/cc/engine/analyzers/engine_config_spec.rb‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,30 @@
202202
expect(engine_config.debug?).to eq(false)
203203
end
204204
end
205+
206+
describe "#patterns_for" do
207+
it "returns patterns for specified language" do
208+
engine_config = CC::Engine::Analyzers::EngineConfig.new({
209+
"config" => {
210+
"languages" => {
211+
"fancy" => {
212+
"patterns" => [
213+
"**/*.fancy"
214+
],
215+
},
216+
},
217+
},
218+
})
219+
220+
expect(engine_config.patterns_for("fancy", []))
221+
.to match_array(["**/*.fancy"])
222+
end
223+
224+
it "returns fallback patterns for missing language" do
225+
engine_config = CC::Engine::Analyzers::EngineConfig.new({})
226+
227+
expect(engine_config.patterns_for("fancy", ["**/*.fancy"]))
228+
.to match_array(["**/*.fancy"])
229+
end
230+
end
205231
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /