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

Commit f9df56c

Browse files
Tejasshetty-tejas
Tejas
authored andcommitted
Minimum Coverage Support
1 parent 13bddf2 commit f9df56c

File tree

7 files changed

+140
-17
lines changed

7 files changed

+140
-17
lines changed

‎lib/simplecov_json_formatter/result_hash_formatter.rb

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ module SimpleCovJSONFormatter
66
class ResultHashFormatter
77
def initialize(result)
88
@result = result
9+
@parent_path = "#{Dir.pwd}/"
910
end
1011

1112
def format
1213
format_files
1314
format_groups
15+
format_total
1416

1517
formatted_result
1618
end
@@ -19,34 +21,65 @@ def format
1921

2022
def format_files
2123
@result.files.each do |source_file|
22-
formatted_result[:coverage][source_file.filename] =
23-
format_source_file(source_file)
24+
formatted_file = format_source_file(source_file)
25+
26+
formatted_result[:coverage][source_file.filename] = formatted_file
27+
minimum_coverage_check_for_file!(source_file, formatted_file)
2428
end
2529
end
2630

2731
def format_groups
2832
@result.groups.each do |name, file_list|
2933
formatted_result[:groups][name] = {
3034
lines: {
31-
covered_percent: file_list.covered_percent
35+
covered_percent: file_list.covered_percent,
36+
covered_lines: file_list.covered_lines,
37+
missed_lines: file_list.missed_lines,
38+
lines_of_code: file_list.lines_of_code
3239
}
3340
}
3441
end
3542
end
3643

44+
def format_total
45+
formatted_result[:total] = {
46+
covered_percent: @result.covered_percent,
47+
covered_lines: @result.covered_lines,
48+
missed_lines: @result.missed_lines,
49+
lines_of_code: @result.total_lines
50+
}
51+
end
52+
3753
def formatted_result
3854
@formatted_result ||= {
3955
meta: {
40-
simplecov_version: SimpleCov::VERSION
56+
simplecov_version: SimpleCov::VERSION,config: config
4157
},
4258
coverage: {},
43-
groups: {}
59+
groups: {},
60+
errors: { less_than_minimum_coverage: {} },
61+
total: {}
4462
}
4563
end
4664

4765
def format_source_file(source_file)
4866
source_file_formatter = SourceFileFormatter.new(source_file)
4967
source_file_formatter.format
5068
end
69+
70+
def config
71+
@config ||= {
72+
minimum_coverage: SimpleCov.minimum_coverage[:line],
73+
minimum_coverage_by_file: SimpleCov.minimum_coverage_by_file[:line]
74+
}
75+
end
76+
77+
def minimum_coverage_check_for_file!(source_file, formatted_file)
78+
return nil unless config[:minimum_coverage_by_file]
79+
return nil unless formatted_file[:percent] < config[:minimum_coverage_by_file]
80+
81+
file_name = source_file.filename.delete_prefix(@parent_path)
82+
formatted_result[:errors][:less_than_minimum_coverage][file_name] = formatted_file[:percent]
83+
end
5184
end
5285
end

‎lib/simplecov_json_formatter/source_file_formatter.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def format
1919

2020
def line_coverage
2121
@line_coverage ||= {
22-
lines: lines
22+
lines: lines,
23+
percent: percent
2324
}
2425
end
2526

@@ -47,6 +48,10 @@ def branches
4748
branches
4849
end
4950

51+
def percent
52+
@source_file.covered_percent
53+
end
54+
5055
def parse_line(line)
5156
return line.coverage unless line.skipped?
5257

‎spec/fixtures/errors.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"meta": {
3+
"simplecov_version": "0.21.2",
4+
"config": { "minimum_coverage": 80, "minimum_coverage_by_file": 80 }
5+
},
6+
"coverage": {
7+
"/STUB_WORKING_DIRECTORY/spec/fixtures/sample.rb": {
8+
"lines": [
9+
null,
10+
0,
11+
0,
12+
0,
13+
0,
14+
null,
15+
null,
16+
0,
17+
0,
18+
null,
19+
null,
20+
1,
21+
1,
22+
0,
23+
null,
24+
1,
25+
null,
26+
null,
27+
null,
28+
"ignored",
29+
"ignored",
30+
"ignored",
31+
"ignored",
32+
"ignored",
33+
null
34+
], "percent": 30.0
35+
}
36+
},
37+
"errors": {
38+
"less_than_minimum_coverage": {"spec/fixtures/sample.rb": 30.0}
39+
},
40+
"total": {"covered_lines": 3, "covered_percent": 30.0, "lines_of_code": 10, "missed_lines": 7},
41+
"groups": {}
42+
}

