Class: Test::Unit::Runner

Inherits:
MiniTest::Unit
  • Object
show all
Includes:
GCStressOption, GlobOption, LoadPathOption, Options, RunCount
Defined in:
lib/test/unit.rb

Direct Known Subclasses

AutoRunner::Runner, Worker

Defined Under Namespace

Classes: Worker

Constant Summary collapse

@@stop_auto_run =
false

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RunCount

have_run?, run_once

Methods included from GCStressOption

#non_options, #setup_options

Methods included from LoadPathOption

#setup_options

Methods included from GlobOption

#non_options, #setup_options

Methods included from Options

#option_parser, #process_args

Constructor Details

#initializeRunner

:nodoc:



762
763
764
765
# File 'lib/test/unit.rb', line 762

def initialize # :nodoc:
  super
  @tty = $stdout.tty?
end

Class Method Details

.autorunObject



363
364
365
366
367
368
369
370
# File 'lib/test/unit.rb', line 363

def self.autorun
  at_exit {
    Test::Unit::RunCount.run_once {
      exit(Test::Unit::Runner.new.run(ARGV) || true)
    } unless @@stop_auto_run
  } unless @@installed_at_exit
  @@installed_at_exit = true
end

Instance Method Details

#_prepare_run(suites, type) ⇒ Object



676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# File 'lib/test/unit.rb', line 676

def _prepare_run(suites, type)
  options[:job_status] ||= :replace if @tty && !@verbose
  case options[:color]
  when :always
    color = true
  when :auto, nil
    color = @options[:job_status] == :replace && /dumb/ !~ ENV["TERM"]
  else
    color = false
  end
  if color
    # dircolors-like style
    colors = (colors = ENV['TEST_COLORS']) ? Hash[colors.scan(/(\w+)=([^:]*)/)] : {}
    @passed_color = "\e[#{colors["pass"] || "32"}m"
    @failed_color = "\e[#{colors["fail"] || "31"}m"
    @skipped_color = "\e[#{colors["skip"] || "33"}m"
    @reset_color = "\e[m"
  else
    @passed_color = @failed_color = @skipped_color = @reset_color = ""
  end
  if color or @options[:job_status] == :replace
    @verbose = !options[:parallel]
    @output = StatusLineOutput.new(self)
  end
  if /\A\/(.*)\/\z/ =~ (filter = options[:filter])
    filter = Regexp.new($1)
  end
  type = "#{type}_methods"
  total = if filter
            suites.inject(0) {|n, suite| n + suite.send(type).grep(filter).size}
          else
            suites.inject(0) {|n, suite| n + suite.send(type).size}
          end
  @test_count = 0
  @total_tests = total.to_s(10)
end

#_print(s) ⇒ Object



723
# File 'lib/test/unit.rb', line 723

def _print(s); $stdout.print(s); end

#_run_parallel(suites, type, result) ⇒ Object



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/test/unit.rb', line 564

def _run_parallel suites, type, result
  if @options[:parallel] < 1
    warn "Error: parameter of -j option should be greater than 0."
    return
  end

  # Require needed things for parallel running
  require 'thread'
  require 'timeout'
  @tasks = @files.dup # Array of filenames.
  @need_quit = false
  @dead_workers = []  # Array of dead workers.
  @warnings = []
  @total_tests = @tasks.size.to_s(10)
  rep = [] # FIXME: more good naming

  @workers      = [] # Array of workers.
  @workers_hash = {} # out-IO => worker
  @ios          = [] # Array of worker IOs
  begin
    # Thread: watchdog
    watchdog = start_watchdog

    @options[:parallel].times {launch_worker}

    while _io = IO.select(@ios)[0]
      break if _io.any? do |io|
        @need_quit or
          (deal(io, type, result, rep).nil? and
           !@workers.any? {|x| [:running, :prepare].include? x.status})
      end
    end
  rescue Interrupt => ex
    @interrupt = ex
    return result
  ensure
    watchdog.kill if watchdog
    if @interrupt
      @ios.select!{|x| @workers_hash[x].status == :running }
      while !@ios.empty? && (__io = IO.select(@ios,[],[],10))
        __io[0].reject! {|io| deal(io, type, result, rep, true)}
      end
    end

    quit_workers

    unless @interrupt || !@options[:retry] || @need_quit
      @options[:parallel] = false
      suites, rep = rep.partition {|r| r[:testcase] && r[:file] && r[:report].any? {|e| !e[2].is_a?(MiniTest::Skip)}}
      suites.map {|r| r[:file]}.uniq.each {|file| require file}
      suites.map! {|r| eval("::"+r[:testcase])}
      del_status_line or puts
      unless suites.empty?
        puts "Retrying..."
        _run_suites(suites, type)
      end
    end
    unless @options[:retry]
      del_status_line or puts
    end
    unless rep.empty?
      rep.each do |r|
        r[:report].each do |f|
          puke(*f) if f
        end
      end
      if @options[:retry]
        @errors   += rep.map{|x| x[:result][0] }.inject(:+)
        @failures += rep.map{|x| x[:result][1] }.inject(:+)
        @skips    += rep.map{|x| x[:result][2] }.inject(:+)
      end
    end
    unless @warnings.empty?
      warn ""
      @warnings.uniq! {|w| w[1].message}
      @warnings.each do |w|
        warn "#{w[0]}: #{w[1].message} (#{w[1].class})"
      end
      warn ""
    end
  end
