Re-introduce config options

This commit is contained in:
Michael Miller 2020-10-19 20:19:25 -06:00
parent c36e006c85
commit a0e6d5c4e8
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
4 changed files with 39 additions and 8 deletions

View file

@ -66,7 +66,7 @@ module Spectator
# Builds the tests and runs the framework.
private def run
# Build the spec and run it.
# DSL::Builder.config = config # TODO: Set config.
DSL::Builder.config = config
spec = DSL::Builder.build
spec.run
true
@ -109,11 +109,11 @@ module Spectator
private def 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)
CommandLineArgumentsConfigSource.new(args).apply(@@config_builder)
end
# Applies configuration options from the command-line arguments
private def apply_command_line_args : Nil
CommandLineArgumentsConfigSource.new.apply_to(@@config_builder)
CommandLineArgumentsConfigSource.new.apply(@@config_builder)
end
end

View file

@ -1,9 +1,17 @@
require "option_parser"
require "./config_source"
require "./formatting"
require "./line_example_filter"
require "./name_example_filter"
require "./source"
require "./source_example_filter"
module Spectator
# Generates configuration from the command-line arguments.
class CommandLineArgumentsConfigSource < ConfigSource
# Logger for this class.
Log = Spectator::Log.for("config")
# Creates the configuration source.
# By default, the command-line arguments (ARGV) are used.
# But custom arguments can be passed in.
@ -12,7 +20,7 @@ module Spectator
# Applies the specified configuration to a builder.
# Calling this method from multiple sources builds up the final configuration.
def apply_to(builder : ConfigBuilder) : Nil
def apply(builder : ConfigBuilder) : Nil
OptionParser.parse(@args) do |parser|
control_parser_options(parser, builder)
filter_parser_options(parser, builder)
@ -33,6 +41,7 @@ module Spectator
# Adds the fail-fast option to the parser.
private def fail_fast_option(parser, builder)
parser.on("-f", "--fail-fast", "Stop testing on first failure") do
Log.debug { "Enabling fail-fast (-f)" }
builder.fail_fast
end
end
@ -40,6 +49,7 @@ module Spectator
# Adds the fail-blank option to the parser.
private def fail_blank_option(parser, builder)
parser.on("-b", "--fail-blank", "Fail if there are no examples") do
Log.debug { "Enabling fail-blank (-b)" }
builder.fail_blank
end
end
@ -47,6 +57,7 @@ module Spectator
# Adds the dry-run option to the parser.
private def dry_run_option(parser, builder)
parser.on("-d", "--dry-run", "Don't run any tests, output what would have run") do
Log.debug { "Enabling dry-run (-d)" }
builder.dry_run
end
end
@ -54,6 +65,7 @@ module Spectator
# Adds the randomize examples option to the parser.
private def random_option(parser, builder)
parser.on("-r", "--rand", "Randomize the execution order of tests") do
Log.debug { "Randomizing test order (-r)" }
builder.randomize
end
end
@ -61,8 +73,9 @@ module Spectator
# Adds the random seed option to the parser.
private def seed_option(parser, builder)
parser.on("--seed INTEGER", "Set the seed for the random number generator (implies -r)") do |seed|
Log.debug { "Randomizing test order and setting RNG seed to #{seed}" }
builder.randomize
builder.seed = seed.to_u64
builder.random_seed = seed.to_u64
end
end
@ -71,11 +84,17 @@ module Spectator
parser.on("--order ORDER", "Set the test execution order. ORDER should be one of: defined, rand, or rand:SEED") do |method|
case method.downcase
when "defined"
Log.debug { "Disabling randomized tests (--order defined)" }
builder.randomize = false
when /^rand/
builder.randomize
parts = method.split(':', 2)
builder.seed = parts[1].to_u64 if parts.size > 1
if (seed = parts[1]?)
Log.debug { "Randomizing test order and setting RNG seed to #{seed} (--order rand:#{seed})" }
builder.random_seed = seed.to_u64
else
Log.debug { "Randomizing test order (--order rand)" }
end
else
nil
end
@ -92,6 +111,7 @@ module Spectator
# Adds the example filter option to the parser.
private def example_option(parser, builder)
parser.on("-e", "--example STRING", "Run examples whose full nested names include STRING") do |pattern|
Log.debug { "Filtering for examples named '#{pattern}' (-e '#{pattern}')" }
filter = NameExampleFilter.new(pattern)
builder.add_example_filter(filter)
end
@ -100,6 +120,7 @@ module Spectator
# Adds the line filter option to the parser.
private def line_option(parser, builder)
parser.on("-l", "--line LINE", "Run examples whose line matches LINE") do |line|
Log.debug { "Filtering for examples on line #{line} (-l #{line})" }
filter = LineExampleFilter.new(line.to_i)
builder.add_example_filter(filter)
end
@ -108,6 +129,7 @@ module Spectator
# Adds the location filter option to the parser.
private def location_option(parser, builder)
parser.on("--location FILE:LINE", "Run the example at line 'LINE' in the file 'FILE', multiple allowed") do |location|
Log.debug { "Filtering for examples at #{location} (--location '#{location}')" }
source = Source.parse(location)
filter = SourceExampleFilter.new(source)
builder.add_example_filter(filter)
@ -128,6 +150,7 @@ module Spectator
# Adds the verbose output option to the parser.
private def verbose_option(parser, builder)
parser.on("-v", "--verbose", "Verbose output using document formatter") do
Log.debug { "Setting output format to document (-v)" }
builder.formatter = Formatting::DocumentFormatter.new
end
end
@ -143,6 +166,7 @@ module Spectator
# Adds the profile output option to the parser.
private def profile_option(parser, builder)
parser.on("-p", "--profile", "Display the 10 slowest specs") do
Log.debug { "Enabling timing information (-p)" }
builder.profile
end
end
@ -150,6 +174,7 @@ module Spectator
# Adds the JSON output option to the parser.
private def json_option(parser, builder)
parser.on("--json", "Generate JSON output") do
Log.debug { "Setting output format to JSON (--json)" }
builder.formatter = Formatting::JsonFormatter.new
end
end
@ -157,6 +182,7 @@ module Spectator
# Adds the TAP output option to the parser.
private def tap_option(parser, builder)
parser.on("--tap", "Generate TAP output (Test Anything Protocol)") do
Log.debug { "Setting output format to TAP (--tap)" }
builder.formatter = Formatting::TAPFormatter.new
end
end
@ -164,6 +190,7 @@ module Spectator
# Adds the JUnit output option to the parser.
private def junit_option(parser, builder)
parser.on("--junit_output OUTPUT_DIR", "Generate JUnit XML output") do |output_dir|
Log.debug { "Setting output format to JUnit XML (--junit_output '#{output_dir}')" }
formatter = Formatting::JUnitFormatter.new(output_dir)
builder.add_formatter(formatter)
end
@ -172,6 +199,7 @@ module Spectator
# Adds the "no color" output option to the parser.
private def no_color_option(parser, builder)
parser.on("--no-color", "Disable colored output") do
Log.debug { "Disabling color output (--no-color)" }
Colorize.enabled = false
end
end

View file

@ -1,8 +1,10 @@
require "./config_builder"
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
abstract def apply(builder : ConfigBuilder) : Nil
end
end

View file

@ -4,6 +4,7 @@
# Including all files with a wildcard would accidentally enable should-syntax.
# Unfortunately, that leads to the existence of this file to include everything but that file.
require "./config"
require "./command_line_arguments_config_source"
require "./config_builder"
require "./config"
require "./dsl"