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

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

Constant Summary

Constants inherited from BaseRunner

BaseRunner::TERMINATION_SIGNALS, BaseRunner::TerminationError

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseRunner

run_tests, #test_dir, #test_file_paths

Constructor Details

#initialize(adapter_class, rspec_pure, args, stream_error, stream_out) ⇒ RSpecRunner

Returns a new instance of RSpecRunner.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/knapsack_pro/runners/queue/rspec_runner.rb', line 23

def initialize(adapter_class, rspec_pure, args, stream_error, stream_out)
  super(adapter_class)
  @adapter_class = adapter_class
  @rspec_pure = rspec_pure
  has_format_option = @adapter_class.has_format_option?((args || '').split)
  @cli_args = rspec_pure.prepare_cli_args(args, has_format_option, test_dir)
  @stream_error = stream_error
  @stream_out = stream_out
  @node_test_file_paths = []
  @rspec_runner = nil # RSpec::Core::Runner is lazy initialized
end

Class Method Details

.run(args, stream_error = $stderr, stream_out = $stdout) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/knapsack_pro/runners/queue/rspec_runner.rb', line 7

def self.run(args, stream_error = $stderr, stream_out = $stdout)
  require 'rspec/core'
  require_relative '../../extensions/rspec_extension'
  require_relative '../../formatters/time_tracker'
  require_relative '../../formatters/time_tracker_fetcher'

  KnapsackPro::Extensions::RSpecExtension.setup!

  ENV['KNAPSACK_PRO_TEST_SUITE_TOKEN'] = KnapsackPro::Config::Env.test_suite_token_rspec

  rspec_pure = KnapsackPro::Pure::Queue::RSpecPure.new

  queue_runner = new(KnapsackPro::Adapters::RSpecAdapter, rspec_pure, args, stream_error, stream_out)
  queue_runner.run
end

Instance Method Details

#handle_signal!Object



99
100
101
# File 'lib/knapsack_pro/runners/queue/rspec_runner.rb', line 99

def handle_signal!
  self.class.handle_signal!
end

#log_fail_fast_limit_metObject



103
104
105
# File 'lib/knapsack_pro/runners/queue/rspec_runner.rb', line 103

def log_fail_fast_limit_met
  KnapsackPro.logger.warn('Test execution has been canceled because the RSpec --fail-fast option is enabled. It will cause other CI nodes to run tests longer because they need to consume more tests from the Knapsack Pro Queue API.')
end

#runFixnum

Returns:

  • (Fixnum)

    exit status code. 0 if all specs passed, or the configured failure exit code (1 by default) if specs failed.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/knapsack_pro/runners/queue/rspec_runner.rb', line 41

def run
  pre_run_setup

  if @rspec_runner.knapsack__wants_to_quit?
    exit_code = @rspec_runner.knapsack__exit_early
    Kernel.exit(exit_code)
  end

  begin
    exit_code = @rspec_runner.knapsack__run_specs(self)
  rescue KnapsackPro::Runners::Queue::BaseRunner::TerminationError
    exit_code = @rspec_pure.error_exit_code(@rspec_runner.knapsack__error_exit_code)
    Kernel.exit(exit_code)
  rescue Exception => exception
    KnapsackPro.logger.error("An unexpected exception happened. RSpec cannot handle it. The exception: #{exception.inspect}")

    message = @rspec_pure.exit_summary(unexecuted_test_files)
    KnapsackPro.logger.warn(message) if message

    exit_code = @rspec_pure.error_exit_code(@rspec_runner.knapsack__error_exit_code)
    Kernel.exit(exit_code)
  end

  post_run_tasks(exit_code)
end

#with_batchObject



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
# File 'lib/knapsack_pro/runners/queue/rspec_runner.rb', line 67

def with_batch
  can_initialize_queue = true

  loop do
    handle_signal!
    test_file_paths = pull_tests_from_queue(can_initialize_queue: can_initialize_queue)
    can_initialize_queue = false

    break if test_file_paths.empty?

    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

    yield test_file_paths

    KnapsackPro::Hooks::Queue.call_after_subset_queue

    if @rspec_runner.knapsack__wants_to_quit?
      KnapsackPro.logger.warn('RSpec wants to quit.')
      set_terminate_process
    end
    if @rspec_runner.knapsack__rspec_is_quitting?
      KnapsackPro.logger.warn('RSpec is quitting.')
      set_terminate_process
    end

    log_rspec_batch_command(test_file_paths)
  end
end