2008-10-16 09:55:09 -04:00
|
|
|
# test/unit compatibility layer using minitest.
|
2008-10-10 02:15:29 -04:00
|
|
|
|
2008-10-16 09:55:09 -04:00
|
|
|
require 'minitest/unit'
|
2008-12-11 05:40:24 -05:00
|
|
|
require 'test/unit/assertions'
|
|
|
|
require 'test/unit/testcase'
|
2010-07-14 13:44:51 -04:00
|
|
|
require 'optparse'
|
2008-10-10 02:15:29 -04:00
|
|
|
|
2008-10-16 09:55:09 -04:00
|
|
|
module Test
|
2008-10-10 02:15:29 -04:00
|
|
|
module Unit
|
2008-10-16 09:55:09 -04:00
|
|
|
TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest'
|
|
|
|
|
2010-07-16 21:08:04 -04:00
|
|
|
module RunCount
|
|
|
|
@@run_count = 0
|
|
|
|
|
|
|
|
def self.have_run?
|
|
|
|
@@run_count.nonzero?
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(*)
|
|
|
|
@@run_count += 1
|
|
|
|
super
|
|
|
|
end
|
2010-07-17 06:01:49 -04:00
|
|
|
|
|
|
|
def run_once
|
|
|
|
return if have_run?
|
|
|
|
return if $! # don't run if there was an exception
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
module_function :run_once
|
2010-07-16 21:08:04 -04:00
|
|
|
end
|
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
module Options
|
2011-02-11 07:41:58 -05:00
|
|
|
def initialize(*, &block)
|
2010-07-17 06:01:49 -04:00
|
|
|
@init_hook = block
|
|
|
|
super(&nil)
|
|
|
|
end
|
2010-07-14 13:44:51 -04:00
|
|
|
|
2011-02-11 07:41:58 -05:00
|
|
|
def option_parser
|
|
|
|
@option_parser ||= OptionParser.new
|
|
|
|
end
|
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
def process_args(args = [])
|
2011-02-11 07:41:58 -05:00
|
|
|
return @options if @options
|
2010-12-15 06:45:57 -05:00
|
|
|
orig_args = args.dup
|
2010-07-17 06:01:49 -04:00
|
|
|
options = {}
|
2011-02-11 07:41:58 -05:00
|
|
|
opts = option_parser
|
|
|
|
setup_options(opts, options)
|
|
|
|
opts.parse!(args)
|
|
|
|
orig_args -= args
|
2010-07-17 06:01:49 -04:00
|
|
|
args = @init_hook.call(args, options) if @init_hook
|
2011-02-12 10:29:24 -05:00
|
|
|
non_options(args, options)
|
2010-12-15 06:45:57 -05:00
|
|
|
@help = orig_args.map { |s| s =~ /[\s|&<>$()]/ ? s.inspect : s }.join " "
|
2011-02-11 07:41:58 -05:00
|
|
|
@options = options
|
2010-07-17 06:01:49 -04:00
|
|
|
end
|
2010-07-14 13:44:51 -04:00
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
private
|
|
|
|
def setup_options(opts, options)
|
2011-02-11 07:41:58 -05:00
|
|
|
opts.separator 'minitest options:'
|
2010-07-17 06:01:49 -04:00
|
|
|
opts.version = MiniTest::Unit::VERSION
|
|
|
|
|
|
|
|
opts.on '-h', '--help', 'Display this help.' do
|
|
|
|
puts opts
|
|
|
|
exit
|
2010-07-14 13:44:51 -04:00
|
|
|
end
|
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m|
|
|
|
|
options[:seed] = m.to_i
|
2010-07-14 13:44:51 -04:00
|
|
|
end
|
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
opts.on '-v', '--verbose', "Verbose. Show progress processing files." do
|
|
|
|
options[:verbose] = true
|
2010-12-01 00:33:32 -05:00
|
|
|
self.verbose = options[:verbose]
|
2010-07-14 13:44:51 -04:00
|
|
|
end
|
2008-10-16 09:55:09 -04:00
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
opts.on '-n', '--name PATTERN', "Filter test names on pattern." do |a|
|
|
|
|
options[:filter] = a
|
|
|
|
end
|
2008-10-16 09:55:09 -04:00
|
|
|
end
|
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
def non_options(files, options)
|
2011-02-11 07:41:58 -05:00
|
|
|
true
|
2010-07-17 06:01:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module GlobOption
|
|
|
|
include Options
|
|
|
|
|
|
|
|
def setup_options(parser, options)
|
|
|
|
super
|
2011-02-11 07:41:58 -05:00
|
|
|
parser.on '-b', '--basedir=DIR', 'Base directory of test suites.' do |dir|
|
|
|
|
options[:base_directory] = dir
|
|
|
|
end
|
2010-12-14 04:32:36 -05:00
|
|
|
parser.on '-x', '--exclude PATTERN', 'Exclude test files on pattern.' do |pattern|
|
2010-07-17 06:01:49 -04:00
|
|
|
(options[:reject] ||= []) << pattern
|
2008-10-16 09:55:09 -04:00
|
|
|
end
|
2010-07-17 06:01:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def non_options(files, options)
|
2011-02-12 00:41:38 -05:00
|
|
|
paths = [options.delete(:base_directory), nil].uniq
|
2010-07-17 06:01:49 -04:00
|
|
|
if reject = options.delete(:reject)
|
|
|
|
reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
|
2008-10-16 09:55:09 -04:00
|
|
|
end
|
2010-09-16 08:40:40 -04:00
|
|
|
files.map! {|f|
|
|
|
|
f = f.tr(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
|
2011-02-11 07:41:58 -05:00
|
|
|
[*(paths if /\A\.\.?(?:\z|\/)/ !~ f), nil].uniq.any? do |prefix|
|
|
|
|
if prefix
|
|
|
|
path = f.empty? ? prefix : "#{prefix}/#{f}"
|
|
|
|
else
|
|
|
|
next if f.empty?
|
|
|
|
path = f
|
|
|
|
end
|
2010-09-16 08:40:40 -04:00
|
|
|
if !(match = Dir["#{path}/**/test_*.rb"]).empty?
|
|
|
|
if reject
|
|
|
|
match.reject! {|n|
|
|
|
|
n[(prefix.length+1)..-1] if prefix
|
|
|
|
reject_pat =~ n
|
|
|
|
}
|
|
|
|
end
|
|
|
|
break match
|
|
|
|
elsif !reject or reject_pat !~ f and File.exist? path
|
|
|
|
break path
|
|
|
|
end
|
|
|
|
end or
|
|
|
|
raise ArgumentError, "file not found: #{f}"
|
|
|
|
}
|
|
|
|
files.flatten!
|
2010-07-17 06:01:49 -04:00
|
|
|
super(files, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module LoadPathOption
|
|
|
|
include Options
|
2008-10-16 09:55:09 -04:00
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
def setup_options(parser, options)
|
|
|
|
super
|
2010-12-14 04:32:36 -05:00
|
|
|
parser.on '-Idirectory', 'Add library load path' do |dirs|
|
2010-07-17 06:01:49 -04:00
|
|
|
dirs.split(':').each { |d| $LOAD_PATH.unshift d }
|
|
|
|
end
|
|
|
|
end
|
2010-07-16 21:08:04 -04:00
|
|
|
end
|
|
|
|
|
2010-12-01 17:20:23 -05:00
|
|
|
module GCStressOption
|
|
|
|
def setup_options(parser, options)
|
|
|
|
super
|
2010-12-14 04:32:36 -05:00
|
|
|
parser.on '--[no-]gc-stress', 'Set GC.stress as true' do |flag|
|
2010-12-01 17:20:23 -05:00
|
|
|
options[:gc_stress] = flag
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def non_options(files, options)
|
|
|
|
if options.delete(:gc_stress)
|
|
|
|
MiniTest::Unit::TestCase.class_eval do
|
|
|
|
oldrun = instance_method(:run)
|
|
|
|
define_method(:run) do |runner|
|
|
|
|
begin
|
|
|
|
gc_stress, GC.stress = GC.stress, true
|
|
|
|
oldrun.bind(self).call(runner)
|
|
|
|
ensure
|
|
|
|
GC.stress = gc_stress
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-10-23 23:11:20 -04:00
|
|
|
module RequireFiles
|
|
|
|
def non_options(files, options)
|
2011-02-12 10:29:24 -05:00
|
|
|
return false if !super
|
|
|
|
result = false
|
2010-10-23 23:11:20 -04:00
|
|
|
files.each {|f|
|
|
|
|
d = File.dirname(path = File.expand_path(f))
|
|
|
|
unless $:.include? d
|
|
|
|
$: << d
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
require path
|
2011-02-12 10:29:24 -05:00
|
|
|
result = true
|
2011-02-13 20:34:54 -05:00
|
|
|
rescue LoadError
|
|
|
|
puts "#{f}: #{$!}"
|
2010-10-23 23:11:20 -04:00
|
|
|
end
|
|
|
|
}
|
2011-02-12 10:29:24 -05:00
|
|
|
result
|
2010-10-23 23:11:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-12 00:41:02 -05:00
|
|
|
class Runner < MiniTest::Unit
|
2010-07-17 06:01:49 -04:00
|
|
|
include Test::Unit::GlobOption
|
|
|
|
include Test::Unit::LoadPathOption
|
2010-12-01 17:20:23 -05:00
|
|
|
include Test::Unit::GCStressOption
|
2010-10-23 23:11:20 -04:00
|
|
|
include Test::Unit::RunCount
|
|
|
|
|
|
|
|
class << self; undef autorun; end
|
|
|
|
def self.autorun
|
|
|
|
at_exit {
|
|
|
|
Test::Unit::RunCount.run_once {
|
2011-02-12 00:41:02 -05:00
|
|
|
exit(Test::Unit::Runner.new.run(ARGV) || true)
|
2010-10-24 01:13:55 -04:00
|
|
|
}
|
2010-10-23 23:11:20 -04:00
|
|
|
} unless @@installed_at_exit
|
|
|
|
@@installed_at_exit = true
|
|
|
|
end
|
2010-10-24 01:13:55 -04:00
|
|
|
|
2010-12-01 17:15:15 -05:00
|
|
|
def _run_suites suites, type
|
|
|
|
@interrupt = nil
|
|
|
|
result = []
|
|
|
|
suites.each {|suite|
|
|
|
|
begin
|
|
|
|
result << _run_suite(suite, type)
|
|
|
|
rescue Interrupt => e
|
|
|
|
@interrupt = e
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def status(*args)
|
|
|
|
result = super
|
|
|
|
raise @interrupt if @interrupt
|
|
|
|
result
|
2010-10-24 02:16:36 -04:00
|
|
|
end
|
2008-10-16 09:55:09 -04:00
|
|
|
end
|
2011-02-11 07:41:58 -05:00
|
|
|
|
|
|
|
class AutoRunner
|
2011-02-18 16:39:09 -05:00
|
|
|
class Runner < Test::Unit::Runner
|
|
|
|
include Test::Unit::RequireFiles
|
|
|
|
end
|
|
|
|
|
2011-02-11 07:41:58 -05:00
|
|
|
attr_accessor :to_run, :options
|
|
|
|
|
|
|
|
def initialize(force_standalone = false, default_dir = nil, argv = ARGV)
|
|
|
|
@runner = Runner.new do |files, options|
|
|
|
|
options[:base_directory] ||= default_dir
|
2011-02-12 10:29:24 -05:00
|
|
|
files << default_dir if files.empty? and default_dir
|
2011-02-11 07:41:58 -05:00
|
|
|
@to_run = files
|
|
|
|
yield self if block_given?
|
|
|
|
files
|
|
|
|
end
|
|
|
|
@options = @runner.option_parser
|
|
|
|
@argv = argv
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_args(*args)
|
|
|
|
@runner.process_args(*args)
|
2011-02-12 10:29:24 -05:00
|
|
|
!@to_run.empty?
|
2011-02-11 07:41:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
@runner.run(@argv) || true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.run(*args)
|
|
|
|
new(*args).run
|
|
|
|
end
|
|
|
|
end
|
2008-10-10 02:15:29 -04:00
|
|
|
end
|
|
|
|
end
|
2010-07-16 09:09:44 -04:00
|
|
|
|
2011-02-12 00:41:02 -05:00
|
|
|
Test::Unit::Runner.autorun
|