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
|
|
|
|
def initialize(&block)
|
|
|
|
@init_hook = block
|
|
|
|
super(&nil)
|
|
|
|
end
|
2010-07-14 13:44:51 -04:00
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
def process_args(args = [])
|
|
|
|
options = {}
|
|
|
|
OptionParser.new do |opts|
|
|
|
|
setup_options(opts, options)
|
|
|
|
opts.parse!(args)
|
2008-10-16 09:55:09 -04:00
|
|
|
end
|
2010-07-17 06:01:49 -04:00
|
|
|
args = @init_hook.call(args, options) if @init_hook
|
|
|
|
non_options(args, options)
|
|
|
|
options
|
|
|
|
end
|
2010-07-14 13:44:51 -04:00
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
private
|
|
|
|
def setup_options(opts, options)
|
|
|
|
opts.banner = 'minitest options:'
|
|
|
|
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-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)
|
|
|
|
files.each {|f|
|
|
|
|
d = File.dirname(path = File.expand_path(f))
|
|
|
|
unless $:.include? d
|
|
|
|
$: << d
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
require path
|
|
|
|
rescue LoadError
|
|
|
|
puts "#{f}: #{$!}"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module GlobOption
|
|
|
|
include Options
|
|
|
|
|
|
|
|
def setup_options(parser, options)
|
|
|
|
super
|
|
|
|
parser.on '-x', '--exclude PATTERN' do |pattern|
|
|
|
|
(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)
|
2010-09-16 08:40:40 -04:00
|
|
|
paths = [options.delete(:base_directory), nil].compact
|
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
|
|
|
|
[*paths, nil].any? do |prefix|
|
|
|
|
path = prefix ? "#{prefix}/#{f}" : f
|
|
|
|
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
|
|
|
|
parser.on '-Idirectory' do |dirs|
|
|
|
|
dirs.split(':').each { |d| $LOAD_PATH.unshift d }
|
|
|
|
end
|
|
|
|
end
|
2010-07-16 21:08:04 -04:00
|
|
|
end
|
|
|
|
|
2010-09-16 08:40:40 -04:00
|
|
|
def self.new(*args, &block)
|
|
|
|
Mini.new(*args, &block)
|
2010-07-16 21:08:04 -04:00
|
|
|
end
|
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
class Mini < MiniTest::Unit
|
|
|
|
include Test::Unit::GlobOption
|
|
|
|
include Test::Unit::LoadPathOption
|
2008-10-16 09:55:09 -04:00
|
|
|
end
|
2008-10-10 02:15:29 -04:00
|
|
|
end
|
|
|
|
end
|
2010-07-16 09:09:44 -04:00
|
|
|
|
2010-07-16 21:08:04 -04:00
|
|
|
class MiniTest::Unit
|
2010-08-13 03:12:18 -04:00
|
|
|
def self.new(*args, &block)
|
|
|
|
obj = allocate
|
2010-07-17 06:01:49 -04:00
|
|
|
.extend(Test::Unit::RunCount)
|
|
|
|
.extend(Test::Unit::Options)
|
2010-08-13 03:12:18 -04:00
|
|
|
obj.__send__(:initialize, *args, &block)
|
|
|
|
obj
|
2010-07-16 21:08:04 -04:00
|
|
|
end
|
|
|
|
|
2010-07-17 06:01:49 -04:00
|
|
|
class << self; undef autorun; end
|
|
|
|
def self.autorun
|
|
|
|
at_exit {
|
2010-08-13 03:12:18 -04:00
|
|
|
Test::Unit::RunCount.run_once {
|
|
|
|
exit(Test::Unit::Mini.new.run(ARGV) || true)
|
|
|
|
}
|
2010-07-17 06:01:49 -04:00
|
|
|
} unless @@installed_at_exit
|
2010-07-16 21:08:04 -04:00
|
|
|
@@installed_at_exit = true
|
|
|
|
end
|
|
|
|
end
|
2010-07-17 06:01:49 -04:00
|
|
|
|
|
|
|
MiniTest::Unit.autorun
|