2004-12-18 21:01:59 -05:00
|
|
|
require 'test/unit'
|
2003-12-08 08:48:14 -05:00
|
|
|
require 'test/unit/ui/testrunnerutilities'
|
2003-10-08 09:21:28 -04:00
|
|
|
require 'optparse'
|
|
|
|
|
|
|
|
module Test
|
|
|
|
module Unit
|
|
|
|
class AutoRunner
|
2005-02-16 23:50:49 -05:00
|
|
|
def self.run(force_standalone=false, default_dir=nil, argv=ARGV, &block)
|
|
|
|
r = new(force_standalone || standalone?, &block)
|
2006-10-01 12:25:37 -04:00
|
|
|
r.base = default_dir
|
|
|
|
r.process_args(argv)
|
2005-02-16 23:50:49 -05:00
|
|
|
r.run
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.standalone?
|
|
|
|
return false unless("-e" == $0)
|
2008-07-04 09:19:24 -04:00
|
|
|
TestCase::DECENDANT_CLASSES.empty?
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
RUNNERS = {
|
|
|
|
:console => proc do |r|
|
2003-12-08 08:48:14 -05:00
|
|
|
require 'test/unit/ui/console/testrunner'
|
|
|
|
Test::Unit::UI::Console::TestRunner
|
2003-10-08 09:21:28 -04:00
|
|
|
end,
|
|
|
|
:gtk => proc do |r|
|
|
|
|
require 'test/unit/ui/gtk/testrunner'
|
2003-12-08 08:48:14 -05:00
|
|
|
Test::Unit::UI::GTK::TestRunner
|
2003-10-08 09:21:28 -04:00
|
|
|
end,
|
2003-11-16 19:23:40 -05:00
|
|
|
:gtk2 => proc do |r|
|
|
|
|
require 'test/unit/ui/gtk2/testrunner'
|
2003-12-08 08:48:14 -05:00
|
|
|
Test::Unit::UI::GTK2::TestRunner
|
2003-11-16 19:23:40 -05:00
|
|
|
end,
|
2003-10-29 20:17:40 -05:00
|
|
|
:fox => proc do |r|
|
2003-10-08 09:21:28 -04:00
|
|
|
require 'test/unit/ui/fox/testrunner'
|
2003-12-08 08:48:14 -05:00
|
|
|
Test::Unit::UI::Fox::TestRunner
|
2003-10-08 09:21:28 -04:00
|
|
|
end,
|
2003-11-06 02:22:39 -05:00
|
|
|
:tk => proc do |r|
|
|
|
|
require 'test/unit/ui/tk/testrunner'
|
2003-12-08 08:48:14 -05:00
|
|
|
Test::Unit::UI::Tk::TestRunner
|
2003-11-06 02:22:39 -05:00
|
|
|
end,
|
2003-10-08 09:21:28 -04:00
|
|
|
}
|
|
|
|
|
2003-12-24 00:08:04 -05:00
|
|
|
OUTPUT_LEVELS = [
|
|
|
|
[:silent, UI::SILENT],
|
|
|
|
[:progress, UI::PROGRESS_ONLY],
|
|
|
|
[:normal, UI::NORMAL],
|
|
|
|
[:verbose, UI::VERBOSE],
|
|
|
|
]
|
2003-10-08 09:21:28 -04:00
|
|
|
|
|
|
|
COLLECTORS = {
|
|
|
|
:objectspace => proc do |r|
|
|
|
|
require 'test/unit/collector/objectspace'
|
|
|
|
c = Collector::ObjectSpace.new
|
|
|
|
c.filter = r.filters
|
|
|
|
c.collect($0.sub(/\.rb\Z/, ''))
|
|
|
|
end,
|
|
|
|
:dir => proc do |r|
|
|
|
|
require 'test/unit/collector/dir'
|
|
|
|
c = Collector::Dir.new
|
|
|
|
c.filter = r.filters
|
2004-12-18 21:01:59 -05:00
|
|
|
c.pattern.concat(r.pattern) if(r.pattern)
|
|
|
|
c.exclude.concat(r.exclude) if(r.exclude)
|
2006-10-01 12:25:37 -04:00
|
|
|
c.base = r.base
|
2006-10-02 09:29:07 -04:00
|
|
|
$:.push(r.base) if r.base
|
2003-10-08 09:21:28 -04:00
|
|
|
c.collect(*(r.to_run.empty? ? ['.'] : r.to_run))
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
attr_reader :suite
|
2006-10-01 12:25:37 -04:00
|
|
|
attr_accessor :output_level, :filters, :to_run, :pattern, :exclude, :base, :workdir
|
2003-10-08 09:21:28 -04:00
|
|
|
attr_writer :runner, :collector
|
|
|
|
|
|
|
|
def initialize(standalone)
|
|
|
|
Unit.run = true
|
|
|
|
@standalone = standalone
|
|
|
|
@runner = RUNNERS[:console]
|
|
|
|
@collector = COLLECTORS[(standalone ? :dir : :objectspace)]
|
|
|
|
@filters = []
|
|
|
|
@to_run = []
|
2003-12-08 08:48:14 -05:00
|
|
|
@output_level = UI::NORMAL
|
2007-08-24 05:57:53 -04:00
|
|
|
@workdir = false
|
2003-10-08 09:21:28 -04:00
|
|
|
yield(self) if(block_given?)
|
|
|
|
end
|
|
|
|
|
2003-12-02 07:31:44 -05:00
|
|
|
def process_args(args = ARGV)
|
|
|
|
begin
|
2006-01-22 09:28:02 -05:00
|
|
|
options.order!(args) {|arg| @to_run << arg}
|
2003-12-02 07:31:44 -05:00
|
|
|
rescue OptionParser::ParseError => e
|
|
|
|
puts e
|
|
|
|
puts options
|
|
|
|
$! = nil
|
|
|
|
abort
|
|
|
|
else
|
|
|
|
@filters << proc{false} unless(@filters.empty?)
|
|
|
|
end
|
|
|
|
not @to_run.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def options
|
|
|
|
@options ||= OptionParser.new do |o|
|
|
|
|
o.banner = "Test::Unit automatic runner."
|
|
|
|
o.banner << "\nUsage: #{$0} [options] [-- untouched arguments]"
|
|
|
|
|
|
|
|
o.on
|
2003-12-24 00:08:04 -05:00
|
|
|
o.on('-r', '--runner=RUNNER', RUNNERS,
|
2003-12-02 07:31:44 -05:00
|
|
|
"Use the given RUNNER.",
|
2003-12-24 00:08:04 -05:00
|
|
|
"(" + keyword_display(RUNNERS) + ")") do |r|
|
|
|
|
@runner = r
|
2003-12-02 07:31:44 -05:00
|
|
|
end
|
2003-10-08 09:21:28 -04:00
|
|
|
|
2003-12-02 07:31:44 -05:00
|
|
|
if(@standalone)
|
2006-10-02 09:29:07 -04:00
|
|
|
o.on('-b', '--basedir=DIR', "Base directory of test suites.") do |b|
|
2006-10-01 12:25:37 -04:00
|
|
|
@base = b
|
|
|
|
end
|
|
|
|
|
|
|
|
o.on('-w', '--workdir=DIR', "Working directory to run tests.") do |w|
|
|
|
|
@workdir = w
|
|
|
|
end
|
|
|
|
|
2003-12-02 07:31:44 -05:00
|
|
|
o.on('-a', '--add=TORUN', Array,
|
|
|
|
"Add TORUN to the list of things to run;",
|
|
|
|
"can be a file or a directory.") do |a|
|
|
|
|
@to_run.concat(a)
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
|
|
|
|
2004-12-18 21:01:59 -05:00
|
|
|
@pattern = []
|
2004-11-15 21:19:44 -05:00
|
|
|
o.on('-p', '--pattern=PATTERN', Regexp,
|
2003-12-02 07:31:44 -05:00
|
|
|
"Match files to collect against PATTERN.") do |e|
|
2004-12-18 21:01:59 -05:00
|
|
|
@pattern << e
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
2004-11-15 23:39:22 -05:00
|
|
|
|
2004-12-18 21:01:59 -05:00
|
|
|
@exclude = []
|
2004-11-15 23:39:22 -05:00
|
|
|
o.on('-x', '--exclude=PATTERN', Regexp,
|
|
|
|
"Ignore files to collect against PATTERN.") do |e|
|
2004-12-18 21:01:59 -05:00
|
|
|
@exclude << e
|
2004-11-15 23:39:22 -05:00
|
|
|
end
|
2003-12-02 07:31:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
o.on('-n', '--name=NAME', String,
|
|
|
|
"Runs tests matching NAME.",
|
|
|
|
"(patterns may be used).") do |n|
|
|
|
|
n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
|
|
|
|
case n
|
|
|
|
when Regexp
|
|
|
|
@filters << proc{|t| n =~ t.method_name ? true : nil}
|
|
|
|
else
|
|
|
|
@filters << proc{|t| n == t.method_name ? true : nil}
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
2003-12-02 07:31:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
o.on('-t', '--testcase=TESTCASE', String,
|
|
|
|
"Runs tests in TestCases matching TESTCASE.",
|
|
|
|
"(patterns may be used).") do |n|
|
|
|
|
n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
|
|
|
|
case n
|
|
|
|
when Regexp
|
|
|
|
@filters << proc{|t| n =~ t.class.name ? true : nil}
|
|
|
|
else
|
|
|
|
@filters << proc{|t| n == t.class.name ? true : nil}
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
2003-12-02 07:31:44 -05:00
|
|
|
end
|
2003-10-08 09:21:28 -04:00
|
|
|
|
2006-10-01 12:25:37 -04:00
|
|
|
o.on('-I', "--load-path=DIR[#{File::PATH_SEPARATOR}DIR...]",
|
|
|
|
"Appends directory list to $LOAD_PATH.") do |dirs|
|
|
|
|
$LOAD_PATH.concat(dirs.split(File::PATH_SEPARATOR))
|
|
|
|
end
|
|
|
|
|
2003-12-24 00:08:04 -05:00
|
|
|
o.on('-v', '--verbose=[LEVEL]', OUTPUT_LEVELS,
|
2003-12-02 07:31:44 -05:00
|
|
|
"Set the output level (default is verbose).",
|
2003-12-24 00:08:04 -05:00
|
|
|
"(" + keyword_display(OUTPUT_LEVELS) + ")") do |l|
|
|
|
|
@output_level = l || UI::VERBOSE
|
2003-12-02 07:31:44 -05:00
|
|
|
end
|
2003-10-08 09:21:28 -04:00
|
|
|
|
2003-12-02 07:31:44 -05:00
|
|
|
o.on('--',
|
|
|
|
"Stop processing options so that the",
|
|
|
|
"remaining options will be passed to the",
|
|
|
|
"test."){o.terminate}
|
2003-10-08 09:21:28 -04:00
|
|
|
|
2003-12-02 07:31:44 -05:00
|
|
|
o.on('-h', '--help', 'Display this help.'){puts o; exit}
|
|
|
|
|
|
|
|
o.on_tail
|
|
|
|
o.on_tail('Deprecated options:')
|
|
|
|
|
|
|
|
o.on_tail('--console', 'Console runner (use --runner).') do
|
|
|
|
warn("Deprecated option (--console).")
|
|
|
|
@runner = RUNNERS[:console]
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
2003-12-02 07:31:44 -05:00
|
|
|
|
|
|
|
o.on_tail('--gtk', 'GTK runner (use --runner).') do
|
|
|
|
warn("Deprecated option (--gtk).")
|
|
|
|
@runner = RUNNERS[:gtk]
|
|
|
|
end
|
|
|
|
|
|
|
|
o.on_tail('--fox', 'Fox runner (use --runner).') do
|
|
|
|
warn("Deprecated option (--fox).")
|
|
|
|
@runner = RUNNERS[:fox]
|
|
|
|
end
|
|
|
|
|
|
|
|
o.on_tail
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def keyword_display(array)
|
2003-12-24 00:08:04 -05:00
|
|
|
list = array.collect {|e, *| e.to_s}
|
|
|
|
Array === array or list.sort!
|
|
|
|
list.collect {|e| e.sub(/^(.)([A-Za-z]+)(?=\w*$)/, '\\1[\\2]')}.join(", ")
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
@suite = @collector[self]
|
2003-12-02 07:31:44 -05:00
|
|
|
result = @runner[self] or return false
|
2006-10-01 12:25:37 -04:00
|
|
|
Dir.chdir(@workdir) if @workdir
|
2003-12-08 08:48:14 -05:00
|
|
|
result.run(@suite, @output_level).passed?
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|