end

#_run_suites(suites, type) ⇒ Object



647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/test/unit.rb', line 647

def _run_suites suites, type
  _prepare_run(suites, type)
  @interrupt = nil
  result = []
  GC.start
  if @options[:parallel]
    _run_parallel suites, type, result
  else
    suites.each {|suite|
      begin
        result << _run_suite(suite, type)
      rescue Interrupt => e
        @interrupt = e
        break
      end
    }
  end
  report.reject!{|r| r.start_with? "Skipped:" } if @options[:hide_skip]
  report.sort_by!{|r| r.start_with?("Skipped:") ? 0 : \
                     (r.start_with?("Failure:") ? 1 : 2) }
  result
end

#add_status(line) ⇒ Object



425
426
427
428
429
430
431
432
433
434
435
# File 'lib/test/unit.rb', line 425

def add_status(line)
  unless @options[:job_status] == :replace
    print(line)
    return
  end
  @status_line_size ||= 0
  line = line[0...(terminal_width-@status_line_size)]
  print line
  $stdout.flush
  @status_line_size += line.size
end

#after_worker_down(worker, e = nil, c = false) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/test/unit.rb', line 372

def after_worker_down(worker, e=nil, c=false)
  return unless @options[:parallel]
  return if @interrupt
  warn e if e
  @need_quit = true
  warn ""
  warn "Some worker was crashed. It seems ruby interpreter's bug"
  warn "or, a bug of test/unit/parallel.rb. try again without -j"
  warn "option."
  warn ""
  STDERR.flush
  exit c
end

#after_worker_quit(worker) ⇒ Object



449
450
451
452
453
454
455
# File 'lib/test/unit.rb', line 449

def after_worker_quit(worker)
  return unless @options[:parallel]
  return if @interrupt
  @workers.delete(worker)
  @dead_workers << worker
  @ios = @workers.map(&:io)
end

#deal(io, type, result, rep, shutting_down = false) ⇒ Object



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/test/unit.rb', line 521

def deal(io, type, result, rep, shutting_down = false)
  worker = @workers_hash[io]
  case worker.read
  when /^okay$/
    worker.status = :running
    jobs_status
  when /^ready(!)?$/
    bang = $1
    worker.status = :ready

    return nil unless task = @tasks.shift
    if @options[:separate] and not bang
      worker.quit
      worker = add_worker
    end
    worker.run(task, type)
    @test_count += 1

    jobs_status
  when /^done (.+?)$/
    r = Marshal.load($1.unpack("m")[0])
    result << r[0..1] unless r[0..1] == [nil,nil]
    rep    << {file: worker.real_file, report: r[2], result: r[3], testcase: r[5]}
    $:.push(*r[4]).uniq!
    return true
  when /^p (.+?)$/
    del_jobs_status
    print $1.unpack("m")[0]
    jobs_status if @options[:job_status] == :replace
  when /^after (.+?)$/
    @warnings << Marshal.load($1.unpack("m")[0])
  when /^bye (.+?)$/
    after_worker_down worker, Marshal.load($1.unpack("m")[0])
  when /^bye$/, nil
    if shutting_down || worker.quit_called
      after_worker_quit worker
    else
      after_worker_down worker
    end
  end
  return false
end

#del_jobs_statusObject



444
445
446
447
# File 'lib/test/unit.rb', line 444

def del_jobs_status
  return unless @options[:job_status] == :replace && @status_line_size.nonzero?
  del_status_line
end

#del_status_lineObject



400
401
402
403
404
405
406
407
408
409
# File 'lib/test/unit.rb', line 400

def del_status_line
  @status_line_size ||= 0
  unless @options[:job_status] == :replace
    $stdout.puts
    return
  end
  print "\r"+" "*@status_line_size+"\r"
  $stdout.flush
  @status_line_size = 0
end

#delete_worker(worker) ⇒ Object



473
474
475
476
477
# File 'lib/test/unit.rb', line 473

def delete_worker(worker)
  @workers_hash.delete worker.io
  @workers.delete worker
  @ios.delete worker.io
end

#failed(s) ⇒ Object



726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/test/unit.rb', line 726

