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

Improve Ctrl-C handling of spec #5719

Merged
Merged
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
8 changes: 5 additions & 3 deletions src/spec/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ module Spec
@results[result.kind] << result
end

def self.print_results(elapsed_time)
@@instance.print_results(elapsed_time)
def self.print_results(elapsed_time, aborted = false)
@@instance.print_results(elapsed_time, aborted)
end

def self.succeeded
@@instance.succeeded
end

def print_results(elapsed_time)
def print_results(elapsed_time, aborted = false)
Spec.formatters.each(&.finish)

pendings = @results[:pending]
Expand Down Expand Up @@ -124,11 +124,13 @@ module Spec
total = pendings.size + failures.size + errors.size + success.size

final_status = case
when aborted then :error
when (failures.size + errors.size) > 0 then :fail
when pendings.size > 0 then :pending
else :success
end

puts "Aborted!".colorize.red if aborted
puts "Finished in #{Spec.to_human(elapsed_time)}"
puts Spec.color("#{total} examples, #{failures.size} failures, #{errors.size} errors, #{pendings.size} pending", final_status)

Expand Down
5 changes: 3 additions & 2 deletions src/spec/dsl.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module Spec

# :nodoc:
def self.abort!
@@aborted = true
exit
end

Expand Down Expand Up @@ -169,8 +170,8 @@ module Spec
start_time = Time.monotonic
at_exit do
elapsed_time = Time.monotonic - start_time
Spec::RootContext.print_results(elapsed_time)
exit 1 unless Spec::RootContext.succeeded
Spec::RootContext.print_results(elapsed_time, @@aborted)
exit 1 unless Spec::RootContext.succeeded && !@@aborted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the expanded condition, I would rather use if !Spec::RootContext.succeeded || @aborted here. That logic is easier to follow.

end
end
end
Expand Down