Intercept most exit calls and raise instead

This commit is contained in:
Michael Miller 2022-07-12 23:02:20 -06:00
parent 3c9c7f88be
commit 754bfd6939
No known key found for this signature in database
GPG key ID: 32B47AE8F388A1FF
4 changed files with 43 additions and 15 deletions

View file

@ -51,6 +51,7 @@ require "./runner_events"
require "./runner"
require "./spec_builder"
require "./spec"
require "./system_exit"
require "./tag_node_filter"
require "./test_context"
require "./value"

View file

@ -0,0 +1,25 @@
module Spectator
# Indicates a call to exit the application was performed.
class SystemExit < Exception
# Status code passed to the exit call.
getter status : Int32
# Creates the exception.
def initialize(message : String? = nil, cause : Exception? = nil, @status : Int32 = 0)
super(message, cause)
end
end
# Allow Spectator to exit normally when needed.
private def self.exit(status = 0) : NoReturn
::Crystal::System::Process.exit(status)
end
end
class Process
# Replace the typically used exit method with a method that raises.
# This allows tests to catch attempts to exit the application.
def self.exit(status = 0) : NoReturn
raise ::Spectator::SystemExit.new(status: status)
end
end