Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 735a1ef

Browse files
committed
Merge pull request #9 from codeclimate/exclude-and-include-paths
Exclude and include paths.
2 parents c74d303 + 7d9790d commit 735a1ef

File tree

4 files changed

+128
-11
lines changed

4 files changed

+128
-11
lines changed

.codeclimate.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ engines:
44
ratings:
55
paths:
66
- "**.rb"
7+
exclude_paths:
8+
- spec/**/*

.rubocop.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Style/StringLiterals:
2+
Enabled: false
3+
4+
Style/Documentation:
5+
Enabled: false
6+
7+
Metrics/LineLength:
8+
Enabled: false
9+
10+
Style/TrailingComma:
11+
Enabled: false
12+
13+
Style/FileName:
14+
Exclude:
15+
- 'bin/**/*'
16+
17+
Style/ClassAndModuleChildren:
18+
Exclude:
19+
- 'spec/**/*'
20+
21+
Metrics/ModuleLength:
22+
Exclude:
23+
- 'spec/**/*'
24+
25+
Style/GuardClause:
26+
Enabled: false
27+
28+
Style/IfUnlessModifier:
29+
Enabled: false
30+
31+
Style/DotPosition:
32+
Enabled: false
33+
34+
Style/SignalException:
35+
Enabled: false
36+
37+
Metrics/AbcSize:
38+
Enabled: false
39+
40+
Rails/TimeZone:
41+
Enabled: false

lib/cc/engine/csslint.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,31 @@ def results
4848
@results ||= Nokogiri::XML(csslint_xml)
4949
end
5050

51+
def build_files_with_exclusions(exclusions)
52+
files = Dir.glob("**/*.css")
53+
files.reject { |f| exclusions.include?(f) }
54+
end
55+
56+
def build_files_with_inclusions(inclusions)
57+
inclusions.map do |include_path|
58+
if include_path =~ %r{/$}
59+
Dir.glob("#{include_path}/**/*.css")
60+
else
61+
include_path
62+
end
63+
end.flatten
64+
end
65+
5166
def csslint_xml
52-
exclusions = @engine_config['exclude_paths'] || []
53-
final_files = files.reject { |f| exclusions.include?(f) }
54-
`csslint --format=checkstyle-xml #{final_files.join(" ")}`
67+
`csslint --format=checkstyle-xml #{files_to_inspect.join(" ")}`
5568
end
5669

57-
def files
58-
Dir.glob("**/*.css")
70+
def files_to_inspect
71+
if @engine_config["include_paths"]
72+
build_files_with_inclusions(@engine_config["include_paths"])
73+
else
74+
build_files_with_exclusions(@engine_config["exclude_paths"] || [])
75+
end
5976
end
6077
end
6178
end

spec/cc/engine/csslint_spec.rb

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,81 @@ module CC
55
module Engine
66
describe CSSlint do
77
let(:code) { Dir.mktmpdir }
8-
let(:lint) { CSSlint.new(directory: code, io: nil, engine_config: {}) }
9-
let(:content) { '#id { color: red; }' }
8+
let(:engine_config) { {} }
9+
let(:lint) do
10+
CSSlint.new(directory: code, io: nil, engine_config: engine_config)
11+
end
12+
let(:id_selector_content) { '#id { color: red; }' }
1013

1114
describe '#run' do
1215
it 'analyzes *.css files' do
13-
create_source_file('foo.css', content)
16+
create_source_file('foo.css', id_selector_content)
1417
expect{ lint.run }.to output(/Don't use IDs in selectors./).to_stdout
1518
end
1619

1720
it "doesn't analyze *.scss files" do
18-
create_source_file('foo.scss', content)
21+
create_source_file('foo.scss', id_selector_content)
1922
expect{ lint.run }.to_not output.to_stdout
2023
end
2124

22-
def create_source_file(path, content)
23-
File.write(File.join(code, path), content)
25+
describe "with exclude_paths" do
26+
let(:engine_config) { {"exclude_paths" => %w(excluded.css)} }
27+
28+
before do
29+
create_source_file("not_excluded.css", "p { margin: 5px }")
30+
create_source_file("excluded.css", id_selector_content)
31+
end
32+
33+
it "excludes all matching paths" do
34+
expect{ lint.run }.not_to \
35+
output(/Don't use IDs in selectors./).to_stdout
36+
end
37+
end
38+
39+
describe "with include_paths" do
40+
let(:engine_config) {
41+
{"include_paths" => %w(included.css included_dir/)}
42+
}
43+
44+
before do
45+
create_source_file("included.css", id_selector_content)
46+
create_source_file(
47+
"included_dir/file.css", "p { color: blue !important; }"
48+
)
49+
create_source_file(
50+
"included_dir/sub/sub/subdir/file.css", "img { }"
51+
)
52+
create_source_file("not_included.css", "a { outline: none; }")
53+
end
54+
55+
it "includes all mentioned files" do
56+
expect{ lint.run }.to \
57+
output(/Don't use IDs in selectors./).to_stdout
58+
end
59+
60+
it "expands directories" do
61+
expect{ lint.run }.to output(/Use of !important/).to_stdout
62+
expect{ lint.run }.to output(/Rule is empty/).to_stdout
63+
end
64+
65+
it "excludes any unmentioned files" do
66+
expect{ lint.run }.not_to \
67+
output(/Outlines should only be modified using :focus/).to_stdout
68+
end
69+
70+
it "shouldn't call a top-level Dir.glob ever" do
71+
expect(Dir).not_to receive(:glob).with("**/*.css")
72+
expect{ lint.run }.to \
73+
output(/Don't use IDs in selectors./).to_stdout
74+
end
2475
end
2576
end
77+
78+
def create_source_file(path, content)
79+
abs_path = File.join(code, path)
80+
FileUtils.mkdir_p(File.dirname(abs_path))
81+
File.write(abs_path, content)
82+
end
2683
end
2784
end
2885
end

0 commit comments

Comments
 (0)