require 'optparse' require 'parallel_tests/test/runner' 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 report_time_taken do groups = runner.tests_in_groups(options[:files], num_processes, options) abort "no #{name}s found!" if groups.size == 0 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" 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 #parse and print results results = runner.find_results(test_results.map { |result| result[:stdout] }*"") puts "" puts runner.summarize_results(results) 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 end end private def self.parse_options!(argv) options = {} OptionParser.new do |opts| opts.banner = < num_processes) do |i| ParallelTests::Test::Runner.execute_command(command, i, options) end end.flatten abort if results.any? { |r| r[:exit_status] != 0 } end def self.report_time_taken start = Time.now yield puts "" puts "Took #{Time.now - start} seconds" end end end