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

Instance Method Summary collapse

Constructor Details

#initializeundefined

Initialize rspec integration



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

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

Instance Method Details

#all_testsEnumerable<Test>

Available tests

Returns:

  • (Enumerable<Test>)


71
72
73
# File 'lib/mutant/integration/rspec.rb', line 71

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)


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

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

#setupself

Setup rspec integration

Returns:

  • (self)


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

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