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

View File

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

View File

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

View File

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

View File

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