Class: TurboTests::Runner

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Runner

Returns a new instance of Runner.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/turbo_tests/runner.rb', line 53

def initialize(opts)
  @reporter = opts[:reporter]
  @files = opts[:files]
  @verbose = opts[:verbose]
  @fail_fast = opts[:fail_fast]
  @use_runtime_info = opts[:use_runtime_info]
  @seed = opts[:seed]
  @profile = opts[:profile]
  @retry_and_log_flaky_tests = opts[:retry_and_log_flaky_tests]
  @failure_count = 0

  @messages = Queue.new
  @threads = []
  @error = false
end

Class Method Details

.default_spec_foldersObject



45
46
47
48
49
50
51
# File 'lib/turbo_tests/runner.rb', line 45

def self.default_spec_folders
  # We do not want to include system specs by default, they are quite slow.
  Dir
    .entries("#{Rails.root}/spec")
    .reject { |entry| !File.directory?("spec/#{entry}") || %w[.. . system].include?(entry) }
    .map { |entry| "spec/#{entry}" }
end

.run(opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
# File 'lib/turbo_tests/runner.rb', line 5

def self.run(opts = {})
  files = opts[:files]
  formatters = opts[:formatters]
  seed = opts[:seed]
  start_time = opts.fetch(:start_time) { Time.now }
  verbose = opts.fetch(:verbose, false)
  fail_fast = opts.fetch(:fail_fast, nil)
  use_runtime_info = opts.fetch(:use_runtime_info, false)
  retry_and_log_flaky_tests = opts.fetch(:retry_and_log_flaky_tests, false)

  STDOUT.puts "VERBOSE" if verbose

  reporter =
    Reporter.from_config(
      formatters,
      start_time,
      max_timings_count: opts[:profile_print_slowest_examples_count],
    )

  if ENV["GITHUB_ACTIONS"]
    RSpec.configure do |config|
      # Enable color output in GitHub Actions
      # This eventually will be `config.color_mode = :on` in RSpec 4?
      config.tty = true
      config.color = true
    end
  end

  new(
    reporter: reporter,
    files: files,
    verbose: verbose,
    fail_fast: fail_fast,
    use_runtime_info: use_runtime_info,
    seed: seed,
    profile: opts[:profile],
    retry_and_log_flaky_tests: retry_and_log_flaky_tests,
  ).run
end

Instance Method Details

#runObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/turbo_tests/runner.rb', line 69

def run
  check_for_migrations

  @num_processes = ParallelTests.determine_number_of_processes(nil)

  group_opts = {}
  group_opts[:runtime_log] = "tmp/turbo_rspec_runtime.log" if @use_runtime_info

  tests_in_groups =
    ParallelTests::RSpec::Runner.tests_in_groups(@files, @num_processes, **group_opts)

  setup_tmp_dir

  @reporter.add_formatter(Flaky::FailuresLoggerFormatter.new) if @retry_and_log_flaky_tests

  subprocess_opts = { record_runtime: @use_runtime_info }

  start_multisite_subprocess(@files, **subprocess_opts)

  tests_in_groups.each_with_index do |tests, process_id|
    start_regular_subprocess(tests, process_id + 1, **subprocess_opts)
  end

  @reporter.start

  handle_messages

  @reporter.finish

  @threads.each(&:join)

  if @retry_and_log_flaky_tests && @reporter.failed_examples.present?
    retry_failed_examples_threshold = 10

    if @reporter.failed_examples.length <= retry_failed_examples_threshold
      STDOUT.puts "Retrying failed examples and logging flaky tests..."
      return rerun_failed_examples(@reporter.failed_examples)
    else
      STDOUT.puts "Retry and log flaky tests was enabled but ignored because there are more than #{retry_failed_examples_threshold} failures."
      Flaky::Manager.remove_flaky_tests
    end
  end

  @reporter.failed_examples.empty? && !@error
end