bin/rgot in rgot-0.0.3 vs bin/rgot in rgot-0.0.4

- old
+ new

@@ -1,15 +1,17 @@ #! /usr/bin/env ruby require 'optparse' require 'rgot' -opts = {} +opts = { + require_paths: [] +} parser = OptionParser.new do |o| o.on '-v', '--verbose', "log all tests" do |arg| opts[:verbose] = arg end - o.on '-b', '--bench [regexp]', "benchmark" do |arg| + o.on '--bench [regexp]', "benchmark" do |arg| unless arg raise Rgot::OptionError, "missing argument for flag --bench" end opts[:bench] = arg end @@ -17,73 +19,96 @@ opts[:benchtime] = arg end o.on '--timeout [sec]', "set timeout sec to testing" do |arg| opts[:timeout] = arg end + o.on '--require [path]', "load some code before running" do |arg| + opts[:require_paths] << arg + end + o.on '--load-path [path]', "Specify $LOAD_PATH directory" do |arg| + $LOAD_PATH.unshift(arg) + end end parser.parse!(ARGV) +opts[:require_paths].each do |path| + require path +end -target = ARGV[0] - -if target - if File.file?(target) - require File.expand_path(target) - elsif File.directory?(target) - Dir.glob("./#{target}/**/*_test.rb") do |i| - require i +ARGV.each do |target| + if target + if File.file?(target) + require File.expand_path(target) + elsif File.directory?(target) + Dir.glob("./#{target}/*_test.rb") do |i| + require i + end + else + puts target end else - puts target + Dir.glob("./**/*_test.rb") do |i| + require i + end end -else - Dir.glob("./**/*_test.rb") do |i| - require i - end end modules = Object.constants.select { |c| next if c == :FileTest /.*Test\z/ =~ c +}.map{ |c| + Object.const_get(c) } -if 1 != modules.length - puts "can not load module. found #{modules.join(', ')}" - exit 1 -end +# if 1 != modules.length +# puts "can not load module. found #{modules.join(', ')}" +# exit 1 +# end -tests = [] -benchmarks = [] -examples = [] -main = nil -c = modules.first +code = 0 +modules.each do |test_module| + pid = fork { + tests = [] + benchmarks = [] + examples = [] + main = nil + methods = test_module.instance_methods.sort + methods.grep(/\Atest_/).each do |m| + if m == :test_main && main.nil? + main = Rgot::InternalTest.new(test_module, m) + else + tests << Rgot::InternalTest.new(test_module, m) + end + end -test_module = Object.const_get(c) -methods = test_module.instance_methods.sort -methods.grep(/\Atest_/).each do |m| - if m == :test_main && main.nil? - main = Rgot::InternalTest.new(test_module, m) - else - tests << Rgot::InternalTest.new(test_module, m) - end -end + methods.grep(/\Abenchmark_/).each do |m| + benchmarks << Rgot::InternalBenchmark.new(test_module, m) + end -methods.grep(/\Abenchmark_/).each do |m| - benchmarks << Rgot::InternalBenchmark.new(test_module, m) -end + methods.grep(/\Aexample_?/).each do |m| + examples << Rgot::InternalExample.new(test_module, m) + end -methods.grep(/\Aexample_?/).each do |m| - examples << Rgot::InternalExample.new(test_module, m) -end + duration = Rgot.now + at_exit { + template = "%s\t%s\t%.3fs" -m = Rgot::M.new(tests: tests, benchmarks: benchmarks, examples: examples, opts: opts) -duration = Rgot.now -at_exit { - if !$! - puts sprintf("ok\t%.3fs", Rgot.now - duration) + if $!.kind_of?(SystemExit) && $!.success? + puts sprintf(template, "ok", test_module, Rgot.now - duration) + else + puts sprintf(template, "FAIL", test_module, Rgot.now - duration) + end + } + m = Rgot::M.new(tests: tests, benchmarks: benchmarks, examples: examples, opts: opts) + if main + main.module.extend main.module + main.module.instance_method(main.name).bind(main.module).call(m) + else + exit m.run + end + } + _, status = Process.waitpid2(pid) + unless status.success? + code = 1 end -} -if main - main.module.extend main.module - main.module.instance_method(main.name).bind(main.module).call(m) -else - exit m.run end + +exit code