summaryrefslogtreecommitdiff
path: root/lib/test/unit.rb
blob: 68fd808102dbf7aab0c429a727fae240142d40ba (plain)
1
2
3
4
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# test/unit compatibility layer using minitest.

require 'minitest/unit'
require 'test/unit/assertions'
require 'test/unit/testcase'
require 'optparse'

module Test
  module Unit
    TEST_UNIT_IMPLEMENTATION = 'test/unit compatibility layer using minitest'

    module RunCount
      @@run_count = 0

      def self.have_run?
        @@run_count.nonzero?
      end

      def run(*)
        @@run_count += 1
        super
      end

      def run_once
        return if have_run?
        return if $! # don't run if there was an exception
        yield
      end
      module_function :run_once
    end

    module Options
      def initialize(&block)
        @init_hook = block
        super(&nil)
      end

      def process_args(args = [])
        options = {}
        OptionParser.new do |opts|
          setup_options(opts, options)
          opts.parse!(args)
        end
        args = @init_hook.call(args, options) if @init_hook
        non_options(args, options)
        options
      end

      private
      def setup_options(opts, options)
        opts.banner  = 'minitest options:'
        opts.version = MiniTest::Unit::VERSION

        opts.on '-h', '--help', 'Display this help.' do
          puts opts
          exit
        end

        opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m|
          options[:seed] = m.to_i
        end

        opts.on '-v', '--verbose', "Verbose. Show progress processing files." do
          options[:verbose] = true
        end

        opts.on '-n', '--name PATTERN', "Filter test names on pattern." do |a|
          options[:filter] = a
        end
      end

      def non_options(files, options)
        files.each {|f|
          d = File.dirname(path = File.expand_path(f))
          unless $:.include? d
            $: << d
          end
          begin
            require path
          rescue LoadError
            puts "#{f}: #{$!}"
          end
        }
      end
    end

    module GlobOption
      include Options

      def non_options(files, options)
        files.map! {|f|
          f = f.tr(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR
          if File.directory? f
            Dir["#{f}/**/test_*.rb"]
          elsif File.file? f
            f
          else
            raise ArgumentError, "file not found: #{f}"
          end
        }
        files.flatten!
        super(files, options)
      end
    end

    module RejectOption
      include Options

      def setup_options(parser, options)
        super
        parser.on '-x', '--exclude PATTERN' do |pattern|
          (options[:reject] ||= []) << pattern
        end
      end

      def non_options(files, options)
        if reject = options.delete(:reject)
          reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
          files.reject! {|f| reject_pat =~ f }
        end
        super(files, options)
      end
    end

    module LoadPathOption
      include Options

      def setup_options(parser, options)
        super
        parser.on '-Idirectory' do |dirs|
          dirs.split(':').each { |d| $LOAD_PATH.unshift d }
        end
      end
    end

    def self.new
      Mini.new do |files, options|
        if block_given?
          files = yield files
        end
        files
      end
    end

    class Mini < MiniTest::Unit
      include Test::Unit::GlobOption
      include Test::Unit::RejectOption
      include Test::Unit::LoadPathOption
    end
  end
end

class MiniTest::Unit
  def self.new(*args, &block)
    obj = allocate
      .extend(Test::Unit::RunCount)
      .extend(Test::Unit::Options)
    obj.__send__(:initialize, *args, &block)
    obj
  end

  class << self; undef autorun; end
  def self.autorun
    at_exit {
      Test::Unit::RunCount.run_once {
        exit(Test::Unit::Mini.new.run(ARGV) || true)
      }
    } unless @@installed_at_exit
    @@installed_at_exit = true
  end
end

MiniTest::Unit.autorun