Class: KnapsackPro::Adapters::RSpecAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/knapsack_pro/adapters/rspec_adapter.rb

Direct Known Subclasses

RspecAdapter

Constant Summary collapse

TEST_DIR_PATTERN =
'spec/**{,/*/**}/*_spec.rb'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseAdapter

adapter_bind_method_called_file, bind, #bind, #bind_queue_mode, slow_test_file?, verify_bind_method_called

Class Method Details

.ensure_no_tag_option_when_rspec_split_by_test_examples_enabled!(cli_args) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/knapsack_pro/adapters/rspec_adapter.rb', line 37

def self.ensure_no_tag_option_when_rspec_split_by_test_examples_enabled!(cli_args)
  if KnapsackPro::Config::Env.rspec_split_by_test_examples? && has_tag_option?(cli_args)
    error_message = "It is not allowed to use the RSpec tag option together with the RSpec split by test examples feature. Please see: #{KnapsackPro::Urls::RSPEC__SPLIT_BY_TEST_EXAMPLES__TAG}"
    KnapsackPro.logger.error(error_message)
    raise error_message
  end
end

.has_format_option?(cli_args) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/knapsack_pro/adapters/rspec_adapter.rb', line 49

def self.has_format_option?(cli_args)
  !!parsed_options(cli_args)&.[](:formatters)
end

.has_tag_option?(cli_args) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/knapsack_pro/adapters/rspec_adapter.rb', line 45

def self.has_tag_option?(cli_args)
  !!parsed_options(cli_args)&.[](:inclusion_filter)
end

.order_option(cli_args) ⇒ Object



53
54
55
# File 'lib/knapsack_pro/adapters/rspec_adapter.rb', line 53

def self.order_option(cli_args)
  parsed_options(cli_args)&.[](:order)
end

.split_by_test_cases_enabled?Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
# File 'lib/knapsack_pro/adapters/rspec_adapter.rb', line 8

def self.split_by_test_cases_enabled?
  return false unless KnapsackPro::Config::Env.rspec_split_by_test_examples?

  require 'rspec/core/version'
  unless Gem::Version.new(::RSpec::Core::Version::STRING) >= Gem::Version.new('3.3.0')
    raise "RSpec >= 3.3.0 is required to split test files by test examples. Learn more: #{KnapsackPro::Urls::SPLIT_BY_TEST_EXAMPLES}"
  end

  true
end

.test_file_cases_for(slow_test_files) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/knapsack_pro/adapters/rspec_adapter.rb', line 19

def self.test_file_cases_for(slow_test_files)
  KnapsackPro.logger.info("Generating RSpec test examples JSON report for slow test files to prepare it to be split by test examples (by individual test cases). Thanks to that, a single slow test file can be split across parallel CI nodes. Analyzing #{slow_test_files.size} slow test files.")

  # generate the RSpec JSON report in a separate process to not pollute the RSpec state
  cmd = [
    'RACK_ENV=test',
    'RAILS_ENV=test',
    KnapsackPro::Config::Env.rspec_test_example_detector_prefix,
    'rake knapsack_pro:rspec_test_example_detector',
  ].join(' ')
  unless Kernel.system(cmd)
    raise "Could not generate JSON report for RSpec. Rake task failed when running #{cmd}"
  end

  # read the JSON report
  KnapsackPro::TestCaseDetectors::RSpecTestExampleDetector.new.test_file_example_paths
end

.test_path(example) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/knapsack_pro/adapters/rspec_adapter.rb', line 57

def self.test_path(example)
  example_group = example.[:example_group]

  if defined?(::Turnip) && Gem::Version.new(::Turnip::VERSION) < Gem::Version.new('2.0.0')
    unless example_group[:turnip]
      until example_group[:parent_example_group].nil?
        example_group = example_group[:parent_example_group]
      end
    end
  else
    until example_group[:parent_example_group].nil?
      example_group = example_group[:parent_example_group]
    end
  end

  example_group[:file_path]
end

Instance Method Details

#bind_before_queue_hookObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/knapsack_pro/adapters/rspec_adapter.rb', line 126

def bind_before_queue_hook
  ::RSpec.configure do |config|
    config.before(:suite) do
      unless ENV['KNAPSACK_PRO_BEFORE_QUEUE_HOOK_CALLED']
        ENV['KNAPSACK_PRO_BEFORE_QUEUE_HOOK_CALLED'] = 'true'
        KnapsackPro::Hooks::Queue.call_before_queue
      end
    end
  end
end

#bind_save_reportObject



118
119
120
121
122
123
124
# File 'lib/knapsack_pro/adapters/rspec_adapter.rb', line 118

def bind_save_report
  ::RSpec.configure do |config|
    config.after(:suite) do
      KnapsackPro::Report.save
    end
  end
end

#bind_time_trackerObject



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
# File 'lib/knapsack_pro/adapters/rspec_adapter.rb', line 75

def bind_time_tracker
  ::RSpec.configure do |config|
    config.prepend_before(:context) do
      KnapsackPro.tracker.start_timer
    end

    config.around(:each) do |example|
      current_test_path = KnapsackPro::Adapters::RSpecAdapter.test_path(example)

      # Stop timer to update time for a previously run test example.
      # This way we count time spent in runtime for the previous test example after around(:each) is already done.
      # Only do that if we're in the same test file. Otherwise, `before(:all)` execution time in the current file
      # will be applied to the previously ran test file.
      if KnapsackPro.tracker.current_test_path&.start_with?(KnapsackPro::TestFileCleaner.clean(current_test_path))
        KnapsackPro.tracker.stop_timer
      end

      KnapsackPro.tracker.current_test_path =
        if KnapsackPro::Config::Env.rspec_split_by_test_examples? && KnapsackPro::Adapters::RSpecAdapter.slow_test_file?(RSpecAdapter, current_test_path)
          example.id
        else
          current_test_path
        end

      if example.[:focus] && KnapsackPro::Adapters::RSpecAdapter.rspec_configuration.filter.rules[:focus]
        raise "We detected a test file path #{current_test_path} with a test using the metadata `:focus` tag. RSpec might not run some tests in the Queue Mode (causing random tests skipping problem). Please remove the `:focus` tag from your codebase. See more: #{KnapsackPro::Urls::RSPEC__SKIPS_TESTS}"
      end

      example.run
    end

    config.append_after(:context) do
      # after(:context) hook is run one time only, after all of the examples in a group
      # stop timer to count time for the very last executed test example
      KnapsackPro.tracker.stop_timer
    end

    config.after(:suite) do
      KnapsackPro.logger.debug(KnapsackPro::Presenter.global_time)
    end
  end
end