summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog4
-rwxr-xr-xbin/testrb3
-rw-r--r--lib/test/unit.rb175
-rw-r--r--test/runner.rb3
4 files changed, 124 insertions, 61 deletions
diff --git a/ChangeLog b/ChangeLog
index e445aa4..f746ac2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Sat Jul 17 19:01:47 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * lib/test/unit.rb (MiniTest::Unit#process_args): refactored.
+
Sat Jul 17 18:30:05 2010 Tanaka Akira <akr@fsij.org>
* tool/file2lastrev.rb: don't depend on pathname.rb if File.realpath
diff --git a/bin/testrb b/bin/testrb
index 6a65fba..f4cd42f 100755
--- a/bin/testrb
+++ b/bin/testrb
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
require 'test/unit'
-exit Test::Unit.start {|files|
+tests = Test::Unit.new {|files|
if files.empty?
puts "Usage: testrb [options] tests..."
exit false
@@ -12,3 +12,4 @@ exit Test::Unit.start {|files|
end
files
}
+exit tests.run(ARGV) || true
diff --git a/lib/test/unit.rb b/lib/test/unit.rb
index 646fd5e..259ff98 100644
--- a/lib/test/unit.rb
+++ b/lib/test/unit.rb
@@ -20,93 +20,150 @@ module Test
@@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
- def self.setup_argv(original_argv=::ARGV)
- minitest_argv = []
- files = []
- reject = []
- original_argv = original_argv.dup
- OptionParser.new do |parser|
- parser.default_argv = original_argv
+ module Options
+ def initialize(&block)
+ @init_hook = block
+ super(&nil)
+ end
- parser.on '-v', '--verbose' do |v|
- minitest_argv << '-v' if v
+ 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
- parser.on '-n', '--name TESTNAME' do |name|
- minitest_argv << '-n'
- minitest_argv << name
+ 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
- parser.on '-x', '--exclude PATTERN' do |pattern|
- reject << pattern
+ opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m|
+ options[:seed] = m.to_i
end
- parser.on '-Idirectory' do |dirs|
- dirs.split(':').each { |d| $LOAD_PATH.unshift d }
+ opts.on '-v', '--verbose', "Verbose. Show progress processing files." do
+ options[:verbose] = true
end
- end.parse!
- files = original_argv
- if block_given?
- files = yield files
+ opts.on '-n', '--name PATTERN', "Filter test names on pattern." do |a|
+ options[:filter] = a
+ end
end
- 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!
-
- reject_pat = Regexp.union(reject.map {|r| /#{r}/ })
- files.reject! {|f| reject_pat =~ f }
-
- MiniTest::Unit._install_at_exit {
- next if RunCount.have_run?
- next if $! # don't run if there was an exception
- exit false unless run(minitest_argv)
- }
-
- files.each {|f|
- d = File.dirname(path = File.expand_path(f))
- unless $:.include? d
- $: << d
+ 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
- begin
- require path
- rescue LoadError
- puts "#{f}: #{$!}"
+ 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
- minitest_argv
+ def setup_options(parser, options)
+ super
+ parser.on '-Idirectory' do |dirs|
+ dirs.split(':').each { |d| $LOAD_PATH.unshift d }
+ end
+ end
end
- def self.run(args)
- exit_code = MiniTest::Unit.new.run(args)
- !exit_code || exit_code == 0
+ def self.new
+ Mini.new do |files, options|
+ if block_given?
+ files = yield files
+ end
+ files
+ end
end
- def self.start(argv=::ARGV, &block)
- run(setup_argv(argv, &block))
+ 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(*)
- super.extend(Test::Unit::RunCount)
+ super
+ .extend(Test::Unit::RunCount)
+ .extend(Test::Unit::Options)
end
- def self._install_at_exit(&block)
- at_exit(&block) unless @@installed_at_exit
+ class << self; undef autorun; end
+ def self.autorun
+ at_exit {
+ Test::Unit::RunCount.run_once {exit(new.run(ARGV) || true)}
+ } unless @@installed_at_exit
@@installed_at_exit = true
end
end
+
+MiniTest::Unit.autorun
diff --git a/test/runner.rb b/test/runner.rb
index 3735461..6fd02a8 100644
--- a/test/runner.rb
+++ b/test/runner.rb
@@ -6,7 +6,7 @@ require 'test/unit'
src_testdir = File.dirname(File.expand_path(__FILE__))
srcdir = File.dirname(src_testdir)
-exit Test::Unit.start {|files|
+tests = Test::Unit.new {|files|
if files.empty?
[src_testdir]
else
@@ -23,3 +23,4 @@ exit Test::Unit.start {|files|
}
end
}
+exit tests.run(ARGV) || true