Skip to content

Commit

Permalink
Add -e option to xspec runner.
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviershay committed Jul 29, 2014
1 parent 08bfd17 commit b4599af
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
27 changes: 26 additions & 1 deletion bin/xspec
@@ -1,12 +1,31 @@
#!/usr/bin/env ruby

require 'optparse'

$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)

require 'xspec'

$LOAD_PATH.unshift "spec"
$LOAD_PATH.unshift "lib"

filter = //

parser = OptionParser.new
parser.banner = "Usage: xspec [options] [files]"
parser.separator ""
parser.on('-e FILTER', "Only run specs with full name including FILTER") do |f|
filter = Regexp.new(f)
end
parser.on("-h", "--help", "Show this message") do
$stderr.puts parser
exit
end
parser.separator ""

parser.parse!


files = if ARGV.any? {|x| x.length > 0 }
ARGV
else
Expand All @@ -18,7 +37,13 @@ files.each do |f|
end

if respond_to?(:run!)
exit 1 unless run!
exit 1 unless run!(scheduler: XSpec::Scheduler::Filter.new(
scheduler: __xspec_opts.fetch(:scheduler),
filter: -> uow {
full_name = (uow.parents + [uow]).map(&:name).compact.join(' ')
full_name =~ filter
}
))
else
$stderr.puts "This script can only be used when XSpec.dsl is mixed in to " +
"global scope."
Expand Down
3 changes: 1 addition & 2 deletions doc/api.rb
Expand Up @@ -432,9 +432,8 @@ def run(context, notifier)
#
# XSpec provides the `xspec` script, that can be used to run XSpec files. It is
# not required, but provides a number of niceties:
# * Adds `spec` and `lib` directories to the load path.
# * Loads all files passed as arguments.
# * Exits non-zero if the run fails.
# * Running specific files and specs, use `--help` option for details.
#
# (`autorun!` provides roughly equivalent behaviour.)
#
Expand Down
10 changes: 7 additions & 3 deletions lib/xspec.rb
Expand Up @@ -35,9 +35,13 @@ def __xspec_context
# `run!` is where the magic happens. Typically called at the end of a
# file (or by `autorun!`), this method takes all the data that was
# accumulated by the DSL methods above and runs it through the scheduler.
def run!
notifier = __xspec_opts.fetch(:notifier)
scheduler = __xspec_opts.fetch(:scheduler)
#
# It takes an optional parameter that can be used to override any options
# set in the initial `XSpec.dsl` call.
def run!(overrides = {})
opts = __xspec_opts.merge(overrides)
notifier = opts.fetch(:notifier)
scheduler = opts.fetch(:scheduler)

scheduler.run(__xspec_context, notifier)
end
Expand Down
19 changes: 19 additions & 0 deletions lib/xspec/schedulers.rb
Expand Up @@ -34,6 +34,25 @@ def run(context, notifier)
attr_reader :clock
end

class Filter
def initialize(scheduler:, filter:)
@scheduler = scheduler
@filter = filter
end

def run(context, notifier)
scheduler.run(FilteredContext.new(context, filter), notifier)
end

FilteredContext = Struct.new(:context, :filter) do
def nested_units_of_work
context.nested_units_of_work.select(&filter)
end
end

attr_reader :scheduler, :filter
end

DEFAULT = Serial.new
end
end

0 comments on commit b4599af

Please sign in to comment.