From 27d7ee765a3e31a05f62a2097e8e47d243ba096e Mon Sep 17 00:00:00 2001 From: Matt Schreiber Date: Tue, 25 Jun 2019 03:55:42 -0400 Subject: [PATCH] Don't call SimpleCov.result before checking SimpleCov.result? (#674) * simplecov.rb: check .result? in .run_exit_tasks! rather than in .process_result in order to avoid instantiating SimpleCov.result prior to performing the .result? check, as doing so guarantees that .result? will always return 'true' * spec/result_merger_spec.rb: test that manually-stored merged results data isn't clobbered by SimpleCov.run_exit_tasks\!/.process_results --- lib/simplecov.rb | 5 +++-- spec/result_merger_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/simplecov.rb b/lib/simplecov.rb index 11ab2db3..4f945685 100644 --- a/lib/simplecov.rb +++ b/lib/simplecov.rb @@ -199,7 +199,9 @@ def run_exit_tasks! SimpleCov.at_exit.call - exit_status = SimpleCov.process_result(SimpleCov.result, exit_status) + # Don't modify the exit status unless the result has already been + # computed + exit_status = SimpleCov.process_result(SimpleCov.result, exit_status) if SimpleCov.result? # Force exit with stored status (see github issue #5) # unless it's nil or 0 (see github issue #281) @@ -212,7 +214,6 @@ def run_exit_tasks! # exit_status = SimpleCov.process_result(SimpleCov.result, exit_status) # def process_result(result, exit_status) - return exit_status unless SimpleCov.result? # Result has been computed return exit_status if exit_status != SimpleCov::ExitCodes::SUCCESS # Existing errors covered_percent = result.covered_percent.round(2) diff --git a/spec/result_merger_spec.rb b/spec/result_merger_spec.rb index 78fb8fe8..0e239252 100644 --- a/spec/result_merger_spec.rb +++ b/spec/result_merger_spec.rb @@ -104,6 +104,31 @@ expect(SimpleCov::ResultMerger).to receive(:synchronize_resultset) SimpleCov::ResultMerger.store_result({}) end + + it "persists after exit" do + skip "fork not available on JRuby" if RUBY_ENGINE == "jruby" + + coverage = {__FILE__ => 5.downto(1).to_a} + + # Fork to ensure that the Kernel.at_exit block defined in + # simplecov/defaults.rb runs + pid = Kernel.fork do + SimpleCov.start + + # Override the default SimpleCov.at_exit block to avoid instantiating + # SimpleCov.result + SimpleCov.at_exit {} + SimpleCov.command_name "merged result persistence" + + result = SimpleCov::Result.new(coverage) + SimpleCov::ResultMerger.store_result(result) + end + + Process.waitpid pid + + merged_result = SimpleCov::ResultMerger.merged_result + expect(merged_result.original_result).to include(coverage) + end end describe ".resultset" do