2003-10-08 09:21:28 -04:00
|
|
|
require 'optparse'
|
|
|
|
|
|
|
|
require 'test/unit/ui/console/testrunner'
|
|
|
|
|
|
|
|
module Test
|
|
|
|
module Unit
|
|
|
|
class AutoRunner
|
2003-12-02 07:31:44 -05:00
|
|
|
def self.run(current_file=nil, default_dir=nil, argv=ARGV, &block)
|
2003-10-08 09:21:28 -04:00
|
|
|
if(!current_file || current_file == $0)
|
|
|
|
r = new(!current_file, &block)
|
2003-12-02 07:31:44 -05:00
|
|
|
if !r.process_args(argv) && default_dir
|
|
|
|
r.to_run << default_dir
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
|
|
|
r.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RUNNERS = {
|
|
|
|
:console => proc do |r|
|
2003-12-02 07:31:44 -05:00
|
|
|
Test::Unit::UI::Console::TestRunner.run(r.suite, r.output_level)
|
2003-10-08 09:21:28 -04:00
|
|
|
end,
|
|
|
|
:gtk => proc do |r|
|
|
|
|
require 'test/unit/ui/gtk/testrunner'
|
|
|
|
Test::Unit::UI::GTK::TestRunner.run(r.suite)
|
|
|
|
end,
|
2003-11-16 19:23:40 -05:00
|
|
|
:gtk2 => proc do |r|
|
|
|
|
require 'test/unit/ui/gtk2/testrunner'
|
|
|
|
Test::Unit::UI::GTK2::TestRunner.run(r.suite)
|
|
|
|
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'
|
|
|
|
Test::Unit::UI::Fox::TestRunner.run(r.suite)
|
|
|
|
end,
|
2003-11-06 02:22:39 -05:00
|
|
|
:tk => proc do |r|
|
|
|
|
require 'test/unit/ui/tk/testrunner'
|
|
|
|
Test::Unit::UI::Tk::TestRunner.run(r.suite)
|
|
|
|
end,
|
2003-10-08 09:21:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
OUTPUT_LEVELS = {
|
|
|
|
:silent => UI::Console::TestRunner::SILENT,
|
|
|
|
:progress => UI::Console::TestRunner::PROGRESS_ONLY,
|
|
|
|
:normal => UI::Console::TestRunner::NORMAL,
|
|
|
|
:verbose => UI::Console::TestRunner::VERBOSE,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
c.pattern = r.pattern if(r.pattern)
|
|
|
|
c.collect(*(r.to_run.empty? ? ['.'] : r.to_run))
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
attr_reader :suite
|
|
|
|
attr_accessor :output_level, :filters, :to_run, :pattern
|
|
|
|
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-02 07:31:44 -05:00
|
|
|
@output_level = Test::Unit::UI::Console::TestRunner::NORMAL
|
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
|
|
|
|
@to_run.concat options.parse!(args)
|
|
|
|
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
|
|
|
|
o.on('-r', '--runner=RUNNER', RUNNERS.keys,
|
|
|
|
"Use the given RUNNER.",
|
|
|
|
"(" + keyword_display(RUNNERS.keys) + ")") do |r|
|
|
|
|
@runner = RUNNERS[r]
|
|
|
|
end
|
2003-10-08 09:21:28 -04:00
|
|
|
|
2003-12-02 07:31:44 -05:00
|
|
|
if(@standalone)
|
|
|
|
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
|
|
|
|
|
2003-12-02 07:31:44 -05:00
|
|
|
o.on('-p', '--pattern=PATTERN', String,
|
|
|
|
"Match files to collect against PATTERN.") do |e|
|
|
|
|
@pattern = Regexp.new(e.sub(%r{\A/(.*)/\Z}m, '\\1'))
|
2003-10-08 09:21:28 -04: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
|
|
|
|
2003-12-02 07:31:44 -05:00
|
|
|
o.on('-v', '--verbose=[LEVEL]', OUTPUT_LEVELS.keys,
|
|
|
|
"Set the output level (default is verbose).",
|
|
|
|
"(" + keyword_display(OUTPUT_LEVELS.keys) + ")") do |l|
|
|
|
|
@output_level = (l ? OUTPUT_LEVELS[l] : OUTPUT_LEVELS[:verbose])
|
|
|
|
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-11-16 19:41:40 -05:00
|
|
|
array.collect{|e| e.to_s.sub(/^(.)([A-Za-z]+)(?=\w*$)/, '\\1[\\2]')}.sort.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
|
|
|
|
result.passed?
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|