3
3
require 'minitest/unit'
4
4
require 'test/unit/assertions'
5
5
require 'test/unit/testcase'
9
10
TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest'
11
def self.setup_argv(original_argv=ARGV)
15
original_argv = original_argv.dup
16
while arg = original_argv.shift
20
when /\A(-n)(.+)?/, /\A(--name)=?\b(.+)?/
22
minitest_argv << ($2 || original_argv.shift)
24
reject << ($1 || original_argv.shift)
35
f = f.tr(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
37
Dir["#{f}/**/test_*.rb"]
41
raise ArgumentError, "file not found: #{f}"
46
reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
47
files.reject! {|f| reject_pat =~ f }
50
d = File.dirname(path = File.expand_path(f))
26
return if $! # don't run if there was an exception
29
module_function :run_once
33
def initialize(*, &block)
40
@option_parser ||= OptionParser.new
43
def process_args(args = [])
44
return @options if @options
48
setup_options(opts, options)
51
args = @init_hook.call(args, options) if @init_hook
52
non_options(args, options)
53
@help = orig_args.map { |s| s =~ /[\s|&<>$()]/ ? s.inspect : s }.join " "
55
if @options[:parallel]
63
def setup_options(opts, options)
64
opts.separator 'minitest options:'
65
opts.version = MiniTest::Unit::VERSION
67
opts.on '-h', '--help', 'Display this help.' do
72
opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m|
76
opts.on '-v', '--verbose', "Verbose. Show progress processing files." do
77
options[:verbose] = true
78
self.verbose = options[:verbose]
81
opts.on '-n', '--name PATTERN', "Filter test names on pattern." do |a|
85
opts.on '--jobs-status [TYPE]', [:normal, :replace],
86
"Show status of jobs every file; Disabled when --jobs isn't specified." do |type|
87
options[:job_status] = type || :normal
90
opts.on '-j N', '--jobs N', "Allow run tests with N jobs at once" do |a|
92
options[:testing] = true # For testing
93
options[:parallel] = a[1..-1].to_i
95
options[:parallel] = a.to_i
99
opts.on '--no-retry', "Don't retry running testcase when --jobs specified" do
100
options[:no_retry] = true
103
opts.on '--ruby VAL', "Path to ruby; It'll have used at -j option" do |a|
104
options[:ruby] = a.split(/ /).reject(&:empty?)
107
opts.on '-q', '--hide-skip', 'Hide skipped tests' do
108
options[:hide_skip] = true
112
def non_options(files, options)
61
ARGV.replace minitest_argv
116
warn "#{caller(1)[0]}: warning: Parallel running disabled because can't get path to ruby; run specify with --ruby argument"
117
options[:parallel] = nil
119
options[:ruby] ||= RbConfig.ruby
129
@@testfile_prefix = "test"
131
def setup_options(parser, options)
133
parser.on '-b', '--basedir=DIR', 'Base directory of test suites.' do |dir|
134
options[:base_directory] = dir
136
parser.on '-x', '--exclude PATTERN', 'Exclude test files on pattern.' do |pattern|
137
(options[:reject] ||= []) << pattern
141
def non_options(files, options)
142
paths = [options.delete(:base_directory), nil].uniq
143
if reject = options.delete(:reject)
144
reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
147
f = f.tr(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
148
((paths if /\A\.\.?(?:\z|\/)/ !~ f) || [nil]).any? do |prefix|
150
path = f.empty? ? prefix : "#{prefix}/#{f}"
155
if !(match = Dir["#{path}/**/#{@@testfile_prefix}_*.rb"]).empty?
158
n[(prefix.length+1)..-1] if prefix
163
elsif !reject or reject_pat !~ f and File.exist? path
167
raise ArgumentError, "file not found: #{f}"
170
super(files, options)
174
module LoadPathOption
177
def setup_options(parser, options)
179
parser.on '-Idirectory', 'Add library load path' do |dirs|
180
dirs.split(':').each { |d| $LOAD_PATH.unshift d }
185
module GCStressOption
186
def setup_options(parser, options)
188
parser.on '--[no-]gc-stress', 'Set GC.stress as true' do |flag|
189
options[:gc_stress] = flag
193
def non_options(files, options)
194
if options.delete(:gc_stress)
195
MiniTest::Unit::TestCase.class_eval do
196
oldrun = instance_method(:run)
197
define_method(:run) do |runner|
199
gc_stress, GC.stress = GC.stress, true
200
oldrun.bind(self).call(runner)
202
GC.stress = gc_stress
212
def non_options(files, options)
213
return false if !super
216
d = File.dirname(path = File.expand_path(f))
221
require path unless options[:parallel]
231
class Runner < MiniTest::Unit
232
include Test::Unit::Options
233
include Test::Unit::GlobOption
234
include Test::Unit::LoadPathOption
235
include Test::Unit::GCStressOption
236
include Test::Unit::RunCount
239
def self.launch(ruby,args=[])
240
io = IO.popen([*ruby,
241
"#{File.dirname(__FILE__)}/unit/parallel.rb",
243
new(io, io.pid, :waiting)
246
def initialize(io, pid, status)
261
@file = File.basename(task).gsub(/\.rb/,"")
264
puts "loadpath #{[Marshal.dump($:-@loadpath)].pack("m").gsub("\n","")}"
266
puts "run #{task} #{type}"
271
raise unless ["stream closed","closed stream"].include? $!.message
283
res = (@status == :quit) ? @io.read : @io.gets
292
def died(*additional)
296
call_hook(:dead,*additional)
303
"#{@pid}:#{@status.to_s.ljust(7)}"
307
attr_reader :io, :pid
308
attr_accessor :status, :file, :real_file, :loadpath
312
def call_hook(id,*additional)
314
@hooks[id].each{|hook| hook[self,additional] }
320
class << self; undef autorun; end
322
@@stop_auto_run = false
325
Test::Unit::RunCount.run_once {
326
exit(Test::Unit::Runner.new.run(ARGV) || true)
327
} unless @@stop_auto_run
328
} unless @@installed_at_exit
329
@@installed_at_exit = true
332
def after_worker_down(worker, e=nil, c=false)
333
return unless @options[:parallel]
337
warn "#{b.shift}: #{e.message} (#{e.class})"
338
STDERR.print b.map{|s| "\tfrom #{s}"}.join("\n")
342
warn "Some worker was crashed. It seems ruby interpreter's bug"
343
warn "or, a bug of test/unit/parallel.rb. try again without -j"
351
return unless @options[:job_status]
352
puts "" unless @options[:verbose]
353
status_line = @workers.map(&:to_s).join(" ")
354
if @options[:job_status] == :replace and $stdout.tty?
359
rescue LoadError, NoMethodError
360
ENV["COLUMNS"].to_i.nonzero? || 80
365
print status_line[0...@terminal_width]
367
@jstr_size = [status_line.size, @terminal_width].min
374
return unless @options[:job_status] == :replace && @jstr_size.nonzero?
375
print "\r"+" "*@jstr_size+"\r"
378
def after_worker_quit(worker)
379
return unless @options[:parallel]
381
@workers.delete(worker)
382
@dead_workers << worker
383
@ios = @workers.map(&:io)
386
def _run_parallel suites, type, result
387
if @options[:parallel] < 1
388
warn "Error: parameter of -j option should be greater than 0."
393
# Require needed things for parallel running
396
@tasks = @files.dup # Array of filenames.
398
@dead_workers = [] # Array of dead workers.
400
shutting_down = false
401
rep = [] # FIXME: more good naming
404
@workers = @options[:parallel].times.map {
405
worker = Worker.launch(@options[:ruby],@args)
406
worker.hook(:dead) do |w,info|
408
after_worker_down w, *info unless info.empty?
414
watchdog = Thread.new do
415
while stat = Process.wait2
416
break if @interrupt # Break when interrupt
418
w = (@workers + @dead_workers).find{|x| pid == x.pid }.dup
420
unless w.status == :quit
422
w.died(nil, !stat.signaled? && stat.exitstatus)
427
@workers_hash = Hash[@workers.map {|w| [w.io,w] }] # out-IO => worker
428
@ios = @workers.map{|w| w.io } # Array of worker IOs
430
while _io = IO.select(@ios)[0]
431
break unless _io.each do |io|
433
worker = @workers_hash[io]
436
worker.status = :running
439
worker.status = :ready
441
break unless @workers.find{|x| x.status == :running }
443
worker.run(@tasks.shift, type)
448
r = Marshal.load($1.unpack("m")[0])
449
result << r[0..1] unless r[0..1] == [nil,nil]
450
rep << {file: worker.real_file,
451
report: r[2], result: r[3], testcase: r[5]}
455
print $1.unpack("m")[0]
456
jobs_status if @options[:job_status] == :replace
458
@warnings << Marshal.load($1.unpack("m")[0])
460
after_worker_down worker, Marshal.load($1.unpack("m")[0])
463
after_worker_quit worker
465
after_worker_down worker
471
rescue Interrupt => e
477
watchdog.kill if watchdog
479
@ios.select!{|x| @workers_hash[x].status == :running }
480
while !@ios.empty? && (__io = IO.select(@ios,[],[],10))
483
worker = @workers_hash[io]
486
r = Marshal.load($1.unpack("m")[0])
487
result << r[0..1] unless r[0..1] == [nil,nil]
488
rep << {file: worker.real_file,
489
report: r[2], result: r[3], testcase: r[5]}
496
@workers.each do |worker|
502
rescue Timeout::Error
507
timeout(0.2*@workers.size) do
510
rescue Timeout::Error
511
@workers.each do |worker|
513
Process.kill(:KILL,worker.pid)
514
rescue Errno::ESRCH; end
518
if @interrupt || @options[:no_retry] || @need_quit
520
report.push(*r[:report])
522
@errors += rep.map{|x| x[:result][0] }.inject(:+)
523
@failures += rep.map{|x| x[:result][1] }.inject(:+)
524
@skips += rep.map{|x| x[:result][2] }.inject(:+)
530
if r[:testcase] && r[:file] && !r[:report].empty?
532
_run_suite(eval(r[:testcase]),type)
534
report.push(*r[:report])
535
@errors += r[:result][0]
536
@failures += r[:result][1]
537
@skips += r[:result][2]
544
@warnings.reject! do |w|
545
r = ary.include?(w[1].message)
549
@warnings.each do |w|
550
warn "#{w[0]}: #{w[1].message} (#{w[1].class})"
557
def _run_suites suites, type
560
if @options[:parallel]
561
_run_parallel suites, type, result
565
result << _run_suite(suite, type)
566
rescue Interrupt => e
572
report.reject!{|r| r.start_with? "Skipped:" } if @options[:hide_skip]
576
# Overriding of MiniTest::Unit#puke
577
def puke klass, meth, e
579
# this overriding is for minitest feature that skip messages are
580
# hidden when not verbose (-v), note this is temporally.
582
when MiniTest::Skip then
584
"Skipped:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
585
when MiniTest::Assertion then
587
"Failure:\n#{meth}(#{klass}) [#{location e}]:\n#{e.message}\n"
590
bt = MiniTest::filter_backtrace(e.backtrace).join "\n "
591
"Error:\n#{meth}(#{klass}):\n#{e.class}: #{e.message}\n #{bt}\n"
599
raise @interrupt if @interrupt
605
class Runner < Test::Unit::Runner
606
include Test::Unit::RequireFiles
609
attr_accessor :to_run, :options
611
def initialize(force_standalone = false, default_dir = nil, argv = ARGV)
612
@runner = Runner.new do |files, options|
613
options[:base_directory] ||= default_dir
614
files << default_dir if files.empty? and default_dir
616
yield self if block_given?
619
Runner.runner = @runner
620
@options = @runner.option_parser
624
def process_args(*args)
625
@runner.process_args(*args)
630
@runner.run(@argv) || true
66
MiniTest::Unit.autorun
640
Test::Unit::Runner.autorun