Skip to content

Commit 53b8a73

Browse files
committed
Make at_exit hook installation opt-in via SimpleCov.start
defaults.rb auto-registered the at_exit block on every require. Move the registration into a new idempotent SimpleCov.install_at_exit_hook; SimpleCov.start calls it, SimpleCov.start_tracking deliberately doesn't. A no-op for normal users — the auto-hook returned early anyway when start hadn't been called.
1 parent 9b008e3 commit 53b8a73

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

lib/simplecov.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ class << self
4646
def start(profile = nil, &)
4747
initial_setup(profile, &)
4848
start_tracking
49+
install_at_exit_hook
50+
end
51+
52+
#
53+
# Install the at_exit hook that formats results and runs exit-code
54+
# checks. `SimpleCov.start` calls this automatically. Idempotent —
55+
# safe to call multiple times. Callers that drive the formatting
56+
# pipeline themselves (e.g., dogfood test setups) can skip it by
57+
# using `start_tracking` directly instead of `start`.
58+
#
59+
def install_at_exit_hook
60+
return if @at_exit_hook_installed
61+
62+
@at_exit_hook_installed = true
63+
Kernel.at_exit do
64+
next if SimpleCov.external_at_exit?
65+
66+
SimpleCov.at_exit_behavior
67+
end
4968
end
5069

5170
#

lib/simplecov/configuration.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,11 @@ def configure(&block)
216216
# end
217217
#
218218
def at_exit(&block)
219-
return proc {} unless active_session? || block
220-
221219
@at_exit = block if block
222-
@at_exit ||= proc { SimpleCov.result.format! }
220+
return @at_exit if @at_exit
221+
return proc {} unless active_session?
222+
223+
@at_exit = proc { SimpleCov.result.format! }
223224
end
224225

225226
# Whether SimpleCov has anything to do at exit: the Coverage module

lib/simplecov/defaults.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818
# Gotta stash this a-s-a-p, see the CommandGuesser class and i.e. #110 for further info
1919
SimpleCov::CommandGuesser.original_run_command = "#{$PROGRAM_NAME} #{ARGV.join(' ')}"
2020

21-
at_exit do
22-
next if SimpleCov.external_at_exit?
23-
24-
SimpleCov.at_exit_behavior
25-
end
26-
2721
# Autoload config from ~/.simplecov if present
2822
require_relative "load_global_config"
2923

0 commit comments

Comments
 (0)