Skip to content

Commit

Permalink
Don't call SimpleCov.result before checking SimpleCov.result? (#674)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tomeon authored and PragTob committed Jun 25, 2019
1 parent 240caf9 commit 27d7ee7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/simplecov.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions spec/result_merger_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 27d7ee7

Please sign in to comment.