Class: Mutest::Integration::Rspec

Inherits:
Mutest::Integration show all
Defined in:
lib/mutest/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 =
':'.freeze
EXIT_SUCCESS =
0
CLI_OPTIONS =
IceNine.deep_freeze(%w[spec --fail-fast])
TEST_ID_FORMAT =
'rspec:%<index>d:%<location>s/%<description>s'.freeze

Instance Method Summary collapse

Constructor Details

#initializeundefined

Initialize rspec integration



33
34
35
36
37
38
# File 'lib/mutest/integration/rspec.rb', line 33

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


73
74
75
# File 'lib/mutest/integration/rspec.rb', line 73

def all_tests
  all_tests_index.keys
end

#call(tests) ⇒ Result::Test

Run a collection of tests

rubocop:disable MethodLength

Parameters:

  • tests (Enumerable<Mutest::Test>)

Returns:

  • (Result::Test)


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

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

#setupself

Setup rspec integration

Returns:

  • (self)


43
44
45
46
# File 'lib/mutest/integration/rspec.rb', line 43

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