def failed(s)
  sep = "\n"
  @report_count ||= 0
  report.each do |msg|
    if msg.start_with? "Skipped:"
      if @options[:hide_skip]
        del_status_line
        next
      end
      color = @skipped_color
    else
      color = @failed_color
    end
    msg = msg.split(/$/, 2)
    $stdout.printf("%s%s%3d) %s%s%s\n",
                   sep, color, @report_count += 1,
                   msg[0], @reset_color, msg[1])
    sep = nil
  end
  report.clear
end

#jobs_statusObject



437
438
439
440
441
442
# File 'lib/test/unit.rb', line 437

def jobs_status
  return unless @options[:job_status]
  puts "" unless @options[:verbose] or @options[:job_status] == :replace
  status_line = @workers.map(&:to_s).join(" ")
  update_status(status_line) or (puts; nil)
end

#launch_workerObject



457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/test/unit.rb', line 457

def launch_worker
  begin
    worker = Worker.launch(@options[:ruby],@args)
  rescue => e
    abort "ERROR: Failed to launch job process - #{e.class}: #{e.message}"
  end
  worker.hook(:dead) do |w,info|
    after_worker_quit w
    after_worker_down w, *info if !info.empty? && !worker.quit_called
  end
  @workers << worker
  @ios << worker.io
  @workers_hash[worker.io] = worker
  worker
end

#new_test(s) ⇒ Object



713
714
715
716
# File 'lib/test/unit.rb', line 713

def new_test(s)
  @test_count += 1
  update_status(s)
end

#outputObject



672
673
674
# File 'lib/test/unit.rb', line 672

def output
  (@output ||= nil) || super
end

#puke(klass, meth, e) ⇒ Object

Overriding of MiniTest::Unit#puke



749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/test/unit.rb', line 749

def puke klass, meth, e
  # TODO:
  #   this overriding is for minitest feature that skip messages are
  #   hidden when not verbose (-v), note this is temporally.
  n = report.size
  rep = super
  if MiniTest::Skip === e and /no message given\z/ =~ e.message
    report.slice!(n..-1)
    rep = "."
  end
  rep
end

#put_status(line) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/test/unit.rb', line 411

def put_status(line)
  unless @options[:job_status] == :replace
    print(line)
    return
  end
  @status_line_size ||= 0
  del_status_line
  $stdout.flush
  line = line[0...terminal_width]
  print line
  $stdout.flush
  @status_line_size = line.size
end

#quit_workersObject



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/test/unit.rb', line 479

def quit_workers
  return if @workers.empty?
  @workers.reject! do |worker|
    begin
      timeout(1) do
        worker.quit
      end
    rescue Errno::EPIPE
    rescue Timeout::Error
    end
    worker.close
  end

  return if @workers.empty?
  begin
    timeout(0.2 * @workers.size) do
      Process.waitall
    end
  rescue Timeout::Error
    @workers.each do |worker|
      worker.kill
    end
    @worker.clear
  end
end

#run(*args) ⇒ Object



773
774
775
776
777
# File 'lib/test/unit.rb', line 773

def run(*args)
  result = super
  puts "\nruby -v: #{RUBY_DESCRIPTION}"
  result
end

#start_watchdogObject



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
# File 'lib/test/unit.rb', line 505

def start_watchdog
  Thread.new do
    while stat = Process.wait2
      break if @interrupt # Break when interrupt
      pid, stat = stat
      w = (@workers + @dead_workers).find{|x| pid == x.pid }
      next unless w
      w = w.dup
      if w.status != :quit && !w.quit_called?
        # Worker down
        w.died(nil, !stat.signaled? && stat.exitstatus)
      end
    end
  end
end

#status(*args) ⇒ Object

Raises:

  • (@interrupt)


767
768
769
770
771
# File 'lib/test/unit.rb', line 767

def status(*args)
  result = super
  raise @interrupt if @interrupt
  result
end

#succeedObject



724
# File 'lib/test/unit.rb', line 724

def succeed; del_status_line; end

#terminal_widthObject



386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/test/unit.rb', line 386

def terminal_width
  unless @terminal_width ||= nil
    begin
      require 'io/console'
      width = $stdout.winsize[1]
    rescue LoadError, NoMethodError, Errno::ENOTTY, Errno::EBADF
      width = ENV["COLUMNS"].to_i.nonzero? || 80
    end
    width -= 1 if /mswin|mingw/ =~ RUBY_PLATFORM
    @terminal_width = width
  end
  @terminal_width
end

#update_status(s) ⇒ Object



718
719
720
721
# File 'lib/test/unit.rb', line 718

def update_status(s)
  count = @test_count.to_s(10).rjust(@total_tests.size)
  put_status("#{@passed_color}[#{count}/#{@total_tests}]#{@reset_color} #{s}")
end