Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit d182fc8

Browse files
committed
Merge pull request #2926 from jaredbeck/warn_about_exit_in_helper
Warn about exit in helper
1 parent a313a81 commit d182fc8

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

lib/rspec/core/configuration.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,14 @@ def load_file_handling_errors(method, file)
19451945
relative_file = Metadata.relative_path(file)
19461946
reporter.notify_non_example_exception(ex, "An error occurred while loading #{relative_file}.")
19471947
RSpec.world.wants_to_quit = true
1948+
rescue SystemExit => ex
1949+
relative_file = Metadata.relative_path(file)
1950+
reporter.notify_non_example_exception(
1951+
ex,
1952+
"While loading #{relative_file} an `exit` / `raise SystemExit` occurred, RSpec will now quit."
1953+
)
1954+
RSpec.world.rspec_is_quitting = true
1955+
raise ex
19481956
end
19491957

19501958
def handle_suite_hook(scope, meta)

lib/rspec/core/world.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ class World
1010
# Used internally to determine what to do when a SIGINT is received.
1111
attr_accessor :wants_to_quit
1212

13+
# Used internally to signify that a SystemExit occurred in
14+
# `Configuration#load_file_handling_errors`, and thus examples cannot
15+
# be counted accurately. Specifically, we cannot accurately report
16+
# "No examples found".
17+
# @private
18+
attr_accessor :rspec_is_quitting
19+
1320
# Used internally to signal that a failure outside of an example
1421
# has occurred, and that therefore the exit status should indicate
1522
# the run failed.
@@ -18,6 +25,7 @@ class World
1825

1926
def initialize(configuration=RSpec.configuration)
2027
@wants_to_quit = false
28+
@rspec_is_quitting = false
2129
@configuration = configuration
2230
configuration.world = self
2331
@example_groups = []
@@ -178,10 +186,12 @@ def announce_filters
178186
return unless example_count.zero?
179187

180188
example_groups.clear
181-
if filter_manager.empty?
182-
report_filter_message("No examples found.")
183-
elsif exclusion_filter.empty? || inclusion_filter.empty?
184-
report_filter_message(everything_filtered_message)
189+
unless rspec_is_quitting
190+
if filter_manager.empty?
191+
report_filter_message("No examples found.")
192+
elsif exclusion_filter.empty? || inclusion_filter.empty?
193+
report_filter_message(everything_filtered_message)
194+
end
185195
end
186196
end
187197

spec/integration/spec_file_load_errors_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,37 @@
7878
EOS
7979
end
8080

81+
it 'prints a warning when a helper file exits early' do
82+
write_file_formatted "helper_with_exit.rb", "exit 999"
83+
84+
expect {
85+
run_command "--require ./helper_with_exit.rb"
86+
}.to raise_error(SystemExit)
87+
output = normalize_durations(last_cmd_stdout)
88+
if defined?(JRUBY_VERSION) && !JRUBY_VERSION.empty?
89+
expect(output).to eq unindent(<<-EOS)
90+
91+
While loading ./helper_with_exit.rb an `exit` / `raise SystemExit` occurred, RSpec will now quit.
92+
Failure/Error: Unable to find org/jruby/RubyKernel.java to read failed line
93+
94+
SystemExit:
95+
exit
96+
# ./helper_with_exit.rb:1#{spec_line_suffix}
97+
EOS
98+
else
99+
expect(output).to eq unindent(<<-EOS)
100+
101+
While loading ./helper_with_exit.rb an `exit` / `raise SystemExit` occurred, RSpec will now quit.
102+
Failure/Error: exit 999
103+
104+
SystemExit:
105+
exit
106+
# ./helper_with_exit.rb:1:in `exit'
107+
# ./helper_with_exit.rb:1#{spec_line_suffix}
108+
EOS
109+
end
110+
end
111+
81112
it 'nicely handles load-time errors in user spec files' do
82113
write_file_formatted "1_spec.rb", "
83114
boom

0 commit comments

Comments
 (0)