Module: Mutineer::TestRunners::RSpecSeed

Defined in:
lib/mutineer/test_runners/rspec_seed.rb

Overview

Gives a random RSpec order the same seed on the clean check and the mutant runs (#282). A defined order is left as it is: assigning config.seed would switch the suite to random. A seed the suite sets (--seed, --order rand:N, SPEC_OPTS, .rspec, or config.seed=) wins. The hooks install before boot, --require files, and spec files load, so a config.seed= in any of those is visible. The seed is written after spec files load and before RSpec orders examples.

A spec helper that only reads config.seed (the generated Kernel.srand config.seed) does not count as setting it. The read still sees RSpec's temporary seed; the examples then run under the pinned seed.

Defined Under Namespace

Modules: PinBeforeOrder, Watch

Class Method Summary collapse

Class Method Details

.advertised_seed ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The seed named in the not-green message. SPEC_OPTS is what the parent can see. A .rspec file or config.seed is applied in the child and is not part of this string.

Returns:

  • (String)


59
60
61
62
63
64
65
66
# File 'lib/mutineer/test_runners/rspec_seed.rb', line 59

def self.advertised_seed
  opts = ENV.fetch("SPEC_OPTS", "")
  found = []
  opts.scan(/--seed(?:=|\s+)(\d+)|--order(?:=|\s+)rand(?:om)?:(\d+)/) do |seed, order|
    found << (seed || order)
  end
  found.last || RunSeed::PINNED_SEED.to_s
end

.embedded_seed?(value) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

True when value is an order string that carries a seed, such as "rand:12" or "random:12". RSpec stores --seed N as "rand:N".

Parameters:

  • value (Object)

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/mutineer/test_runners/rspec_seed.rb', line 48

def self.embedded_seed?(value)
  _order, seed = value.to_s.split(":", 2)
  !seed.nil? && !seed.empty? && seed.match?(/\A\d+\z/)
end

.install! ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Installs the hooks. Idempotent. Requires rspec/core, which the caller has already loaded.



25
26
27
28
29
30
# File 'lib/mutineer/test_runners/rspec_seed.rb', line 25

def self.install!
  return if ::RSpec::Core::Configuration.ancestors.include?(Watch)

  ::RSpec::Core::Configuration.prepend(Watch)
  ::RSpec::Core::World.prepend(PinBeforeOrder)
end

.pin!(config) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Sets the shared seed when the loaded suite's order is random and the suite did not set a seed. Re-seeds Ruby's global generator so rand in examples matches, after a spec helper's Kernel.srand.

Parameters:

  • config (RSpec::Core::Configuration)


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mutineer/test_runners/rspec_seed.rb', line 75

def self.pin!(config)
  return if config.instance_variable_get(:@mutineer_seed_explicit)
  return unless random_order?(config)

  @pinning = true
  begin
    config.seed = RunSeed::PINNED_SEED
    Kernel.srand(config.seed)
  ensure
    @pinning = false
  end
end

.pinning? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

True while pin! is assigning the shared seed, so that assignment is not recorded as the suite's own seed.

Returns:

  • (Boolean)


37
38
39
# File 'lib/mutineer/test_runners/rspec_seed.rb', line 37

def self.pinning?
  @pinning == true
end

.random_order?(config) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

True when the global ordering strategy is RSpec's random one.

Parameters:

  • config (RSpec::Core::Configuration)

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/mutineer/test_runners/rspec_seed.rb', line 93

def self.random_order?(config)
  registry = config.ordering_registry
  registry.fetch(:global).equal?(registry.fetch(:random))
end