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.

rubocop:disable Metrics/ClassLength

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



41
42
43
44
45
# File 'lib/mutant/integration/rspec.rb', line 41

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>)


90
91
92
# File 'lib/mutant/integration/rspec.rb', line 90

def all_tests
  all_tests_index.keys
end

#available_testsEnumerable<Test>

Available tests

Returns:

  • (Enumerable<Test>)


98
99
100
# File 'lib/mutant/integration/rspec.rb', line 98

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

#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)


71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mutant/integration/rspec.rb', line 71

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(
    output:  '',
    passed:  passed,
    runtime: timer.now - start
  )
end

#freezeObject



33
34
35
36
# File 'lib/mutant/integration/rspec.rb', line 33

def freeze
  super() if @setup_elapsed
  self
end

#setupself

Setup rspec integration

Returns:

  • (self)


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mutant/integration/rspec.rb', line 50

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