Class: KnapsackPro::Runners::RSpecRunner

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

Class Method Summary collapse

Methods inherited from BaseRunner

#initialize, #stringify_test_file_paths, #test_dir, #test_file_paths, #test_files_to_execute_exist?

Constructor Details

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

Class Method Details

.formattersObject

Use RSpec::Core::ConfigurationOptions to respect external configurations like .rspec



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/knapsack_pro/runners/rspec_runner.rb', line 42

def self.formatters
  require_relative '../formatters/time_tracker'

  formatters = ::RSpec::Core::ConfigurationOptions
    .new([])
    .options
    .fetch(:formatters, [])
    .map do |formatter, output|
      arg = "--format #{formatter}"
      arg += " --out #{output}" if output
      arg
    end
  formatters = ['--format progress'] if formatters.empty?
  formatters += ["--format #{KnapsackPro::Formatters::TimeTracker}"]
  formatters.join(' ')
end

.run(args) ⇒ Object



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

def self.run(args)
  require 'rspec/core'

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

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

  if runner.test_files_to_execute_exist?
    adapter_class.verify_bind_method_called

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

    require 'rspec/core/rake_task'

    task_name = 'knapsack_pro:rspec_run'
    if Rake::Task.task_defined?(task_name)
      Rake::Task[task_name].clear
    end

    ::RSpec::Core::RakeTask.new(task_name) do |t|
      # we cannot pass runner.test_file_paths array to t.pattern
      # because pattern does not accept test example path like spec/a_spec.rb[1:2]
      # instead we pass test files and test example paths to t.rspec_opts
      t.pattern = []
      t.rspec_opts = "#{args} #{self.formatters} --default-path #{runner.test_dir} #{runner.stringify_test_file_paths}"
      t.verbose = KnapsackPro::Config::Env.log_level < ::Logger::WARN
    end
    Rake::Task[task_name].invoke
  end
end