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.

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



37
38
39
40
41
# File 'lib/mutant/integration/rspec.rb', line 37

def initialize(*)
  super
  @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>)


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

def all_tests = all_tests_index.keys

#available_testsEnumerable<Test>

Available tests

Returns:

  • (Enumerable<Test>)


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

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

#call(tests) ⇒ Result::Test

Run a collection of tests

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

Parameters:

  • tests (Enumerable<Mutant::Test>)

Returns:

  • (Result::Test)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mutant/integration/rspec.rb', line 67

def call(tests)
  reset_examples
  setup_examples(tests.map(&all_tests_index))
  @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



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

def freeze = tap { super if @setup_elapsed }

#setupself

Setup rspec integration

Returns:

  • (self)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mutant/integration/rspec.rb', line 46

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