Skip to content

Commit

Permalink
Avoid premature failure with parallel_tests
Browse files Browse the repository at this point in the history
When running within
[parallel_tests](https://github.com/grosser/parallel_tests/wiki), wait
until all test processes complete before evaluating coverage statistics.
This avoids a premature failure exit code if an early run finishes with
coverage that does not meet the configured criteria.
  • Loading branch information
f1sherman authored and PragTob committed Jun 25, 2019
1 parent 2d5e6c9 commit 318b212
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

* Print the exit status explicitly when it's not a successful build so it's easier figure out SimpleCov failed the build in the output.

## Bugfixes

* Avoid a premature failure exit code when setting `minimum_coverage` in combination with using [parallel_tests](https://github.com/grosser/parallel_tests)

0.16.1 (2018-03-16)
===================
Expand Down
18 changes: 17 additions & 1 deletion lib/simplecov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def result
# If we're using merging of results, store the current result
# first (if there is one), then merge the results and return those
if use_merging
wait_for_other_processes
SimpleCov::ResultMerger.store_result(@result) if result?
@result = SimpleCov::ResultMerger.merged_result
end
Expand Down Expand Up @@ -224,7 +225,7 @@ def process_result(result, exit_status)
if result_exit_status == SimpleCov::ExitCodes::SUCCESS # No result errors
write_last_run(covered_percent)
end
result_exit_status
final_result_process? ? result_exit_status : SimpleCov::ExitCodes::SUCCESS
end

# @api private
Expand Down Expand Up @@ -252,6 +253,21 @@ def result_exit_status(result, covered_percent)
end
# rubocop:enable Metrics/MethodLength

#
# @api private
#
def final_result_process?
!defined?(ParallelTests) || ParallelTests.last_process?
end

#
# @api private
#
def wait_for_other_processes
return unless defined?(ParallelTests) && final_result_process?
ParallelTests.wait_for_other_processes_to_finish
end

#
# @api private
#
Expand Down
23 changes: 23 additions & 0 deletions spec/simplecov_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
context "with merging disabled" do
before do
allow(SimpleCov).to receive(:use_merging).once.and_return(false)
expect(SimpleCov).to_not receive(:wait_for_other_processes)
end

context "when not running" do
Expand Down Expand Up @@ -64,6 +65,7 @@
allow(SimpleCov).to receive(:use_merging).once.and_return(true)
allow(SimpleCov::ResultMerger).to receive(:store_result).once
allow(SimpleCov::ResultMerger).to receive(:merged_result).once.and_return(the_merged_result)
expect(SimpleCov).to receive(:wait_for_other_processes)
end

context "when not running" do
Expand Down Expand Up @@ -170,5 +172,26 @@
end
end
end

describe ".process_result" do
before do
expect(SimpleCov).to receive(:result_exit_status).and_return SimpleCov::ExitCodes::MINIMUM_COVERAGE
expect(SimpleCov).to receive(:result?).and_return true
end
context "when the final result process" do
let(:result) { double(SimpleCov::Result, :covered_percent => 0.0) }
before { expect(SimpleCov).to receive(:final_result_process?).and_return true }
it "returns the exit code from .result_exit_status" do
expect(SimpleCov.process_result(result, SimpleCov::ExitCodes::SUCCESS)).to eq SimpleCov::ExitCodes::MINIMUM_COVERAGE
end
end
context "when not the final result process" do
let(:result) { double(SimpleCov::Result, :covered_percent => 0.0) }
before { expect(SimpleCov).to receive(:final_result_process?).and_return false }
it "returns the success exit code" do
expect(SimpleCov.process_result(result, SimpleCov::ExitCodes::SUCCESS)).to eq SimpleCov::ExitCodes::SUCCESS
end
end
end
end
end

0 comments on commit 318b212

Please sign in to comment.