Class: Test::Unit::Runner

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

Overview

:nodoc: all

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

Returns a new instance of Runner.



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

def initialize
  super
  @tty = $stdout.tty?
end

Class Method Details

.autorunObject



369
370
371
372
373
374
375
376
# File 'lib/test/unit.rb', line 369

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



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
712
713
714
715
716
717
# File 'lib/test/unit.rb', line 682

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



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

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

#_run_parallel(suites, type, result) ⇒ Object



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
646
647
648
649
650
651
# File 'lib/test/unit.rb', line 570

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



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'lib/test/unit.rb', line 653

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



431
432
433
434
435
436
437
438
439
440
441
# File 'lib/test/unit.rb', line 431

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



378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/test/unit.rb', line 378

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



455
456
457
458
459
460
461
# File 'lib/test/unit.rb', line 455

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



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
563
564
565
566
567
568
# File 'lib/test/unit.rb', line 527

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



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

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

#del_status_lineObject



406
407
408
409
410
411
412
413
414
415
# File 'lib/test/unit.rb', line 406

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



479
480
481
482
483
# File 'lib/test/unit.rb', line 479

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

#failed(s) ⇒ Object



732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# File 'lib/test/unit.rb', line 732

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



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

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



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/test/unit.rb', line 463

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



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

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

#outputObject



678
679
680
# File 'lib/test/unit.rb', line 678

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

#puke(klass, meth, e) ⇒ Object

Overriding of MiniTest::Unit#puke



755
756
757
758
759
760
761
762
763
764
765
766
# File 'lib/test/unit.rb', line 755

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



417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/test/unit.rb', line 417

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



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/test/unit.rb', line 485

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



779
780
781
782
783
# File 'lib/test/unit.rb', line 779

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

#start_watchdogObject



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/test/unit.rb', line 511

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)


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

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

#succeedObject



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

def succeed; del_status_line; end

#terminal_widthObject



392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/test/unit.rb', line 392

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



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

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