Structure for applying configuration

This commit is contained in:
Michael Miller 2018-12-13 13:50:59 -07:00
parent 9527427b45
commit 23368f6183
6 changed files with 84 additions and 5 deletions

View file

@ -72,11 +72,21 @@ module Spectator
run if autorun?
end
@@config_builder = ConfigBuilder.new
# Provides a means to configure how Spectator will run and report tests.
# A `ConfigBuilder` is yielded to allow changing the configuration.
# NOTE: The configuration set here can be overriden
# with a `.spectator` file and command-line arguments.
def configure : Nil
yield @@config_builder
end
# Builds the tests and runs the framework.
private def self.run
# Build the test suite and run it.
suite = ::Spectator::DSL::Builder.build
Runner.new(suite).run
Runner.new(suite, config).run
rescue ex
# Catch all unhandled exceptions here.
# Examples are already wrapped, so any exceptions they throw are caught.
@ -87,8 +97,40 @@ module Spectator
exit(1)
end
# Processes and builds up a configuration to use for running tests.
private def self.config
# Build up the configuration from various sources.
# The sources that take priority are later in the list.
apply_config_file
apply_command_line_args
@@config_builder.build
end
# Path to the Spectator configuration file.
# The contents of this file should contain command-line arguments.
# Those arguments are automatically applied when Spectator starts.
# Arguments should be placed with one per line.
CONFIG_FILE_PATH = ".spectator"
# Loads configuration arguments from a file.
# The file is expected to be new-line delimited,
# one argument per line.
# The arguments are identical to those
# that would be passed on the command-line.
private def self.apply_config_file(file_path = CONFIG_FILE_PATH) : Nil
return unless File.exists?(file_path)
args = File.read(file_path).lines
CommandLineArgumentsConfigSource.new(args).apply_to(@@config_builder)
end
# Applies configuration options from the command-line arguments
private def self.apply_command_line_args : Nil
CommandLineArgumentsConfigSource.new.apply_to(@@config_builder)
end
# Displays an error message.
private def self.display_error(error)
private def self.display_error(error) : Nil
puts
puts "Encountered an unexpected error in framework"
puts error.message

View file

@ -0,0 +1,16 @@
module Spectator
# Generates configuration from the command-line arguments.
class CommandLineArgumentsConfigSource < ConfigSource
# Creates the configuration source.
# By default, the command-line arguments (ARGV) are used.
# But custom arguments can be passed in.
def initialize(@args : Array(String) = ARGV)
end
# Applies the specified configuration to a builder.
# Calling this method from multiple sources builds up the final configuration.
def apply_to(builder : ConfigBuilder) : Nil
# ...
end
end
end

View file

@ -0,0 +1,9 @@
module Spectator
class ConfigBuilder
property formatter : Formatters::Formatter = Formatters::DefaultFormatter.new
def build
Config.new
end
end
end

View file

@ -0,0 +1,8 @@
module Spectator
# Interface for all places that configuration can originate.
abstract class ConfigSource
# Applies the specified configuration to a builder.
# Calling this method from multiple sources builds up the final configuration.
abstract def apply_to(builder : ConfigBuilder) : Nil
end
end

View file

@ -23,9 +23,13 @@ require "./example_group"
require "./nested_example_group"
require "./root_example_group"
require "./config"
require "./config_builder"
require "./config_source"
require "./command_line_arguments_config_source"
require "./example_failed"
require "./expectation_failed"
require "./config"
require "./test_suite"
require "./test_suite_results"
require "./runner"

View file

@ -1,8 +1,8 @@
module Spectator
# Main driver for executing tests and feeding results to formatters.
class Runner
def initialize(@suite : TestSuite,
@formatter : Formatters::Formatter = Formatters::DefaultFormatter.new)
def initialize(@suite : TestSuite, config : Config)
@formatter = Formatters::DefaultFormatter.new
end
def run : Nil