From a3faac1603099ddde390680909313317a564d7f0 Mon Sep 17 00:00:00 2001 From: Brian John Date: Sat, 8 Dec 2018 20:32:40 -0600 Subject: [PATCH] Avoid premature failure with parallel_tests 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. --- CHANGELOG.md | 7 +++++++ lib/simplecov.rb | 18 +++++++++++++++++- lib/simplecov/version.rb | 2 +- spec/simplecov_spec.rb | 23 +++++++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44673f37..2e5ad6a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +0.16.2 (TBD) +=================== + +## 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) =================== diff --git a/lib/simplecov.rb b/lib/simplecov.rb index 11ab2db3..0176a94e 100644 --- a/lib/simplecov.rb +++ b/lib/simplecov.rb @@ -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 @@ -220,7 +221,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 @@ -248,6 +249,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 # diff --git a/lib/simplecov/version.rb b/lib/simplecov/version.rb index 8475e649..8e0a05ff 100644 --- a/lib/simplecov/version.rb +++ b/lib/simplecov/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module SimpleCov - VERSION = "0.16.1".freeze + VERSION = "0.16.2".freeze end diff --git a/spec/simplecov_spec.rb b/spec/simplecov_spec.rb index 6102fcb6..336d0a15 100644 --- a/spec/simplecov_spec.rb +++ b/spec/simplecov_spec.rb @@ -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 @@ -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 @@ -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