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
41
# File 'lib/mutant/integration/rspec.rb', line 36

def initialize(*)
  super
  @output = StringIO.new
  @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>)


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

def all_tests
  all_tests_index.keys
end

#call(tests) ⇒ Result::Test

Run a collection of tests

rubocop:disable Metrics/MethodLength

Parameters:

  • tests (Enumerable<Mutant::Test>)

Returns:

  • (Result::Test)


59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mutant/integration/rspec.rb', line 59

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)
  @output.rewind
  Result::Test.new(
    output:  @output.read,
    passed:  passed,
    runtime: Timer.now - start,
    tests:   tests
  )
end

#setupself

Setup rspec integration

Returns:

  • (self)


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

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