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([^ ]+)(?: )?/.freeze
EXIT_SUCCESS =
0
CLI_OPTIONS =
%w[spec --fail-fast].freeze
TEST_ID_FORMAT =
'rspec:%<index>d:%<location>s/%<description>s'

Instance Method Summary collapse

Constructor Details

#initializeundefined

Initialize rspec integration



35
36
37
38
39
# File 'lib/mutant/integration/rspec.rb', line 35

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

Instance Method Details

#all_testsEnumerable<Test>

Available tests

Returns:

  • (Enumerable<Test>)


69
70
71
# File 'lib/mutant/integration/rspec.rb', line 69

def all_tests
  all_tests_index.keys
end

#call(tests) ⇒ Result::Test

Run a collection of tests

Parameters:

  • tests (Enumerable<Mutant::Test>)

Returns:

  • (Result::Test)


55
56
57
58
59
60
61
62
63
64
# File 'lib/mutant/integration/rspec.rb', line 55

def call(tests)
  examples = tests.map(&all_tests_index)
  filter_examples(&examples.public_method(:include?))
  start = timer.now
  passed = @runner.run_specs(@rspec_world.ordered_example_groups).equal?(EXIT_SUCCESS)
  Result::Test.new(
    passed:  passed,
    runtime: timer.now - start
  )
end

#setupself

Setup rspec integration

Returns:

  • (self)


44
45
46
47
# File 'lib/mutant/integration/rspec.rb', line 44

def setup
  @runner.setup($stderr, $stdout)
  self
end