[前][次][番号順一覧][スレッド一覧]

ruby-changes:18816

From: nobu <ko1@a...>
Date: Fri, 11 Feb 2011 21:42:28 +0900 (JST)
Subject: [ruby-changes:18816] Ruby:r30841 (trunk): * bin/testrb, test/runner.rb, lib/test/unit.rb: improve backward

nobu	2011-02-11 21:41:58 +0900 (Fri, 11 Feb 2011)

  New Revision: 30841

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=30841

  Log:
    * bin/testrb, test/runner.rb, lib/test/unit.rb: improve backward
      compatibility.

  Modified files:
    trunk/ChangeLog
    trunk/bin/testrb
    trunk/lib/test/unit.rb
    trunk/test/runner.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 30840)
+++ ChangeLog	(revision 30841)
@@ -1,3 +1,8 @@
+Fri Feb 11 21:41:53 2011  Nobuyoshi Nakada  <nobu@r...>
+
+	* bin/testrb, test/runner.rb, lib/test/unit.rb: improve backward
+	  compatibility.
+
 Fri Feb 11 19:45:26 2011  Nobuyoshi Nakada  <nobu@r...>
 
 	* eval.c (ruby_cleanup): use rb_ary_free to free internal object.
Index: lib/test/unit.rb
===================================================================
--- lib/test/unit.rb	(revision 30840)
+++ lib/test/unit.rb	(revision 30841)
@@ -30,28 +30,32 @@
     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 @@
       end
 
       def non_options(files, options)
+        true
       end
     end
 
@@ -82,6 +87,9 @@
 
       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 @@
         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 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 @@
       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 @@
         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
 
Index: bin/testrb
===================================================================
--- bin/testrb	(revision 30840)
+++ bin/testrb	(revision 30841)
@@ -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
Index: test/runner.rb
===================================================================
--- test/runner.rb	(revision 30840)
+++ test/runner.rb	(revision 30841)
@@ -7,12 +7,4 @@
 
 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)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]