ruby-changes:37061
From: nobu <ko1@a...>
Date: Sun, 4 Jan 2015 22:32:49 +0900 (JST)
Subject: [ruby-changes:37061] nobu:r49142 (trunk): test/unit.rb: reorder Test::Unit
nobu 2015-01-04 22:32:44 +0900 (Sun, 04 Jan 2015) New Revision: 49142 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49142 Log: test/unit.rb: reorder Test::Unit * test/lib/test/unit.rb (Test::Unit): reorder modules and merge each modules. Modified files: trunk/ChangeLog trunk/test/lib/test/unit.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 49141) +++ ChangeLog (revision 49142) @@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 -Sun Jan 4 22:32:09 2015 Nobuyoshi Nakada <nobu@r...> +Sun Jan 4 22:32:42 2015 Nobuyoshi Nakada <nobu@r...> + + * test/lib/test/unit.rb (Test::Unit): reorder modules and merge + each modules. * test/lib/test/unit.rb (Test::Unit): split the large class into each modules. Index: test/lib/test/unit.rb =================================================================== --- test/lib/test/unit.rb (revision 49141) +++ test/lib/test/unit.rb (revision 49142) @@ -65,20 +65,7 @@ module Test https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit.rb#L65 @help = orig_args.map { |s| s =~ /[\s|&<>$()]/ ? s.inspect : s }.join " " @options = options end - end - - module Parallel # :nodoc: all - def process_args(args = []) - return @options if @options - options = super - if @options[:parallel] - @files = args - end - options - end - end - module Options # :nodoc: all private def setup_options(opts, options) opts.separator 'minitest options:' @@ -108,31 +95,22 @@ module Test https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit.rb#L95 end end - module Skipping # :nodoc: all - end - - module Colorize # :nodoc: all - end - - module StatusLine # :nodoc: all - prepend Colorize - - private - def setup_options(opts, options) - super - - opts.separator "status line options:" - - options[:job_status] = nil - - opts.on '--jobs-status [TYPE]', [:normal, :replace], - "Show status of jobs every file; Disabled when --jobs isn't specified." do |type| - options[:job_status] = type || :normal + module Parallel # :nodoc: all + def process_args(args = []) + return @options if @options + options = super + if @options[:parallel] + @files = args end + options + end + + def status(*args) + result = super + raise @interrupt if @interrupt + result end - end - module Parallel # :nodoc: all private def setup_options(opts, options) super @@ -167,180 +145,7 @@ module Test https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit.rb#L145 options[:ruby] = a.split(/ /).reject(&:empty?) end end - end - - module Skipping # :nodoc: all - private - def setup_options(opts, options) - super - - opts.separator "skipping options:" - - options[:hide_skip] = true - - opts.on '-q', '--hide-skip', 'Hide skipped tests' do - options[:hide_skip] = true - end - - opts.on '--show-skip', 'Show skipped tests' do - options[:hide_skip] = false - end - end - end - - module Colorize # :nodoc: all - private - def setup_options(opts, options) - super - - opts.on '--color[=WHEN]', - [:always, :never, :auto], - "colorize the output. WHEN defaults to 'always'", "or can be 'never' or 'auto'." do |c| - options[:color] = c || :always - end - - opts.on '--tty[=WHEN]', - [:yes, :no], - "force to output tty control. WHEN defaults to 'yes'", "or can be 'no'." do |c| - @tty = c != :no - end - end - end - - module LoadPathOption # :nodoc: all - def non_options(files, options) - begin - require "rbconfig" - rescue LoadError - warn "#{caller(1)[0]}: warning: Parallel running disabled because can't get path to ruby; run specify with --ruby argument" - options[:parallel] = nil - else - options[:ruby] ||= [RbConfig.ruby] - end - - super - end - end - - module GlobOption # :nodoc: all - @@testfile_prefix = "test" - - def setup_options(parser, options) - super - parser.separator "globbing options:" - parser.on '-b', '--basedir=DIR', 'Base directory of test suites.' do |dir| - options[:base_directory] = dir - end - parser.on '-x', '--exclude REGEXP', 'Exclude test files on pattern.' do |pattern| - (options[:reject] ||= []) << pattern - end - end - - def non_options(files, options) - paths = [options.delete(:base_directory), nil].uniq - if reject = options.delete(:reject) - reject_pat = Regexp.union(reject.map {|r| %r"#{r}"}) - end - files.map! {|f| - f = f.tr(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR - ((paths if /\A\.\.?(?:\z|\/)/ !~ f) || [nil]).any? do |prefix| - if prefix - path = f.empty? ? prefix : "#{prefix}/#{f}" - else - next if f.empty? - path = f - end - if !(match = Dir["#{path}/**/#{@@testfile_prefix}_*.rb"]).empty? - if reject - match.reject! {|n| - n[(prefix.length+1)..-1] if prefix - reject_pat =~ n - } - end - break match - elsif !reject or reject_pat !~ f and File.exist? path - break path - end - end or - raise ArgumentError, "file not found: #{f}" - } - files.flatten! - super(files, options) - end - end - - module LoadPathOption # :nodoc: all - def setup_options(parser, options) - super - parser.separator "load path options:" - parser.on '-Idirectory', 'Add library load path' do |dirs| - dirs.split(':').each { |d| $LOAD_PATH.unshift d } - end - end - end - - module GCStressOption # :nodoc: all - def setup_options(parser, options) - super - parser.separator "GC options:" - parser.on '--[no-]gc-stress', 'Set GC.stress as true' do |flag| - options[:gc_stress] = flag - end - end - - def non_options(files, options) - if options.delete(:gc_stress) - MiniTest::Unit::TestCase.class_eval do - oldrun = instance_method(:run) - define_method(:run) do |runner| - begin - gc_stress, GC.stress = GC.stress, true - oldrun.bind(self).call(runner) - ensure - GC.stress = gc_stress - end - end - end - end - super - end - end - - module RequireFiles # :nodoc: all - def non_options(files, options) - return false if !super - errors = {} - result = false - files.each {|f| - d = File.dirname(path = File.realpath(f)) - unless $:.include? d - $: << d - end - begin - require path unless options[:parallel] - result = true - rescue LoadError - next if errors[$!.message] - errors[$!.message] = true - puts "#{f}: #{$!}" - end - } - result - end - end - - class Runner < MiniTest::Unit # :nodoc: all - include Test::Unit::Options - include Test::Unit::StatusLine - include Test::Unit::Parallel - include Test::Unit::Skipping - include Test::Unit::GlobOption - include Test::Unit::LoadPathOption - include Test::Unit::GCStressOption - include Test::Unit::RunCount - end - module Parallel # :nodoc: all class Worker def self.launch(ruby,args=[]) io = IO.popen([*ruby, @@ -438,23 +243,7 @@ module Test https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit.rb#L243 end end - end - - class Runner < MiniTest::Unit # :nodoc: all - class << self; undef autorun; end - - @@stop_auto_run = false - def self.autorun - at_exit { - Test::Unit::RunCount.run_once { - exit(Test::Unit::Runner.new.run(ARGV) || true) - } unless @@stop_auto_run - } unless @@installed_at_exit - @@installed_at_exit = true - end - end - module Parallel # :nodoc: all def after_worker_down(worker, e=nil, c=false) return unless @options[:parallel] return if @interrupt @@ -468,80 +257,13 @@ module Test https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit.rb#L257 STDERR.flush exit c end - end - module StatusLine # :nodoc: all - def terminal_width - unless @terminal_width ||= nil - begin - require 'io/console' - width = $stdout.winsize[1] - rescue LoadError, NoMethodError, Errno::ENOTTY, Errno::EBADF, Errno::EINVAL - width = ENV["COLUMNS"].to_i.nonzero? || 80 - end - width -= 1 if /mswin|mingw/ =~ RUBY_PLATFORM - @terminal_width = width - end - @terminal_width - end - - def del_status_line - @status_line_size ||= 0 - unless @options[:job_status] == :replace - $stdout.puts - return - end - print "\r"+" "*@status_line_size+"\r" - $stdout.flush - @status_line_size = 0 - end - - def put_status(line) - unless @options[:job_status] == :replace - print(line) - return - end - @status_line_size ||= 0 - del_status_line - $stdout.flush - line = line[0...terminal_width] - print line - $stdout.flush - @status_line_size = line.size - end - - def add_status(line) - unless @options[:job_status] == :replace - print(line) - return - end - @status_line_size ||= 0 - line = line[0...(terminal_width-@status_line_size)] - print line - $stdout.flush - @status_line_size += line.size - end - - def jobs_status - return unless @options[:job_status] - puts "" unless @options[:verbose] or @options[:job_status] == :replace - status_line = @workers.map(&:to_s).join(" ") - update_status(status_line) or (puts; nil) - end - - def del_jobs_status - return unless @options[:job_status] == :replace && @status_line_size.nonzero? - del_status_line - end - end - - module Parallel # :nodoc: all - def after_worker_quit(worker) - return unless @options[:parallel] - return if @interrupt - @workers.delete(worker) - @dead_workers << worker - @ios = @workers.map(&:io) + def after_worker_quit(worker) + return unless @options[:parallel] + return if @interrupt + @workers.delete(worker) + @dead_workers << worker + @ios = @workers.map(&:io) end def launch_worker @@ -768,6 +490,23 @@ module Test https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit.rb#L490 module Skipping # :nodoc: all private + def setup_options(opts, options) + super + + opts.separator "skipping options:" + + options[:hide_skip] = true + + opts.on '-q', '--hide-skip', 'Hide skipped tests' do + options[:hide_skip] = true + end + + opts.on '--show-skip', 'Show skipped tests' do + options[:hide_skip] = false + end + end + + private def _run_suites(suites, type) result = super report.reject!{|r| r.start_with? "Skipped:" } if @options[:hide_skip] @@ -777,11 +516,70 @@ module Test https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit.rb#L516 end end - class Runner < MiniTest::Unit # :nodoc: all - alias mini_run_suite _run_suite - end - module StatusLine # :nodoc: all + def terminal_width + unless @terminal_width ||= nil + begin + require 'io/console' + width = $stdout.winsize[1] + rescue LoadError, NoMethodError, Errno::ENOTTY, Errno::EBADF, Errno::EINVAL + width = ENV["COLUMNS"].to_i.nonzero? || 80 + end + width -= 1 if /mswin|mingw/ =~ RUBY_PLATFORM + @terminal_width = width + end + @terminal_width + end + + def del_status_line + @status_line_size ||= 0 + unless @options[:job_status] == :replace + $stdout.puts + return + end + print "\r"+" "*@status_line_size+"\r" + $stdout.flush + @status_line_size = 0 + end + + def put_status(line) + unless @options[:job_status] == :replace + print(line) + return + end + @status_line_size ||= 0 + del_status_line + $stdout.flush + line = line[0...terminal_width] + print line + $stdout.flush + @status_line_size = line.size + end + + def add_status(line) + unless @options[:job_status] == :replace + print(line) + return + end + @status_line_size ||= 0 + line = line[0...(terminal_width-@status_line_size)] + print line + $stdout.flush + @status_line_size += line.size + end + + def jobs_status + return unless @options[:job_status] + puts "" unless @options[:verbose] or @options[:job_status] == :replace + status_line = @workers.map(&:to_s).join(" ") + update_status(status_line) or (puts; nil) + end + + def del_jobs_status + return unless @options[:job_status] == :replace && @status_line_size.nonzero? + del_status_line + end + def output (@output ||= nil) || super end @@ -808,7 +606,7 @@ module Test https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit.rb#L606 end if color or @options[:job_status] == :replace @verbose = !options[:parallel] - @output = StatusLineOutput.new(self) + @output = Output.new(self) end if /\A\/(.*)\/\z/ =~ (filter = options[:filter]) filter = Regexp.new($1) @@ -857,65 +655,222 @@ module Test https://github.com/ruby/ruby/blob/trunk/test/lib/test/unit.rb#L655 end report.clear end + + def initialize + super + @tty = $stdout.tty? + end + + def run(*args) + result = super + puts "\nruby -v: #{RUBY_DESCRIPTION}" + result + end + + private + def setup_options(opts, options) + super + + opts.separator "status line options:" + + options[:job_status] = nil + + opts.on '--jobs-status [TYPE]', [:normal, :replace], + "Show status of jobs every file; Disabled when --jobs isn't specified." do |type| + options[:job_status] = type || :normal + end + + opts.on '--color[=WHEN]', + [:always, :never, :auto], + "colorize the output. WHEN defaults to 'always'", "or can be 'never' or 'auto'." do |c| + options[:color] = c || :always + end + + opts.on '--tty[=WHEN]', + [:yes, :no], + "force to output tty control. WHEN defaults to 'yes'", "or can be 'no'." do |c| + @tty = c != :no + end + end + + class Output < Struct.new(:runner) # :nodoc: all + def puts(*a) $stdout.puts(*a) unless a.empty? end + def respond_to_missing?(*a) $stdout.respond_to?(*a) end + def method_missing(*a, &b) $stdout.__send__(*a, &b) end + + def print(s) + case s + when /\A(.*\#.*) = \z/ + runner.new_test($1) + when /\A(.* s) = \z/ + runner.add_status(" = "+$1.chomp) + when /\A\.+\z/ + runner.succeed + when /\A[EFS]\z/ + runner.failed(s) + else + $stdout.print(s) + end + end + end end - class Runner < MiniTest::Unit # :nodoc: all - # Overriding of MiniTest::Unit#puke - def puke klass, meth, e - # TODO: - # this overriding is for minitest feature that skip messages are - # hidden when not verbose (-v), note this is temporally. - n = report.size - rep = super - if MiniTest::Skip === e and /no message given\z/ =~ e.message - report.slice!(n..-1) - rep = "." + module LoadPathOption # :nodoc: all + def non_options(files, options) + begin + require "rbconfig" + rescue LoadError + warn "#{caller(1)[0]}: warning: Parallel running disabled because can't get path to ruby; run specify with --ruby argument" + options[:parallel] = nil + else + options[:ruby] ||= [RbConfig.ruby] + end + + super + end + + def setup_options(parser, options) + super + parser.separator "load path options:" + parser.on '-Idirectory', 'Add library load path' do |dirs| + dirs.split(':').each { |d| $LOAD_PATH.unshift d } end - rep end end - module StatusLine # :nodoc: all - def initialize + module GlobOption # :nodoc: all + @@testfile_prefix = "test" + + def setup_options(parser, options) super - @tty = $stdout.tty? + parser.separator "globbing options:" + parser.on '-b', '--basedir=DIR', 'Base directory of test suites.' do |dir| + options[:base_directory] = dir + end + parser.on '-x', '--exclude REGEXP', 'Exclude test files on pattern.' do |pattern| + (options[:reject] ||= []) << pattern + end + end + + def non_options(files, options) + paths = [options.delete(:base_directory), nil].uniq + if reject = options.delete(:reject) + reject_pat = Regexp.union(reject.map {|r| %r"#{r}"}) + end + files.map! {|f| + f = f.tr(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR + ((paths if /\A\.\.?(?:\z|\/)/ !~ f) || [nil]).any? do |prefix| + if prefix + path = f.empty? ? prefix : "#{prefix}/#{f}" + else + next if f.empty? + path = f + end + if !(match = Dir["#{path}/**/#{@@testfile_prefix}_*.rb"]).empty? + if reject + match.reject! {|n| + n[(prefix.length+1)..-1] if prefix + reject_pat =~ n + } + end + break match + elsif !reject or reject_pat !~ f and File.exist? path + break path + end + end or + raise ArgumentError, "file not found: #{f}" + } + files.flatten! + super(files, options) end end - module Parallel # :nodoc: all - def status(*args) - result = super - raise @interrupt if @interrupt - result + module GCStressOption # :nodoc: all + def setup_options(parser, options) + super + parser.separator "GC options:" + parser.on '--[no-]gc-stress', 'Set GC.stress as true' do |flag| + options[:gc_stress] = flag + end + end + + def non_options(files, options) + if options.delete(:gc_stress) + MiniTest::Unit::TestCase.class_eval do + oldrun = instance_method(:run) + define_method(:run) do |runner| + begin + gc_stress, GC.stress = GC.stress, true + oldrun.bind(self).call(runner) + ensure + GC.stress = gc_stress + end + end + end + end + super end end - module StatusLine # :nodoc: all - def run(*args) - (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/