Skip to content

Commit

Permalink
Fixes simplecov-ruby#624 Only write last run result on successes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas07vt committed Oct 5, 2017
1 parent 96b7247 commit 3d5d338
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
50 changes: 50 additions & 0 deletions features/maximum_coverage_drop.feature
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,55 @@ Feature:
}
}
"""
Scenario: test failures do not update the resultset
Given SimpleCov for RSpec is configured with:
"""
require 'simplecov'
SimpleCov.start do
add_group 'Libs', 'lib/faked_project/'
add_filter '/spec/'
maximum_coverage_drop 0
end
"""

And a file named "lib/faked_project/missed.rb" with:
"""
class UncoveredSourceCode
def foo
never_reached
rescue => err
but no one cares about invalid ruby here
end
end
"""

And a file named "spec/failing_spec.rb" with:
"""
require "spec_helper"
describe FakedProject do
it "fails" do
expect(false).to eq(true)
end
end
"""
And the file named "coverage/.last_run.json" with:
"""
{
"result": {
"covered_percent": 100.0
}
}
"""

When I run `bundle exec rspec spec`
Then the exit status should be 1
And a file named "coverage/.last_run.json" should exist
And the file "coverage/.last_run.json" should contain:
"""
{
"result": {
"covered_percent": 100.0
}
}
"""

7 changes: 4 additions & 3 deletions lib/simplecov/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@
elsif covered_percentages.any? { |p| p < SimpleCov.minimum_coverage_by_file } # rubocop:disable Metrics/BlockNesting
$stderr.printf("File (%s) is only (%.2f%%) covered. This is below the expected minimum coverage per file of (%.2f%%).\n", SimpleCov.result.least_covered_file, covered_percentages.min, SimpleCov.minimum_coverage_by_file)
@exit_status = SimpleCov::ExitCodes::MINIMUM_COVERAGE
elsif (last_run = SimpleCov::LastRun.read) # rubocop:disable Metrics/BlockNesting
end

if (last_run = SimpleCov::LastRun.read) # rubocop:disable Metrics/BlockNesting
coverage_diff = last_run["result"]["covered_percent"] - covered_percent
if coverage_diff > SimpleCov.maximum_coverage_drop # rubocop:disable Metrics/BlockNesting
$stderr.printf("Coverage has dropped by %.2f%% since the last time (maximum allowed: %.2f%%).\n", coverage_diff, SimpleCov.maximum_coverage_drop)
Expand All @@ -87,8 +89,7 @@
end
end

# Don't overwrite last_run file if refuse_coverage_drop option is enabled and the coverage has dropped
unless @exit_status == SimpleCov::ExitCodes::MAXIMUM_COVERAGE_DROP
if @exit_status == SimpleCov::ExitCodes::SUCCESS
SimpleCov::LastRun.write(:result => {:covered_percent => covered_percent})
end
end
Expand Down

0 comments on commit 3d5d338

Please sign in to comment.