class ParallelTests::Test::Runner
Constants
- NAME
Public Class Methods
command_with_seed(cmd, seed)
click to toggle source
remove old seed and add new seed
# File lib/parallel_tests/test/runner.rb, line 127 def command_with_seed(cmd, seed) clean = cmd.sub(/\s--seed\s+\d+\b/, '') "#{clean} --seed #{seed}" end
execute_command(cmd, process_number, num_processes, options)
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 72 def execute_command(cmd, process_number, num_processes, options) env = (options[:env] || {}).merge( "TEST_ENV_NUMBER" => test_env_number(process_number, options).to_s, "PARALLEL_TEST_GROUPS" => num_processes.to_s, "PARALLEL_PID_FILE" => ParallelTests.pid_file_path, ) cmd = "nice #{cmd}" if options[:nice] cmd = "#{cmd} 2>&1" if options[:combine_stderr] puts cmd if report_process_command?(options) && !options[:serialize_stdout] execute_command_and_capture_output(env, cmd, options) end
execute_command_and_capture_output(env, cmd, options)
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 86 def execute_command_and_capture_output(env, cmd, options) pid = nil output = IO.popen(env, cmd) do |io| pid = io.pid ParallelTests.pids.add(pid) capture_output(io, env, options) end ParallelTests.pids.delete(pid) if pid exitstatus = $?.exitstatus seed = output[/seed (\d+)/,1] if report_process_command?(options) && options[:serialize_stdout] output = [cmd, output].join("\n") end {:stdout => output, :exit_status => exitstatus, :command => cmd, :seed => seed} end
find_results(test_output)
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 104 def find_results(test_output) test_output.lines.map do |line| line.chomp! line.gsub!(/\e\[\d+m/, '') # remove color coding next unless line_is_result?(line) line end.compact end
line_is_result?(line)
click to toggle source
ignores other commands runner noise
# File lib/parallel_tests/test/runner.rb, line 34 def line_is_result?(line) line =~ /\d+ failure(?!:)/ end
name()
click to toggle source
— usually overwritten by other runners
# File lib/parallel_tests/test/runner.rb, line 11 def name NAME end
run_tests(test_files, process_number, num_processes, options)
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 27 def run_tests(test_files, process_number, num_processes, options) require_list = test_files.map { |file| file.sub(" ", "\\ ") }.join(" ") cmd = "#{executable} -Itest -e '%w[#{require_list}].each { |f| require %{./\#{f}} }' -- #{options[:test_options]}" execute_command(cmd, process_number, num_processes, options) end
runtime_log()
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 15 def runtime_log 'tmp/parallel_runtime_test.log' end
summarize_results(results)
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 121 def summarize_results(results) sums = sum_up_results(results) sums.sort.map{|word, number| "#{number} #{word}#{'s' if number != 1}" }.join(', ') end
test_env_number(process_number, options={})
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 113 def test_env_number(process_number, options={}) if process_number == 0 && !options[:first_is_1] '' else process_number + 1 end end
test_file_name()
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 23 def test_file_name "test" end
test_suffix()
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 19 def test_suffix /_(test|spec).rb$/ end
tests_in_groups(tests, num_groups, options={})
click to toggle source
finds all tests and partitions them into groups
# File lib/parallel_tests/test/runner.rb, line 41 def tests_in_groups(tests, num_groups, options={}) tests = tests_with_size(tests, options) Grouper.in_even_groups_by_size(tests, num_groups, options) end
tests_with_size(tests, options)
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 46 def tests_with_size(tests, options) tests = find_tests(tests, options) case options[:group_by] when :found tests.map! { |t| [t, 1] } when :filesize sort_by_filesize(tests) when :runtime sort_by_runtime(tests, runtimes(tests, options), options.merge(allowed_missing: (options[:allowed_missing_percent] || 50) / 100.0)) when nil # use recorded test runtime if we got enough data runtimes = runtimes(tests, options) rescue [] if runtimes.size * 1.5 > tests.size puts "Using recorded test runtime" sort_by_runtime(tests, runtimes) else sort_by_filesize(tests) end else raise ArgumentError, "Unsupported option #{options[:group_by]}" end tests end
Protected Class Methods
capture_output(out, env, options={})
click to toggle source
read output of the process and print it in chunks
# File lib/parallel_tests/test/runner.rb, line 152 def capture_output(out, env, options={}) result = "" loop do begin read = out.readpartial(1000000) # read whatever chunk we can get if Encoding.default_internal read = read.force_encoding(Encoding.default_internal) end result << read unless options[:serialize_stdout] message = read message = "[TEST GROUP #{env['TEST_ENV_NUMBER']}] #{message}" if options[:prefix_output_with_test_env_number] $stdout.print message $stdout.flush end end end rescue EOFError result end
determine_executable()
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 138 def determine_executable "ruby" end
executable()
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 134 def executable ENV['PARALLEL_TESTS_EXECUTABLE'] || determine_executable end
files_in_folder(folder, options={})
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 226 def files_in_folder(folder, options={}) pattern = if options[:symlinks] == false # not nil or true "**/*" else # follow one symlink and direct children # http://stackoverflow.com/questions/357754/can-i-traverse-symlinked-directories-in-ruby-with-a-glob "**{,/*/**}/*" end Dir[File.join(folder, pattern)].uniq end
find_tests(tests, options = {})
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 209 def find_tests(tests, options = {}) suffix_pattern = options[:suffix] || test_suffix include_pattern = options[:pattern] || // exclude_pattern = options[:exclude_pattern] (tests || []).flat_map do |file_or_folder| if File.directory?(file_or_folder) files = files_in_folder(file_or_folder, options) files = files.grep(suffix_pattern).grep(include_pattern) files -= files.grep(exclude_pattern) if exclude_pattern files else file_or_folder end end.uniq end
runtimes(tests, options)
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 194 def runtimes(tests, options) log = options[:runtime_log] || runtime_log lines = File.read(log).split("\n") lines.each_with_object({}) do |line, times| test, _, time = line.rpartition(':') next unless test and time times[test] = time.to_f if tests.include?(test) end end
sort_by_filesize(tests)
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 204 def sort_by_filesize(tests) tests.sort! tests.map! { |test| [test, File.stat(test).size] } end
sort_by_runtime(tests, runtimes, options={})
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 172 def sort_by_runtime(tests, runtimes, options={}) allowed_missing = options[:allowed_missing] || 1.0 allowed_missing = tests.size * allowed_missing # set know runtime for each test tests.sort! tests.map! do |test| allowed_missing -= 1 unless time = runtimes[test] if allowed_missing < 0 log = options[:runtime_log] || runtime_log raise "Runtime log file '#{log}' does not contain sufficient data to sort #{tests.size} test files, please update it." end [test, time] end if options[:verbose] puts "Runtime found for #{tests.count(&:last)} of #{tests.size} tests" end set_unknown_runtime tests, options end
sum_up_results(results)
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 142 def sum_up_results(results) results = results.join(' ').gsub(/s\b/,'') # combine and singularize results counts = results.scan(/(\d+) (\w+)/) counts.inject(Hash.new(0)) do |sum, (number, word)| sum[word] += number.to_i sum end end
Private Class Methods
report_process_command?(options)
click to toggle source
# File lib/parallel_tests/test/runner.rb, line 249 def report_process_command?(options) options[:verbose] || options[:verbose_process_command] end
set_unknown_runtime(tests, options)
click to toggle source
fill gaps with unknown-runtime if given, average otherwise NOTE: an optimization could be doing runtime by average runtime per file size, but would need file checks
# File lib/parallel_tests/test/runner.rb, line 241 def set_unknown_runtime(tests, options) known, unknown = tests.partition(&:last) return if unknown.empty? unknown_runtime = options[:unknown_runtime] || (known.empty? ? 1 : known.map!(&:last).inject(:+) / known.size) # average unknown.each { |set| set[1] = unknown_runtime } end