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 01:58:39 -04:00
|
|
|
@@installed_at_exit = true
|
|
|
|
@@run_count = 0
|
|
|
|
|
|
|
|
RunCount = Module.new do
|
|
|
|
def run(*)
|
|
|
|
@@run_count += 1
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.new(*)
|
|
|
|
super.extend(RunCount)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.setup_argv(original_argv=::ARGV)
|
2008-10-19 11:59:35 -04:00
|
|
|
minitest_argv = []
|
|
|
|
files = []
|
2008-10-16 09:55:09 -04:00
|
|
|
reject = []
|
|
|
|
original_argv = original_argv.dup
|
2010-07-14 13:44:51 -04:00
|
|
|
OptionParser.new do |parser|
|
|
|
|
parser.default_argv = original_argv
|
|
|
|
|
|
|
|
parser.on '-v', '--verbose' do |v|
|
|
|
|
minitest_argv << '-v' if v
|
2008-10-16 09:55:09 -04:00
|
|
|
end
|
2010-07-14 13:44:51 -04:00
|
|
|
|
|
|
|
parser.on '-n', '--name TESTNAME' do |name|
|
|
|
|
minitest_argv << '-n'
|
|
|
|
minitest_argv << name
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on '-x', '--exclude PATTERN' do |pattern|
|
|
|
|
reject << pattern
|
|
|
|
end
|
|
|
|
|
|
|
|
parser.on '-Idirectory' do |dirs|
|
|
|
|
dirs.split(':').each { |d| $LOAD_PATH.unshift d }
|
|
|
|
end
|
|
|
|
end.parse!
|
|
|
|
files = original_argv
|
2008-10-16 09:55:09 -04:00
|
|
|
|
2008-10-19 11:59:35 -04:00
|
|
|
if block_given?
|
|
|
|
files = yield files
|
2008-10-16 09:55:09 -04:00
|
|
|
end
|
|
|
|
|
2008-10-19 11:59:35 -04:00
|
|
|
files.map! {|f|
|
2009-08-04 14:28:49 -04:00
|
|
|
f = f.tr(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
|
2008-10-19 11:59:35 -04:00
|
|
|
if File.directory? f
|
|
|
|
Dir["#{f}/**/test_*.rb"]
|
|
|
|
elsif File.file? f
|
|
|
|
f
|
|
|
|
else
|
|
|
|
raise ArgumentError, "file not found: #{f}"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
files.flatten!
|
|
|
|
|
2008-10-16 09:55:09 -04:00
|
|
|
reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
|
|
|
|
files.reject! {|f| reject_pat =~ f }
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-16 09:55:09 -04:00
|
|
|
files.each {|f|
|
2009-08-04 14:28:49 -04:00
|
|
|
d = File.dirname(path = File.expand_path(f))
|
2008-10-16 09:55:09 -04:00
|
|
|
unless $:.include? d
|
|
|
|
$: << d
|
|
|
|
end
|
|
|
|
begin
|
2009-08-04 14:28:49 -04:00
|
|
|
require path
|
2008-10-16 09:55:09 -04:00
|
|
|
rescue LoadError
|
2008-10-18 00:41:17 -04:00
|
|
|
puts "#{f}: #{$!}"
|
2008-10-16 09:55:09 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2010-07-16 01:58:39 -04:00
|
|
|
at_exit {
|
|
|
|
next if @@run_count.nonzero?
|
|
|
|
next if $! # don't run if there was an exception
|
|
|
|
exit false unless run(minitest_argv)
|
|
|
|
} unless @@installed_at_exit
|
|
|
|
@@installed_at_exit = true
|
|
|
|
|
|
|
|
minitest_argv
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.run(args)
|
|
|
|
exit_code = MiniTest::Unit.new.run(args)
|
|
|
|
!exit_code || exit_code == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.start(argv=::ARGV, &block)
|
|
|
|
run(setup_argv(argv, &block))
|
2008-10-16 09:55:09 -04:00
|
|
|
end
|
2008-10-10 02:15:29 -04:00
|
|
|
end
|
|
|
|
end
|