Class: Mutant::Integration::Rspec

Inherits:
Mutant::Integration show all
Defined in:
lib/mutant/integration/rspec.rb

Overview

Rspec integration

This looks so complicated, because rspec:

  • Keeps its state global in RSpec.world and lots of other places
  • There is no API to "just run a subset of examples", the examples need to be selected in-place via mutating the RSpec.filtered_examples data structure
  • Does not maintain a unique identification for an example, aside the instances of RSpec::Core::Example objects itself. For that reason identifying examples by:
    • full description
    • location Is NOT enough. It would not be unique. So we add an "example index" for unique reference.

Defined Under Namespace

Classes: Ordering

Constant Summary collapse

ALL_EXPRESSION =
Expression::Namespace::Recursive.new(scope_name: nil)
EXPRESSION_CANDIDATE =
/\A([^ ]+)(?: )?/
EXIT_SUCCESS =
0
DEFAULT_CLI_OPTIONS =
%w[--fail-fast spec].freeze
TEST_ID_FORMAT =
'rspec:%<index>d:%<location>s/%<description>s'

Instance Method Summary collapse

Constructor Details

#initializeundefined

Initialize rspec integration



81
82
83
84
85
86
# File 'lib/mutant/integration/rspec.rb', line 81

def initialize(*)
  super
  @ordering    = Ordering.new
  @runner      = RSpec::Core::Runner.new(RSpec::Core::ConfigurationOptions.new(effective_arguments))
  @rspec_world = RSpec.world
end

Instance Method Details

#all_testsEnumerable<Test>

All tests

Returns:

  • (Enumerable<Test>)


137
# File 'lib/mutant/integration/rspec.rb', line 137

def all_tests = all_tests_index.keys

#available_testsEnumerable<Test>

Available tests

Returns:

  • (Enumerable<Test>)


143
# File 'lib/mutant/integration/rspec.rb', line 143

def available_tests = all_tests_index.select { |_test, example| example..fetch(:mutant, true) }.keys

#call(tests) ⇒ Result::Test

Run a collection of tests, likeliest killer first

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength

Parameters:

  • tests (Enumerable<Mutant::Test>)

Returns:

  • (Result::Test)


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mutant/integration/rspec.rb', line 112

def call(tests)
  examples = tests.map(&all_tests_index)

  install_ordering
  reset_examples
  @ordering.call(examples)
  setup_examples(examples)
  @runner.configuration.start_time = world.time.now - @setup_elapsed
  start = timer.now
  passed = @runner.run_specs(@rspec_world.ordered_example_groups).equal?(EXIT_SUCCESS)
  @runner.configuration.reset_reporter

  Result::Test.new(
    job_index: nil,
    output:    LogCapture::String.new(content: ''),
    passed:,
    runtime:   timer.now - start
  )
end

#freezeObject

Ordering



76
# File 'lib/mutant/integration/rspec.rb', line 76

def freeze = tap { super if @setup_elapsed }

#setupself

Setup rspec integration

Returns:

  • (self)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mutant/integration/rspec.rb', line 91

def setup
  @setup_elapsed = timer.elapsed do
    @runner.setup(world.stderr, world.stdout)
    fail 'RSpec setup failure' if rspec_setup_failure?
    example_group_map
  end
  @runner.configuration.force(color_mode: :on)
  @runner.configuration.reporter
  reset_examples
  freeze
end