diff --git a/src/spectator.cr b/src/spectator.cr index 5b18701..19538fe 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -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 diff --git a/src/spectator/command_line_arguments_config_source.cr b/src/spectator/command_line_arguments_config_source.cr new file mode 100644 index 0000000..40845d5 --- /dev/null +++ b/src/spectator/command_line_arguments_config_source.cr @@ -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 diff --git a/src/spectator/config_builder.cr b/src/spectator/config_builder.cr new file mode 100644 index 0000000..f526934 --- /dev/null +++ b/src/spectator/config_builder.cr @@ -0,0 +1,9 @@ +module Spectator + class ConfigBuilder + property formatter : Formatters::Formatter = Formatters::DefaultFormatter.new + + def build + Config.new + end + end +end diff --git a/src/spectator/config_source.cr b/src/spectator/config_source.cr new file mode 100644 index 0000000..40a243f --- /dev/null +++ b/src/spectator/config_source.cr @@ -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 diff --git a/src/spectator/includes.cr b/src/spectator/includes.cr index c7cca55..4d4a455 100644 --- a/src/spectator/includes.cr +++ b/src/spectator/includes.cr @@ -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" diff --git a/src/spectator/runner.cr b/src/spectator/runner.cr index 569a880..2eec367 100644 --- a/src/spectator/runner.cr +++ b/src/spectator/runner.cr @@ -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