Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run specs even if Kernel#exit() was called #720

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/rspec/core/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@ class Runner
# Register an at_exit hook that runs the suite.
def self.autorun
return if autorun_disabled? || installed_at_exit? || running_in_drb?
at_exit { exit run(ARGV, $stderr, $stdout).to_i unless $! }
at_exit do
# Don't bother running any specs and just let the program terminate
# if we got here due to an unrescued exception (anything other than
# SystemExit, which is raised when somebody calls Kernel#exit).
next if $! and not $!.kind_of? SystemExit

# We got here because either the end of the program was reached or
# somebody called Kernel#exit. Run the specs and then override any
# existing exit status with RSpec's exit status if any specs failed.
status = run(ARGV, $stderr, $stdout).to_i
exit status if status != 0
end
@installed_at_exit = true
end
AT_EXIT_HOOK_BACKTRACE_LINE = "#{__FILE__}:#{__LINE__ - 2}:in `autorun'"
Expand Down
19 changes: 19 additions & 0 deletions spec/rspec/core/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ module RSpec::Core
RSpec::Core::Runner.should_receive(:at_exit).never
RSpec::Core::Runner.autorun
end

context 'when dealing with exceptions' do
let(:ruby) { 'ruby -I lib -r rspec/autorun' }

it 'runs specs if no exception was raised' do
`#{ruby} -e nil`.should_not be_empty
end

it 'runs specs if SystemExit was raised' do
`#{ruby} -e exit`.should_not be_empty
`#{ruby} -e 'at_exit { exit }'`.should_not be_empty
`#{ruby} -e 'at_exit { raise SystemExit }'`.should_not be_empty
end

it 'does not run specs if an exception other than SystemExit was raised' do
`#{ruby} -e 'at_exit { raise }' 2>/dev/null`.should be_empty
`#{ruby} -e 'at_exit { raise Exception }' 2>/dev/null`.should be_empty
end
end
end

describe "#run" do
Expand Down