From 9c560af1b74ef031ab990ba82362192981407c42 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 11 Feb 2011 12:41:58 +0000 Subject: [PATCH] * bin/testrb, test/runner.rb, lib/test/unit.rb: improve backward compatibility. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30841 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 ++++ bin/testrb | 21 ++++++-------- lib/test/unit.rb | 75 ++++++++++++++++++++++++++++++++++++------------ test/runner.rb | 10 +------ 4 files changed, 70 insertions(+), 41 deletions(-) diff --git a/ChangeLog b/ChangeLog index f9e9c09d9a..e5749d29f9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Fri Feb 11 21:41:53 2011 Nobuyoshi Nakada + + * bin/testrb, test/runner.rb, lib/test/unit.rb: improve backward + compatibility. + Fri Feb 11 19:45:26 2011 Nobuyoshi Nakada * eval.c (ruby_cleanup): use rb_ary_free to free internal object. diff --git a/bin/testrb b/bin/testrb index f4cd42f443..d03f057224 100755 --- a/bin/testrb +++ b/bin/testrb @@ -1,15 +1,10 @@ #!/usr/bin/env ruby require 'test/unit' -tests = Test::Unit.new {|files| - if files.empty? - puts "Usage: testrb [options] tests..." - exit false - end - if files.size == 1 - $0 = File.basename(files[0]) - else - $0 = files.to_s - end - files -} -exit tests.run(ARGV) || true +tests = Test::Unit::AutoRunner.new(true) +tests.options.banner.sub!(/\[options\]/, '\& tests...') +unless tests.process_args(ARGV) + abort tests.options.banner +end +p files = tests.to_run +$0 = files.size == 1 ? File.basename(files[0]) : files.to_s +exit tests.run diff --git a/lib/test/unit.rb b/lib/test/unit.rb index 97d0da5ca5..25ff6cc980 100644 --- a/lib/test/unit.rb +++ b/lib/test/unit.rb @@ -30,28 +30,32 @@ module Test end module Options - def initialize(&block) + def initialize(*, &block) @init_hook = block super(&nil) end + def option_parser + @option_parser ||= OptionParser.new + end + def process_args(args = []) + return @options if @options orig_args = args.dup options = {} - OptionParser.new do |opts| - setup_options(opts, options) - opts.parse!(args) - orig_args -= args - end + opts = option_parser + setup_options(opts, options) + opts.parse!(args) + orig_args -= args args = @init_hook.call(args, options) if @init_hook - non_options(args, options) + non_options(args, options) or return nil @help = orig_args.map { |s| s =~ /[\s|&<>$()]/ ? s.inspect : s }.join " " - options + @options = options end private def setup_options(opts, options) - opts.banner = 'minitest options:' + opts.separator 'minitest options:' opts.version = MiniTest::Unit::VERSION opts.on '-h', '--help', 'Display this help.' do @@ -74,6 +78,7 @@ module Test end def non_options(files, options) + true end end @@ -82,6 +87,9 @@ module Test def setup_options(parser, options) super + parser.on '-b', '--basedir=DIR', 'Base directory of test suites.' do |dir| + options[:base_directory] = dir + end parser.on '-x', '--exclude PATTERN', 'Exclude test files on pattern.' do |pattern| (options[:reject] ||= []) << pattern end @@ -94,8 +102,13 @@ module Test end files.map! {|f| f = f.tr(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR - [*paths, nil].any? do |prefix| - path = prefix ? "#{prefix}/#{f}" : f + [*(paths if /\A\.\.?(?:\z|\/)/ !~ f), nil].uniq.any? do |prefix| + if prefix + path = f.empty? ? prefix : "#{prefix}/#{f}" + else + next if f.empty? + path = f + end if !(match = Dir["#{path}/**/test_*.rb"]).empty? if reject match.reject! {|n| @@ -154,7 +167,7 @@ module Test module RequireFiles def non_options(files, options) - super + return false if !super or files.empty? files.each {|f| d = File.dirname(path = File.expand_path(f)) unless $:.include? d @@ -169,13 +182,6 @@ module Test end end - def self.new(*args, &block) - Mini.class_eval do - include Test::Unit::RequireFiles - end - Mini.new(*args, &block) - end - class Mini < MiniTest::Unit include Test::Unit::GlobOption include Test::Unit::LoadPathOption @@ -213,6 +219,37 @@ module Test result end end + + class AutoRunner + class Runner < Mini + include Test::Unit::RequireFiles + end + + attr_accessor :to_run, :options + + def initialize(force_standalone = false, default_dir = nil, argv = ARGV) + @runner = Runner.new do |files, options| + options[:base_directory] ||= default_dir + @to_run = files + yield self if block_given? + files + end + @options = @runner.option_parser + @argv = argv + end + + def process_args(*args) + @runner.process_args(*args) + end + + def run + @runner.run(@argv) || true + end + + def self.run(*args) + new(*args).run + end + end end end diff --git a/test/runner.rb b/test/runner.rb index e19ed82562..441bb0782e 100644 --- a/test/runner.rb +++ b/test/runner.rb @@ -7,12 +7,4 @@ srcdir = File.dirname(src_testdir) require_relative 'profile_test_all' if ENV['RUBY_TEST_ALL_PROFILE'] == 'true' -tests = Test::Unit.new {|files, options| - options[:base_directory] = src_testdir - if files.empty? - [src_testdir] - else - files - end -} -exit tests.run(ARGV) || true +exit Test::Unit::AutoRunner.run(true, src_testdir)