Flag to disable running tests

This is needed for testing Spectator.
This commit is contained in:
Michael Miller 2018-10-15 00:32:29 -06:00
parent cd86687dfb
commit c77a85c97a
2 changed files with 29 additions and 16 deletions

View file

@ -1,2 +1,5 @@
require "spec" require "spec"
require "../src/spectator" require "../src/spectator"
# Prevent Spectator from trying to run tests.
Spectator.autorun = false

View file

@ -45,7 +45,14 @@ module Spectator
end end
end end
# Flag indicating whether Spectator should automatically run tests.
# This should be left alone (set to true) in typical usage.
# There are times when Spectator shouldn't run tests.
# One of those is testing Spectator.
class_property? autorun = true
# All tests are ran just before the executable exits. # All tests are ran just before the executable exits.
# Tests will be skipped, however, if `#autorun?` is set to false.
# There are a couple of reasons for this. # There are a couple of reasons for this.
# #
# First is that we want a clean interface for the end-user. # First is that we want a clean interface for the end-user.
@ -62,21 +69,24 @@ module Spectator
# Crystal doesn't display much information about what happened. # Crystal doesn't display much information about what happened.
# That issue is handled by putting a begin/rescue block to show a custom error message. # That issue is handled by putting a begin/rescue block to show a custom error message.
at_exit do at_exit do
begin run if autorun?
# Build the root-level example group and run it. end
group = ::Spectator::DSL::Builder.build
Runner.new(group).run # Builds the tests and runs the framework.
rescue ex private def self.run
# Catch all unhandled exceptions here. # Build the root-level example group and run it.
# Examples are already wrapped, so any exceptions they throw are caught. group = ::Spectator::DSL::Builder.build
# But if an exception occurs outside an example, Runner.new(group).run
# it's likely the fault of the test framework (Spectator). rescue ex
# So we display a helpful error that could be reported and return non-zero. # Catch all unhandled exceptions here.
puts # Examples are already wrapped, so any exceptions they throw are caught.
puts "Encountered an unexpected error in framework" # But if an exception occurs outside an example,
puts ex.message # it's likely the fault of the test framework (Spectator).
puts ex.backtrace.join("\n") # So we display a helpful error that could be reported and return non-zero.
exit(1) puts
end puts "Encountered an unexpected error in framework"
puts ex.message
puts ex.backtrace.join("\n")
exit(1)
end end
end end