-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix exit in at_exit handlers #5413
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
30b1b43
Fix exit/raise in at_exit handlers
bew 1e0ca29
Add specs for exit & at_exit
bew 42fef26
Refactor handler loop
bew c8d6ca0
Disallow nested at_exit handlers
bew 4f68b04
Print an unhandled exception after all at_exit handlers
bew dac5291
Use try for the unhandled exception handler
bew 2620e04
Move the proc creation inside AtExitHandlers
bew 15f4c35
Fix doc
bew 6f9b050
Use a separate list for exceptions registered to print after at_exit …
bew aacbb40
Don't early return, always check for exceptions
bew 604b49c
Don't use a list for unhandled exceptions, store only one
bew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
require "spec" | ||
require "tempfile" | ||
|
||
private def build_and_run(code) | ||
code_file = Tempfile.new("exit_spec_code") | ||
code_file.close | ||
|
||
# write code to the temp file | ||
File.write(code_file.path, code) | ||
|
||
binary_file = Tempfile.new("exit_spec_output") | ||
binary_file.close | ||
|
||
`bin/crystal build #{code_file.path.inspect} -o #{binary_file.path.inspect}` | ||
File.exists?(binary_file.path).should be_true | ||
|
||
out_io, err_io = IO::Memory.new, IO::Memory.new | ||
status = Process.run binary_file.path, output: out_io, error: err_io | ||
|
||
{status, out_io.to_s, err_io.to_s} | ||
ensure | ||
File.delete(code_file.path) if code_file | ||
File.delete(binary_file.path) if binary_file | ||
end | ||
|
||
describe "exit" do | ||
it "exits normally with status 0" do | ||
status, _ = build_and_run "exit" | ||
status.success?.should be_true | ||
end | ||
|
||
it "exits with given error code" do | ||
status, _ = build_and_run "exit 42" | ||
status.success?.should be_false | ||
status.exit_code.should eq(42) | ||
end | ||
end | ||
|
||
describe "at_exit" do | ||
it "runs handlers on normal program ending" do | ||
status, output = build_and_run <<-CODE | ||
at_exit do | ||
puts "handler code" | ||
end | ||
CODE | ||
|
||
status.success?.should be_true | ||
output.should eq("handler code\n") | ||
end | ||
|
||
it "runs handlers on explicit program ending" do | ||
status, output = build_and_run <<-'CODE' | ||
at_exit do |exit_code| | ||
puts "handler code, exit code: #{exit_code}" | ||
end | ||
|
||
exit 42 | ||
CODE | ||
|
||
status.exit_code.should eq(42) | ||
output.should eq("handler code, exit code: 42\n") | ||
end | ||
|
||
it "runs handlers in reverse order" do | ||
status, output = build_and_run <<-CODE | ||
at_exit do | ||
puts "first handler code" | ||
end | ||
|
||
at_exit do | ||
puts "second handler code" | ||
end | ||
CODE | ||
|
||
status.success?.should be_true | ||
output.should eq <<-OUTPUT | ||
second handler code | ||
first handler code | ||
|
||
OUTPUT | ||
end | ||
|
||
it "runs all handlers maximum once" do | ||
status, output = build_and_run <<-CODE | ||
at_exit do | ||
puts "first handler code" | ||
end | ||
|
||
at_exit do | ||
puts "second handler code, explicit exit!" | ||
exit | ||
|
||
puts "not executed" | ||
end | ||
|
||
at_exit do | ||
puts "third handler code" | ||
end | ||
CODE | ||
|
||
status.success?.should be_true | ||
output.should eq <<-OUTPUT | ||
third handler code | ||
second handler code, explicit exit! | ||
first handler code | ||
|
||
OUTPUT | ||
end | ||
|
||
it "allows handlers to change the exit code with explicit `exit` call" do | ||
status, output = build_and_run <<-'CODE' | ||
at_exit do |exit_code| | ||
puts "first handler code, exit code: #{exit_code}" | ||
end | ||
|
||
at_exit do | ||
puts "second handler code, re-exiting" | ||
exit 42 | ||
|
||
puts "not executed" | ||
end | ||
|
||
at_exit do |exit_code| | ||
puts "third handler code, exit code: #{exit_code}" | ||
end | ||
CODE | ||
|
||
status.success?.should be_false | ||
status.exit_code.should eq(42) | ||
output.should eq <<-OUTPUT | ||
third handler code, exit code: 0 | ||
second handler code, re-exiting | ||
first handler code, exit code: 42 | ||
|
||
OUTPUT | ||
end | ||
|
||
it "allows handlers to change the exit code with explicit `exit` call (2)" do | ||
status, output = build_and_run <<-'CODE' | ||
at_exit do |exit_code| | ||
puts "first handler code, exit code: #{exit_code}" | ||
end | ||
|
||
at_exit do | ||
puts "second handler code, re-exiting" | ||
exit 42 | ||
|
||
puts "not executed" | ||
end | ||
|
||
at_exit do |exit_code| | ||
puts "third handler code, exit code: #{exit_code}" | ||
end | ||
|
||
exit 21 | ||
CODE | ||
|
||
status.success?.should be_false | ||
status.exit_code.should eq(42) | ||
output.should eq <<-OUTPUT | ||
third handler code, exit code: 21 | ||
second handler code, re-exiting | ||
first handler code, exit code: 42 | ||
|
||
OUTPUT | ||
end | ||
|
||
it "changes final exit code when an handler raises an error" do | ||
status, output, error = build_and_run <<-'CODE' | ||
at_exit do |exit_code| | ||
puts "first handler code, exit code: #{exit_code}" | ||
end | ||
|
||
at_exit do | ||
puts "second handler code, raising" | ||
raise "Raised from at_exit handler!" | ||
|
||
puts "not executed" | ||
end | ||
|
||
at_exit do |exit_code| | ||
puts "third handler code, exit code: #{exit_code}" | ||
end | ||
CODE | ||
|
||
status.success?.should be_false | ||
status.exit_code.should eq(1) | ||
output.should eq <<-OUTPUT | ||
third handler code, exit code: 0 | ||
second handler code, raising | ||
first handler code, exit code: 1 | ||
|
||
OUTPUT | ||
error.should eq "Error running at_exit handler: Raised from at_exit handler!\n" | ||
end | ||
|
||
it "errors when used in an at_exit handler" do | ||
status, output, error = build_and_run <<-CODE | ||
at_exit do | ||
at_exit {} | ||
end | ||
CODE | ||
|
||
status.success?.should be_false | ||
error.should eq "Error running at_exit handler: Cannot use at_exit from an at_exit handler\n" | ||
end | ||
|
||
it "shows unhandled exceptions after at_exit handlers" do | ||
status, _, error = build_and_run <<-CODE | ||
at_exit do | ||
STDERR.puts "first handler code" | ||
end | ||
|
||
at_exit do | ||
STDERR.puts "second handler code" | ||
end | ||
|
||
raise "Kaboom!" | ||
CODE | ||
|
||
status.success?.should be_false | ||
error.should contain <<-OUTPUT | ||
second handler code | ||
first handler code | ||
Unhandled exception: Kaboom! | ||
OUTPUT | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC you can skip
begin ... end
block altogether:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it worked too, but it doesn't inside a while..
expecting identifier 'end', not 'rescue'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember exactly but I think it was ambiguous whether the rescue should be after each loop, or after the whole while.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep