Class: Spork::TestFramework::TestUnit

Inherits:
Spork::TestFramework
  • Object
show all
Defined in:
lib/spork/test_framework/test_unit.rb

Constant Summary collapse

DEFAULT_PORT =
8988
HELPER_FILE =
File.join(Dir.pwd, "test/test_helper.rb")

Instance Method Summary collapse

Instance Method Details

#run_tests(argv, stderr, stdout) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/spork/test_framework/test_unit.rb', line 5

def run_tests(argv, stderr, stdout)
  if defined? MiniTest
    # Ruby 1.9
    stdout = Purdytest::IO.new(stdout) if defined? Purdytest # rewrap
    MiniTest::Unit.output = stdout

    # MiniTest's test/unit does not support -I, -r, or -e
    # Extract them and remove from arguments that are passed to testrb.
    argv.each_with_index do |arg, idx|
      if arg =~ /-I(.*)/
        if $1 == ''
          # Path is next argument.
          include_path = argv[idx + 1]
          argv[idx + 1] = nil # Will be squashed when compact called.
        else
          include_path = $1
        end
        $LOAD_PATH << include_path
        argv[idx] = nil
      elsif arg =~ /-r(.*)/
        if $1 == ''
          # File is next argument.
          require_file = argv[idx + 1]
          argv[idx + 1] = nil # Will be squashed when compact called.
        else
          require_file = $1
        end
        require require_file
        argv[idx] = nil
      elsif arg =~ /^-e$/
        eval argv[idx + 1]
        argv[idx] = argv[idx + 1] = nil
      end
    end
    argv.compact!

    require 'test/unit'                                                                                         
    if defined? Turn
      # Use turn's wrapper around minitest
      if argv.empty?
        puts "Usage: testrb [options] tests..."
        exit 1
      end
      runner = Turn::MiniRunner.new
      config = Turn.config do |c|
        c.tests     = argv
        c.framework = :minitest
      end
      controller = Turn::Controller.new(config)
      controller.start
    elsif Test::Unit.respond_to?(:setup_argv)
      # copied from ruby-1.9.2-p136/bin/testrb:
      Test::Unit.setup_argv(argv) {|files|
        if files.empty?
          puts "Usage: testrb [options] tests..."
          exit 1
        end
        if files.size == 1
          $0 = File.basename(files[0])
        else
          $0 = files.to_s
        end
        files
      }

      MiniTest::Unit.new.run(argv)
    else
      # copied from ruby-head/bin/testrb:
      tests = Test::Unit::AutoRunner.new(true)
      tests.options.banner.sub!(/\[options\]/, '\& tests...')
      unless tests.process_args(argv)
        abort tests.options.banner
      end
      files = tests.to_run
      $0 = files.size == 1 ? File.basename(files[0]) : files.to_s
      tests.run
    end
  else
    # Ruby 1.8
    Object.send(:remove_const, :STDOUT); Object.send(:const_set, :STDOUT, stdout)
    require 'test/unit/autorunner'
    r = Test::Unit::AutoRunner.new(true)
    exit(false) unless r.process_args(argv)
    r.run
  end
rescue => e
  puts "-"*30
  puts "Error executing #{argv.join(' ')}"
  puts e.message
  puts e.backtrace
  puts "-"*30
end