‎spec/fixtures/sample.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"meta": {
3-
"simplecov_version": "0.21.2"
3+
"simplecov_version": "0.21.2",
4+
"config": { "minimum_coverage": null, "minimum_coverage_by_file": null }
45
},
56
"coverage": {
67
"/STUB_WORKING_DIRECTORY/spec/fixtures/sample.rb": {
@@ -30,8 +31,12 @@
3031
"ignored",
3132
"ignored",
3233
null
33-
]
34+
], "percent": 90.0
3435
}
3536
},
37+
"errors": {
38+
"less_than_minimum_coverage": {}
39+
},
40+
"total": {"covered_lines": 9, "covered_percent": 90.0, "lines_of_code": 10, "missed_lines": 1},
3641
"groups": {}
37-
}
42+
}

‎spec/fixtures/sample_groups.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"meta": {
3-
"simplecov_version": "0.21.2"
3+
"simplecov_version": "0.21.2",
4+
"config": { "minimum_coverage": null, "minimum_coverage_by_file": null }
45
},
56
"coverage": {
67
"/STUB_WORKING_DIRECTORY/spec/fixtures/sample.rb": {
@@ -30,14 +31,22 @@
3031
"ignored",
3132
"ignored",
3233
null
33-
]
34+
],
35+
"percent": 90.0
3436
}
3537
},
38+
"errors": {
39+
"less_than_minimum_coverage": {}
40+
},
41+
"total": {"covered_lines": 9, "covered_percent": 90.0, "lines_of_code": 10, "missed_lines": 1},
3642
"groups": {
3743
"My Group": {
3844
"lines": {
39-
"covered_percent": 80.0
45+
"covered_percent": 90.0,
46+
"covered_lines": 90,
47+
"missed_lines": 10,
48+
"lines_of_code": 100
4049
}
4150
}
4251
}
43-
}
52+
}

‎spec/fixtures/sample_with_branch.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"meta": {
3-
"simplecov_version": "0.21.2"
3+
"simplecov_version": "0.21.2",
4+
"config": { "minimum_coverage": null, "minimum_coverage_by_file": null }
45
},
56
"coverage": {
67
"/STUB_WORKING_DIRECTORY/spec/fixtures/sample.rb": {
@@ -44,8 +45,12 @@
4445
"end_line": 16,
4546
"coverage": 1
4647
}
47-
]
48+
], "percent": 90.0
4849
}
4950
},
51+
"errors": {
52+
"less_than_minimum_coverage": {}
53+
},
54+
"total": {"covered_lines": 9, "covered_percent": 90.0, "lines_of_code": 10, "missed_lines": 1},
5055
"groups": {}
51-
}
56+
}

‎spec/simplecov_json_formatter_spec.rb

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@
5656
end
5757
end
5858

59+
context 'with errors for minimum coverage' do
60+
let(:result) do
61+
SimpleCov::Result.new({
62+
source_fixture('sample.rb') => { 'lines' => [
63+
nil, 0, 0, 0, 0, nil, nil, 0, 0, nil, nil,
64+
1, 1, 0, nil, 1, nil, nil, nil, nil, 1, 0, nil, nil, nil
65+
] }
66+
})
67+
end
68+
69+
before do
70+
allow(SimpleCov).to receive(:minimum_coverage).and_return({ line: 80 })
71+
allow(SimpleCov).to receive(:minimum_coverage_by_file).and_return({ line: 80 })
72+
end
73+
74+
it 'displays errors for less than minimum coverage by file' do
75+
subject.format(result)
76+
expect(json_ouput).to eq(json_result('errors'))
77+
end
78+
end
79+
5980
context 'with groups' do
6081
let(:result) do
6182
res = SimpleCov::Result.new({
@@ -67,7 +88,10 @@
6788

6889
# right now SimpleCov works mostly on global state, hence setting the groups that way
6990
# would be global state --> Mocking is better here
70-
allow(res).to receive_messages(groups: { 'My Group' => double('File List', covered_percent: 80.0) })
91+
allow(res).to receive_messages(groups: { 'My Group' => double('File List', covered_percent: 90.0,
92+
covered_lines: 90,
93+
missed_lines: 10,
94+
lines_of_code: 100) })
7195
res
7296
end
7397

0 commit comments

Comments
(0)

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