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

View file

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

View file

@ -1,8 +1,10 @@
require "./config_builder"
module Spectator module Spectator
# Interface for all places that configuration can originate. # Interface for all places that configuration can originate.
abstract class ConfigSource abstract class ConfigSource
# Applies the specified configuration to a builder. # Applies the specified configuration to a builder.
# Calling this method from multiple sources builds up the final configuration. # 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
end end

View file

@ -4,6 +4,7 @@
# Including all files with a wildcard would accidentally enable should-syntax. # 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. # 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_builder"
require "./config"
require "./dsl" require "./dsl"