Skip to content

Commit 120d0ea

Browse files
authored
Fix minimum_coverage_by_file check & prep 0.21.1 (#966)
minimum_coverage_by_file was a gaping hole in the test suite, which is now partially remedied through rather thorough cucumber scenarios and 2 small unit tests that should be expanded on in the future. On the negative side: This will inconvenince some people. On the positive side stuffed another hole in the test suite towards a better future :D Also it was reported very soon thanks to @iMacTia Fixes #965
1 parent 20af434 commit 120d0ea

8 files changed

+121
-22
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
0.21.1 (2021-01-04)
2+
==========
3+
4+
## Bugfixes
5+
* `minimum_coverage_by_file` works again as expected (errored out before 😱)
6+
17
0.21.0 (2021-01-03)
28
==========
39

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
simplecov (0.21.0)
4+
simplecov (0.21.1)
55
docile (~> 1.1)
66
simplecov-html (~> 0.11)
77
simplecov_json_formatter (~> 0.1)

features/minimum_coverage.feature

+3-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Feature:
77
Background:
88
Given I'm working on the project "faked_project"
99

10-
Scenario:
10+
Scenario: It fails against too high coverage
1111
Given SimpleCov for Test/Unit is configured with:
1212
"""
1313
require 'simplecov'
@@ -22,7 +22,7 @@ Feature:
2222
And the output should contain "Line coverage (88.09%) is below the expected minimum coverage (90.00%)."
2323
And the output should contain "SimpleCov failed with exit 2"
2424

25-
Scenario:
25+
Scenario: It fails if it's just 0.01% too low
2626
Given SimpleCov for Test/Unit is configured with:
2727
"""
2828
require 'simplecov'
@@ -37,7 +37,7 @@ Feature:
3737
And the output should contain "Line coverage (88.09%) is below the expected minimum coverage (88.10%)."
3838
And the output should contain "SimpleCov failed with exit 2"
3939

40-
Scenario:
40+
Scenario: It passes when it is exactly the coverage
4141
Given SimpleCov for Test/Unit is configured with:
4242
"""
4343
require 'simplecov'
@@ -50,18 +50,6 @@ Feature:
5050
When I run `bundle exec rake test`
5151
Then the exit status should be 0
5252

53-
Scenario:
54-
Given SimpleCov for Test/Unit is configured with:
55-
"""
56-
require 'simplecov'
57-
SimpleCov.start do
58-
add_filter 'test.rb'
59-
end
60-
"""
61-
62-
When I run `bundle exec rake test`
63-
Then the exit status should be 0
64-
6553
@branch_coverage
6654
Scenario: Works together with branch coverage and the new criterion announcing both failures
6755
Given SimpleCov for Test/Unit is configured with:
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
@test_unit @config
2+
Feature:
3+
4+
Exit code should be non-zero if the coverage of any one file is below the configured value.
5+
6+
Background:
7+
Given I'm working on the project "faked_project"
8+
9+
Scenario: slightly under minimum coverage by file
10+
Given SimpleCov for Test/Unit is configured with:
11+
"""
12+
require 'simplecov'
13+
SimpleCov.start do
14+
add_filter 'test.rb'
15+
minimum_coverage_by_file 75.01
16+
end
17+
"""
18+
19+
When I run `bundle exec rake test`
20+
Then the exit status should not be 0
21+
And the output should contain "Line coverage by file (75.00%) is below the expected minimum coverage (75.01%)."
22+
And the output should contain "SimpleCov failed with exit 2"
23+
24+
Scenario: Just passing it
25+
Given SimpleCov for Test/Unit is configured with:
26+
"""
27+
require 'simplecov'
28+
SimpleCov.start do
29+
add_filter 'test.rb'
30+
minimum_coverage_by_file 75
31+
end
32+
"""
33+
34+
When I run `bundle exec rake test`
35+
Then the exit status should be 0
36+
37+
@branch_coverage
38+
Scenario: Works together with branch coverage and the new criterion announcing both failures
39+
Given SimpleCov for Test/Unit is configured with:
40+
"""
41+
require 'simplecov'
42+
SimpleCov.start do
43+
add_filter 'test.rb'
44+
enable_coverage :branch
45+
minimum_coverage_by_file line: 90, branch: 70
46+
end
47+
"""
48+
49+
When I run `bundle exec rake test`
50+
Then the exit status should not be 0
51+
And the output should contain "Line coverage by file (80.00%) is below the expected minimum coverage (90.00%)."
52+
And the output should contain "Branch coverage by file (50.00%) is below the expected minimum coverage (70.00%)."
53+
And the output should contain "SimpleCov failed with exit 2"
54+
55+
@branch_coverage
56+
Scenario: Can set branch as primary coverage and it will fail if branch is below minimum coverage
57+
Given SimpleCov for Test/Unit is configured with:
58+
"""
59+
require 'simplecov'
60+
SimpleCov.start do
61+
add_filter 'test.rb'
62+
enable_coverage :branch
63+
primary_coverage :branch
64+
minimum_coverage_by_file 70
65+
end
66+
"""
67+
68+
When I run `bundle exec rake test`
69+
Then the exit status should not be 0
70+
And the output should contain "Branch coverage by file (50.00%) is below the expected minimum coverage (70.00%)."
71+
And the output should not contain "Line coverage"
72+
And the output should contain "SimpleCov failed with exit 2"

lib/simplecov/exit_codes/minimum_coverage_by_file_check.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def failing?
1515
def report
1616
minimum_violations.each do |violation|
1717
$stderr.printf(
18-
"%<criterion>s coverage (%<covered>.2f%%) is below the expected minimum coverage (%<minimum_coverage>.2f%%).\n",
18+
"%<criterion>s coverage by file (%<covered>.2f%%) is below the expected minimum coverage (%<minimum_coverage>.2f%%).\n",
1919
covered: SimpleCov.round_coverage(violation.fetch(:actual)),
2020
minimum_coverage: violation.fetch(:minimum_expected),
2121
criterion: violation.fetch(:criterion).capitalize
@@ -40,11 +40,11 @@ def minimum_violations
4040

4141
def compute_minimum_coverage_data
4242
minimum_coverage_by_file.flat_map do |criterion, expected_percent|
43-
result.coverage_statistics_by_file[criterion].map do |actual_percent|
43+
result.coverage_statistics_by_file.fetch(criterion).map do |actual_coverage|
4444
{
4545
criterion: criterion,
4646
minimum_expected: expected_percent,
47-
actual: SimpleCov.round_coverage(actual_percent)
47+
actual: SimpleCov.round_coverage(actual_coverage.percent)
4848
}
4949
end
5050
end

lib/simplecov/file_list.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def branch_covered_percent
106106

107107
def compute_coverage_statistics_by_file
108108
@files.each_with_object(line: [], branch: []) do |file, together|
109-
together[:line] << file.coverage_statistics[:line]
110-
together[:branch] << file.coverage_statistics[:branch] if SimpleCov.branch_coverage?
109+
together[:line] << file.coverage_statistics.fetch(:line)
110+
together[:branch] << file.coverage_statistics.fetch(:branch) if SimpleCov.branch_coverage?
111111
end
112112
end
113113

lib/simplecov/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module SimpleCov
4-
VERSION = "0.21.0"
4+
VERSION = "0.21.1"
55
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
require "helper"
4+
5+
RSpec.describe SimpleCov::ExitCodes::MinimumCoverageByFileCheck do
6+
let(:result) do
7+
instance_double(SimpleCov::Result, coverage_statistics_by_file: stats)
8+
end
9+
let(:stats) do
10+
{
11+
line: [SimpleCov::CoverageStatistics.new(covered: 8, missed: 2)]
12+
}
13+
end
14+
15+
subject { described_class.new(result, minimum_coverage_by_file) }
16+
17+
context "all files passing requirements" do
18+
let(:minimum_coverage_by_file) { {line: 80} }
19+
20+
it "passes" do
21+
expect(subject).not_to be_failing
22+
end
23+
end
24+
25+
context "one file violating requirements" do
26+
let(:minimum_coverage_by_file) { {line: 90} }
27+
28+
it "fails" do
29+
p minimum_coverage_by_file
30+
expect(subject).to be_failing
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)