Class: ParallelSpecs::Test::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel_specs/test/runner.rb

Direct Known Subclasses

RSpec::Runner

Constant Summary collapse

RuntimeLogTooSmallError =
Class.new(StandardError)
RuntimeLogParseError =
Class.new(StandardError)
MissingTestFileError =
Class.new(ArgumentError)
OUTPUT_MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.command_with_seed(command, seed) ⇒ Object



106
107
108
# File 'lib/parallel_specs/test/runner.rb', line 106

def command_with_seed(command, seed)
  [*remove_command_arguments(command, "--seed"), "--seed", seed]
end

.execute_command(cmd, process_number, num_processes, options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/parallel_specs/test/runner.rb', line 60

def execute_command(cmd, process_number, num_processes, options)
  env = {
    "TEST_ENV_NUMBER" => test_env_number(process_number),
    "PARALLEL_SPECS_GROUPS" => num_processes.to_s,
    "PARALLEL_SPECS_PID_FILE" => ParallelSpecs.pid_file_path
  }

  if (dashboard_event_files = options[:dashboard_event_files])
    env["PARALLEL_SPECS_DASHBOARD_EVENT_LOG"] = dashboard_event_files.fetch(process_number)
  end

  execute_command_and_capture_output(env, cmd, options)
end

.execute_command_and_capture_output(env, cmd, options) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/parallel_specs/test/runner.rb', line 74

def execute_command_and_capture_output(env, cmd, options)
  pid = nil
  output = IO.popen(env, cmd, err: [:child, :out]) do |io|
    pid = io.pid
    ParallelSpecs.pids.add(pid)
    capture_output(io, options[:dashboard])
  ensure
    ParallelSpecs.pids.delete(pid) if pid
  end

  status = $?
  exit_status = if status.exitstatus
    status.exitstatus
  elsif status.termsig
    status.termsig + 128
  else
    1
  end

  {env: env, stdout: output, exit_status: exit_status, command: cmd, seed: seed_from(output)}
end

.find_results(test_output) ⇒ Object



110
111
112
113
114
115
# File 'lib/parallel_specs/test/runner.rb', line 110

def find_results(test_output)
  test_output.lines.filter_map do |line|
    line = line.chomp.gsub(/\e\[\d+m/, "")
    line if line_is_result?(line)
  end
end


96
97
98
99
100
# File 'lib/parallel_specs/test/runner.rb', line 96

def print_command(command, env = {})
  env_string = rerun_env(env).map { |key, value| "#{key}=#{Shellwords.escape(value)}" }.join(" ")
  command_string = Shellwords.shelljoin(command)
  puts [env_string, command_string].reject(&:empty?).join(" ")
end

.rerun_command(command, seed: nil) ⇒ Object



102
103
104
# File 'lib/parallel_specs/test/runner.rb', line 102

def rerun_command(command, seed: nil)
  seed ? command_with_seed(command, seed) : command
end

.summarize_results(results) ⇒ Object



117
118
119
# File 'lib/parallel_specs/test/runner.rb', line 117

def summarize_results(results)
  sum_up_results(results).sort.map { |word, count| "#{count} #{word}#{"s" if count != 1}" }.join(", ")
end

.tests_in_groups(tests, num_groups, options = {}) ⇒ Object



15
16
17
# File 'lib/parallel_specs/test/runner.rb', line 15

def tests_in_groups(tests, num_groups, options = {})
  ParallelSpecs::Grouper.in_even_groups_by_size(tests_with_size(tests, options), num_groups)
end

.tests_with_size(tests, options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/parallel_specs/test/runner.rb', line 19

def tests_with_size(tests, options)
  tests = find_tests(tests, options)

  case options[:group_by]
  when :found
    tests.map! { |test| [test, 1] }
  when :runtime
    sort_by_runtime(
      tests,
      runtimes(tests, options),
      options.merge(allowed_missing: (options[:allowed_missing_percent] || 50) / 100.0)
    )
  when :filesize
    sort_by_filesize(tests)
  when nil
    begin
      known_runtimes = runtimes(tests, options)
    rescue Errno::ENOENT
      warn "parallel_specs: runtime log #{runtime_log_path(options)} was not found; falling back to filesize grouping" if options[:runtime_log]
      known_runtimes = {}
    rescue RuntimeLogParseError => e
      warn "parallel_specs: unable to use runtime log #{runtime_log_path(options)}: #{e.message}; falling back to filesize grouping"
      known_runtimes = {}
    rescue => e
      warn "parallel_specs: unable to load runtime log #{runtime_log_path(options)}: #{e.class}: #{e.message}"
      raise
    end

    if known_runtimes.size * 1.5 > tests.size
      puts "Using recorded test runtime"
      sort_by_runtime(tests, known_runtimes)
    else
      sort_by_filesize(tests)
    end
  else
    raise ArgumentError, "Unsupported option #{options[:group_by]}"
  end

  tests
end