lib/parallel_tests/cli.rb in parallel_tests-0.7.0.alpha2 vs lib/parallel_tests/cli.rb in parallel_tests-0.7.0.rc1

- old
+ new

@@ -3,53 +3,73 @@ module ParallelTest module CLI def self.run(argv) options = parse_options!(argv) - test_results = nil num_processes = ParallelTests.determine_number_of_processes(options[:count]) num_processes = num_processes * (options[:multiply] || 1) if options[:execute] execute_shell_command_in_parallel(options[:execute], num_processes, options) else - lib = options[:type] || 'test' - require "parallel_tests/#{lib}/runner" - runner = eval("ParallelTests::#{lib.capitalize}::Runner") - name = runner.test_file_name + run_tests_in_parallel(num_processes, options) + end + end - report_time_taken do - groups = runner.tests_in_groups(options[:files], num_processes, options) - abort "no #{name}s found!" if groups.size == 0 + private - num_processes = groups.size - num_tests = groups.inject(0) { |sum, item| sum + item.size } - puts "#{num_processes} processes for #{num_tests} #{name}s, ~ #{num_tests / groups.size} #{name}s per process" + def self.run_tests_in_parallel(num_processes, options) + test_results = nil + lib = options[:type] || 'test' + runner = load_runner_for(lib) - test_results = Parallel.map(groups, :in_processes => num_processes) do |group| - if group.empty? - {:stdout => '', :exit_status => 0} - else - runner.run_tests(group, groups.index(group), options) - end - end + report_time_taken do + groups = runner.tests_in_groups(options[:files], num_processes, options) + report_number_of_tests runner, groups - #parse and print results - results = runner.find_results(test_results.map { |result| result[:stdout] }*"") - puts "" - puts runner.summarize_results(results) + test_results = Parallel.map(groups, :in_processes => groups.size) do |group| + run_tests(runner, group, groups.index(group), options) end - #exit with correct status code so rake parallel:test && echo 123 works - failed = test_results.any? { |result| result[:exit_status] != 0 } - abort "#{lib.capitalize}s Failed" if failed + report_results runner, test_results end + + abort "#{lib.capitalize}s Failed" if any_test_failed?(test_results) end - private + def self.run_tests(runner, group, process_number, options) + if group.empty? + {:stdout => '', :exit_status => 0} + else + runner.run_tests(group, process_number, options) + end + end + def self.report_results(runner, test_results) + results = runner.find_results(test_results.map { |result| result[:stdout] }*"") + puts "" + puts runner.summarize_results(results) + end + + def self.report_number_of_tests(runner, groups) + name = runner.test_file_name + num_processes = groups.size + num_tests = groups.map(&:size).inject(:+) + puts "#{num_processes} processes for #{num_tests} #{name}s, ~ #{num_tests / groups.size} #{name}s per process" + end + + #exit with correct status code so rake parallel:test && echo 123 works + def self.any_test_failed?(test_results) + test_results.any? { |result| result[:exit_status] != 0 } + end + + def self.load_runner_for(lib) + require "parallel_tests/#{lib}/runner" + eval("ParallelTests::#{lib.capitalize}::Runner") + end + def self.parse_options!(argv) options = {} OptionParser.new do |opts| opts.banner = <<BANNER Run all tests in parallel, giving each process ENV['TEST_ENV_NUMBER'] ('', '2', '3', ...) @@ -58,10 +78,10 @@ parallel_test test/bar test/baz/xxx_text.rb Options are: BANNER opts.on("-n [PROCESSES]", Integer, "How many processes to use, default: available CPUs") { |n| options[:count] = n } - opts.on("-p", '--pattern [PATTERN]', "run tests matching this pattern") { |pattern| options[:pattern] = pattern } + opts.on("-p", '--pattern [PATTERN]', "run tests matching this pattern") { |pattern| options[:pattern] = /#{pattern}/ } opts.on("--no-sort", "do not sort files before running them") { |no_sort| options[:no_sort] = no_sort } opts.on("-m [FLOAT]", "--multiply-processes [FLOAT]", Float, "use given number as a multiplier of processes to run") { |multiply| options[:multiply] = multiply } opts.on("-s [PATTERN]", "--single [PATTERN]", "Run all matching files in only one process") do |pattern| options[:single_process] ||= [] options[:single_process] << /#{pattern}/