Renamed Minitest::SEED to Minitest.seed

+ Calling `srand Minitest.seed` before all shuffles to ensure determinism.

This defends against other tests/libraries calling srand themselves
and messing up minitest's seeded ordering.

[git-p4: depot-paths = "//src/minitest/dev/": change = 13428]
This commit is contained in:
Ryan Davis 2022-06-06 00:12:08 -08:00
parent 4b16c03764
commit 2d64718cae
5 changed files with 14 additions and 18 deletions

View File

@ -11,13 +11,6 @@ require "etc"
module Minitest
VERSION = "5.15.0" # :nodoc:
##
# The random seed used for this run.
#
# Set via Minitest.run after processing args.
SEED = nil
@@installed_at_exit ||= false
@@after_run = []
@extensions = []
@ -27,9 +20,14 @@ module Minitest
end
##
# :call-seq:
# parallel_executor # => Parallel::Executor
# The random seed used for this run. This is used to srand at the
# start of the run and between each +Runnable.run+.
#
# Set via Minitest.run after processing args.
cattr_accessor :seed
##
# Parallel test executor
cattr_accessor :parallel_executor
@ -144,10 +142,8 @@ module Minitest
options = process_args args
unless defined?(Minitest::SEED) && Minitest::SEED then
remove_const :SEED
const_set :SEED, options[:seed]
end
Minitest.seed = options[:seed]
srand Minitest.seed
reporter = CompositeReporter.new
reporter << SummaryReporter.new(options[:io], options)
@ -258,8 +254,6 @@ module Minitest
orig_args << "--seed" << options[:seed].to_s
end
srand options[:seed]
options[:args] = orig_args.map { |s|
s =~ /[\s|&<>$()]/ ? s.inspect : s
}.join " "

View File

@ -67,6 +67,7 @@ module Minitest
case self.test_order
when :random, :parallel then
srand Minitest.seed
methods.sort.shuffle
when :alpha, :sorted then
methods.sort

View File

@ -129,7 +129,7 @@ class MetaMetaMetaTestCase < Minitest::Test
def setup
super
srand 42
Minitest.seed = 42
Minitest::Test.reset
@tu = nil
end

View File

@ -30,6 +30,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
end
def setup
super
self.io = StringIO.new("")
self.r = new_composite_reporter
end

View File

@ -331,7 +331,7 @@ class TestMinitestRunner < MetaMetaMetaTestCase
end
def test_seed # this is set for THIS run, so I'm not testing it's actual value
assert_instance_of Integer, Minitest::SEED
assert_instance_of Integer, Minitest.seed
end
def test_run_failing_filtered
@ -918,7 +918,7 @@ class TestMinitestUnitTestCase < Minitest::Test
random_tests_3 = sample_test_case 1_000
assert_equal random_tests_1, random_tests_2
refute_equal random_tests_1, random_tests_3
assert_equal random_tests_1, random_tests_3
end
def test_runnable_methods_sorted