From 54bfb33366b21321e7093e454935a35a488dbb25 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 4 Jan 2015 13:32:44 +0000 Subject: test/unit.rb: reorder Test::Unit * test/lib/test/unit.rb (Test::Unit): reorder modules and merge each modules. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49142 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/lib/test/unit.rb | 641 +++++++++++++++++++++++--------------------------- 1 file changed, 298 insertions(+), 343 deletions(-) (limited to 'test/lib/test') diff --git a/test/lib/test/unit.rb b/test/lib/test/unit.rb index edeb829d1c..048fc19332 100644 --- a/test/lib/test/unit.rb +++ b/test/lib/test/unit.rb @@ -65,20 +65,7 @@ module Test @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 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 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 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 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 @@ -767,6 +489,23 @@ module Test 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 + private def _run_suites(suites, type) result = super @@ -777,11 +516,70 @@ module Test 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 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 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) - result = super - puts "\nruby -v: #{RUBY_DESCRIPTION}" + 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 StatusLineOutput < 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) + 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 + + 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 + + alias mini_run_suite _run_suite + + # 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 = "." end + rep end end -- cgit v1.2.3