Class: KnapsackPro::Runners::Queue::RSpecRunner

Inherits:
BaseRunner
  • Object
show all
Defined in:
lib/knapsack_pro/runners/queue/rspec_runner.rb

Constant Summary collapse

@@used_seed =
nil

Constants inherited from BaseRunner

BaseRunner::TERMINATION_SIGNALS

Class Method Summary collapse

Methods inherited from BaseRunner

#initialize, #test_dir, #test_file_paths

Constructor Details

This class inherits a constructor from KnapsackPro::Runners::Queue::BaseRunner

Class Method Details

.ensure_spec_opts_have_knapsack_pro_formattersObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/knapsack_pro/runners/queue/rspec_runner.rb', line 137

def self.ensure_spec_opts_have_knapsack_pro_formatters
  return unless ENV['SPEC_OPTS']

  if [
      ENV['SPEC_OPTS'].include?(KnapsackPro::Formatters::RSpecQueueSummaryFormatter.to_s),
      ENV['SPEC_OPTS'].include?(KnapsackPro::Formatters::TimeTracker.to_s),
  ].all?
    return
  end

  unless ENV['SPEC_OPTS'].include?(KnapsackPro::Formatters::RSpecQueueSummaryFormatter.to_s)
    ENV['SPEC_OPTS'] = "#{ENV['SPEC_OPTS']} --format #{KnapsackPro::Formatters::RSpecQueueSummaryFormatter}"
  end

  unless ENV['SPEC_OPTS'].include?(KnapsackPro::Formatters::TimeTracker.to_s)
    ENV['SPEC_OPTS'] = "#{ENV['SPEC_OPTS']} --format #{KnapsackPro::Formatters::TimeTracker}"
  end
end

.run(args) ⇒ Object



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
44
45
46
47
48
49
50
# File 'lib/knapsack_pro/runners/queue/rspec_runner.rb', line 9

def self.run(args)
  require 'rspec/core'
  require_relative '../../formatters/time_tracker'
  require_relative '../../formatters/time_tracker_fetcher'
  require_relative '../../formatters/rspec_queue_summary_formatter'
  require_relative '../../formatters/rspec_queue_profile_formatter_extension'

  ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN'] = KnapsackPro::Config::Env.test_suite_token_rspec
  ENV['KNAPSACK_PRO_QUEUE_RECORDING_ENABLED'] = 'true'
  ENV['KNAPSACK_PRO_QUEUE_ID'] = KnapsackPro::Config::EnvGenerator.set_queue_id

  KnapsackPro::Config::Env.set_test_runner_adapter(adapter_class)
  runner = new(adapter_class)

  cli_args = (args || '').split
  adapter_class.ensure_no_tag_option_when_rspec_split_by_test_examples_enabled!(cli_args)

  # when format option is not defined by user then use progress formatter to show tests execution progress
  cli_args += ['--format', 'progress'] unless adapter_class.has_format_option?(cli_args)

  cli_args += [
    # shows summary of all tests executed in Queue Mode at the very end
    '--format', KnapsackPro::Formatters::RSpecQueueSummaryFormatter.to_s,
    '--format', KnapsackPro::Formatters::TimeTracker.to_s,
    '--default-path', runner.test_dir,
  ]

  accumulator = {
    status: :next,
    runner: runner,
    can_initialize_queue: true,
    args: cli_args,
    exitstatus: 0,
    all_test_file_paths: [],
  }
  while accumulator[:status] == :next
    handle_signal!
    accumulator = run_tests(accumulator)
  end

  Kernel.exit(accumulator[:exitstatus])
end

.run_tests(accumulator) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/knapsack_pro/runners/queue/rspec_runner.rb', line 52

def self.run_tests(accumulator)
  runner = accumulator.fetch(:runner)
  can_initialize_queue = accumulator.fetch(:can_initialize_queue)
  args = accumulator.fetch(:args)
  exitstatus = accumulator.fetch(:exitstatus)
  all_test_file_paths = accumulator.fetch(:all_test_file_paths)

  test_file_paths = runner.test_file_paths(
    can_initialize_queue: can_initialize_queue,
    executed_test_files: all_test_file_paths
  )

  if test_file_paths.empty?
    unless all_test_file_paths.empty?
      KnapsackPro::Adapters::RSpecAdapter.verify_bind_method_called

      KnapsackPro::Formatters::RSpecQueueSummaryFormatter.print_summary
      KnapsackPro::Formatters::RSpecQueueProfileFormatterExtension.print_summary

      args += ['--seed', @@used_seed] if @@used_seed

      log_rspec_command(args, all_test_file_paths, :end_of_queue)
    end

    KnapsackPro::Hooks::Queue.call_after_queue

    time_tracker = KnapsackPro::Formatters::TimeTrackerFetcher.call
    KnapsackPro::Report.save_node_queue_to_api(time_tracker&.queue(all_test_file_paths) || [])

    return {
      status: :completed,
      exitstatus: exitstatus,
    }
  else
    subset_queue_id = KnapsackPro::Config::EnvGenerator.set_subset_queue_id
    ENV['KNAPSACK_PRO_SUBSET_QUEUE_ID'] = subset_queue_id

    KnapsackPro::Hooks::Queue.call_before_subset_queue

    all_test_file_paths += test_file_paths
    cli_args = args + test_file_paths

    ensure_spec_opts_have_knapsack_pro_formatters
    options = ::RSpec::Core::ConfigurationOptions.new(cli_args)
    rspec_runner = ::RSpec::Core::Runner.new(options)

    begin
      exit_code = rspec_runner.run($stderr, $stdout)
      exitstatus = exit_code if exit_code != 0
    rescue Exception => exception
      KnapsackPro.logger.error("Having exception when running RSpec: #{exception.inspect}")
      KnapsackPro::Formatters::RSpecQueueSummaryFormatter.print_exit_summary(all_test_file_paths)
      KnapsackPro::Hooks::Queue.call_after_subset_queue
      KnapsackPro::Hooks::Queue.call_after_queue
      Kernel.exit(1)
      raise
    else
      if rspec_runner.world.wants_to_quit
        KnapsackPro.logger.warn('RSpec wants to quit.')
        set_terminate_process
      end
      if rspec_runner.world.respond_to?(:rspec_is_quitting) && rspec_runner.world.rspec_is_quitting
        KnapsackPro.logger.warn('RSpec is quitting.')
        set_terminate_process
      end

      printable_args = args_with_seed_option_added_when_viable(args, rspec_runner)
      log_rspec_command(printable_args, test_file_paths, :subset_queue)

      rspec_clear_examples

      KnapsackPro::Hooks::Queue.call_after_subset_queue

      return {
        status: :next,
        runner: runner,
        can_initialize_queue: false,
        args: args,
        exitstatus: exitstatus,
        all_test_file_paths: all_test_file_paths,
      }
    end
  end
end