minitest 5.15.0 → 5.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/minitest/unit.rb CHANGED
@@ -1,5 +1,3 @@
1
- # :stopdoc:
2
-
3
1
  unless defined?(Minitest) then
4
2
  # all of this crap is just to avoid circular requires and is only
5
3
  # needed if a user requires "minitest/unit" directly instead of
@@ -10,17 +8,18 @@ unless defined?(Minitest) then
10
8
  warn %(Warning: or add 'gem "minitest"' before 'require "minitest/autorun"')
11
9
  warn "From:\n #{from}"
12
10
 
13
- module Minitest; end
14
- MiniTest = Minitest # prevents minitest.rb from requiring back to us
11
+ module Minitest # :nodoc:
12
+ end
13
+ MiniTest = Minitest # :nodoc: # prevents minitest.rb from requiring back to us
15
14
  require "minitest"
16
15
  end
17
16
 
18
17
  MiniTest = Minitest unless defined?(MiniTest)
19
18
 
20
19
  module Minitest
21
- class Unit
20
+ class Unit # :nodoc:
22
21
  VERSION = Minitest::VERSION
23
- class TestCase < Minitest::Test
22
+ class TestCase < Minitest::Test # :nodoc:
24
23
  def self.inherited klass # :nodoc:
25
24
  from = caller.first
26
25
  warn "MiniTest::Unit::TestCase is now Minitest::Test. From #{from}"
@@ -41,5 +40,3 @@ module Minitest
41
40
  end
42
41
  end
43
42
  end
44
-
45
- # :startdoc:
data/lib/minitest.rb CHANGED
@@ -9,47 +9,60 @@ require "etc"
9
9
  # :include: README.rdoc
10
10
 
11
11
  module Minitest
12
- VERSION = "5.15.0" # :nodoc:
13
- ENCS = "".respond_to? :encoding # :nodoc:
12
+ VERSION = "5.20.0" # :nodoc:
14
13
 
15
14
  @@installed_at_exit ||= false
16
15
  @@after_run = []
17
16
  @extensions = []
18
17
 
19
- mc = (class << self; self; end)
18
+ def self.cattr_accessor name # :nodoc:
19
+ (class << self; self; end).attr_accessor name
20
+ end
21
+
22
+ ##
23
+ # The random seed used for this run. This is used to srand at the
24
+ # start of the run and between each +Runnable.run+.
25
+ #
26
+ # Set via Minitest.run after processing args.
27
+
28
+ cattr_accessor :seed
20
29
 
21
30
  ##
22
31
  # Parallel test executor
23
32
 
24
- mc.send :attr_accessor, :parallel_executor
33
+ cattr_accessor :parallel_executor
25
34
 
26
- warn "DEPRECATED: use MT_CPU instead of N for parallel test runs" if ENV["N"]
35
+ warn "DEPRECATED: use MT_CPU instead of N for parallel test runs" if ENV["N"] && ENV["N"].to_i > 0
27
36
  n_threads = (ENV["MT_CPU"] || ENV["N"] || Etc.nprocessors).to_i
37
+
28
38
  self.parallel_executor = Parallel::Executor.new n_threads
29
39
 
30
40
  ##
31
41
  # Filter object for backtraces.
32
42
 
33
- mc.send :attr_accessor, :backtrace_filter
43
+ cattr_accessor :backtrace_filter
34
44
 
35
45
  ##
36
46
  # Reporter object to be used for all runs.
37
47
  #
38
48
  # NOTE: This accessor is only available during setup, not during runs.
39
49
 
40
- mc.send :attr_accessor, :reporter
50
+ cattr_accessor :reporter
41
51
 
42
52
  ##
43
53
  # Names of known extension plugins.
44
54
 
45
- mc.send :attr_accessor, :extensions
55
+ cattr_accessor :extensions
46
56
 
47
57
  ##
48
58
  # The signal to use for dumping information to STDERR. Defaults to "INFO".
49
59
 
50
- mc.send :attr_accessor, :info_signal
60
+ cattr_accessor :info_signal
51
61
  self.info_signal = "INFO"
52
62
 
63
+ cattr_accessor :allow_fork
64
+ self.allow_fork = false
65
+
53
66
  ##
54
67
  # Registers Minitest to run at process exit
55
68
 
@@ -65,7 +78,7 @@ module Minitest
65
78
 
66
79
  pid = Process.pid
67
80
  at_exit {
68
- next if Process.pid != pid
81
+ next if !Minitest.allow_fork && Process.pid != pid
69
82
  @@after_run.reverse_each(&:call)
70
83
  exit exit_code || false
71
84
  }
@@ -132,6 +145,9 @@ module Minitest
132
145
 
133
146
  options = process_args args
134
147
 
148
+ Minitest.seed = options[:seed]
149
+ srand Minitest.seed
150
+
135
151
  reporter = CompositeReporter.new
136
152
  reporter << SummaryReporter.new(options[:io], options)
137
153
  reporter << ProgressReporter.new(options[:io], options)
@@ -158,7 +174,7 @@ module Minitest
158
174
  # sub-classes to run.
159
175
 
160
176
  def self.__run reporter, options
161
- suites = Runnable.runnables.reject { |s| s.runnable_methods.empty? }.shuffle
177
+ suites = Runnable.runnables.shuffle
162
178
  parallel, serial = suites.partition { |s| s.test_order == :parallel }
163
179
 
164
180
  # If we run the parallel tests before the serial tests, the parallel tests
@@ -196,6 +212,10 @@ module Minitest
196
212
  options[:verbose] = true
197
213
  end
198
214
 
215
+ opts.on "--show-skips", "Show skipped at the end of run." do
216
+ options[:show_skips] = true
217
+ end
218
+
199
219
  opts.on "-n", "--name PATTERN", "Filter run on /regexp/ or string." do |a|
200
220
  options[:filter] = a
201
221
  end
@@ -237,8 +257,6 @@ module Minitest
237
257
  orig_args << "--seed" << options[:seed].to_s
238
258
  end
239
259
 
240
- srand options[:seed]
241
-
242
260
  options[:args] = orig_args.map { |s|
243
261
  s =~ /[\s|&<>$()]/ ? s.inspect : s
244
262
  }.join " "
@@ -316,19 +334,25 @@ module Minitest
316
334
  # reporter to record.
317
335
 
318
336
  def self.run reporter, options = {}
319
- filter = options[:filter] || "/./"
320
- filter = Regexp.new $1 if filter.is_a?(String) && filter =~ %r%/(.*)/%
321
-
322
- filtered_methods = self.runnable_methods.find_all { |m|
323
- filter === m || filter === "#{self}##{m}"
324
- }
337
+ filtered_methods = if options[:filter]
338
+ filter = options[:filter]
339
+ filter = Regexp.new $1 if filter.is_a?(String) && filter =~ %r%/(.*)/%
340
+
341
+ self.runnable_methods.find_all { |m|
342
+ filter === m || filter === "#{self}##{m}"
343
+ }
344
+ else
345
+ self.runnable_methods
346
+ end
325
347
 
326
- exclude = options[:exclude]
327
- exclude = Regexp.new $1 if exclude =~ %r%/(.*)/%
348
+ if options[:exclude]
349
+ exclude = options[:exclude]
350
+ exclude = Regexp.new $1 if exclude =~ %r%/(.*)/%
328
351
 
329
- filtered_methods.delete_if { |m|
330
- exclude === m || exclude === "#{self}##{m}"
331
- }
352
+ filtered_methods.delete_if { |m|
353
+ exclude === m || exclude === "#{self}##{m}"
354
+ }
355
+ end
332
356
 
333
357
  return if filtered_methods.empty?
334
358
 
@@ -350,6 +374,14 @@ module Minitest
350
374
  reporter.record Minitest.run_one_method(klass, method_name)
351
375
  end
352
376
 
377
+ ##
378
+ # Defines the order to run tests (:random by default). Override
379
+ # this or use a convenience method to change it for your tests.
380
+
381
+ def self.test_order
382
+ :random
383
+ end
384
+
353
385
  def self.with_info_handler reporter, &block # :nodoc:
354
386
  handler = lambda do
355
387
  unless reporter.passed? then
@@ -417,6 +449,31 @@ module Minitest
417
449
  self.name = name
418
450
  self.failures = []
419
451
  self.assertions = 0
452
+ # lazy initializer for metadata
453
+ end
454
+
455
+ ##
456
+ # Metadata you attach to the test results that get sent to the reporter.
457
+ #
458
+ # Lazily initializes to a hash, to keep memory down.
459
+ #
460
+ # NOTE: this data *must* be plain (read: marshal-able) data!
461
+ # Hashes! Arrays! Strings!
462
+
463
+ def metadata
464
+ @metadata ||= {}
465
+ end
466
+
467
+ ##
468
+ # Sets metadata, mainly used for +Result.from+.
469
+
470
+ attr_writer :metadata
471
+
472
+ ##
473
+ # Returns true if metadata exists.
474
+
475
+ def metadata?
476
+ defined? @metadata
420
477
  end
421
478
 
422
479
  ##
@@ -537,6 +594,7 @@ module Minitest
537
594
  r.assertions = o.assertions
538
595
  r.failures = o.failures.dup
539
596
  r.time = o.time
597
+ r.metadata = o.metadata if o.metadata?
540
598
 
541
599
  r.source_location = o.method(o.name).source_location rescue ["unknown", -1]
542
600
 
@@ -793,7 +851,8 @@ module Minitest
793
851
 
794
852
  def aggregated_results io # :nodoc:
795
853
  filtered_results = results.dup
796
- filtered_results.reject!(&:skipped?) unless options[:verbose]
854
+ filtered_results.reject!(&:skipped?) unless
855
+ options[:verbose] or options[:show_skips]
797
856
 
798
857
  skip = options[:skip] || []
799
858
 
@@ -807,26 +866,19 @@ module Minitest
807
866
  end
808
867
 
809
868
  def to_s # :nodoc:
810
- aggregated_results(StringIO.new(binary_string)).string
869
+ aggregated_results(StringIO.new(''.b)).string
811
870
  end
812
871
 
813
872
  def summary # :nodoc:
814
873
  extra = ""
815
874
 
816
875
  extra = "\n\nYou have skipped tests. Run with --verbose for details." if
817
- results.any?(&:skipped?) unless options[:verbose] or ENV["MT_NO_SKIP_MSG"]
876
+ results.any?(&:skipped?) unless
877
+ options[:verbose] or options[:show_skips] or ENV["MT_NO_SKIP_MSG"]
818
878
 
819
879
  "%d runs, %d assertions, %d failures, %d errors, %d skips%s" %
820
880
  [count, assertions, failures, errors, skips, extra]
821
881
  end
822
-
823
- private
824
-
825
- if '<3'.respond_to? :b
826
- def binary_string; ''.b; end
827
- else
828
- def binary_string; ''.force_encoding(Encoding::ASCII_8BIT); end
829
- end
830
882
  end
831
883
 
832
884
  ##
@@ -129,7 +129,7 @@ class MetaMetaMetaTestCase < Minitest::Test
129
129
 
130
130
  def setup
131
131
  super
132
- srand 42
132
+ Minitest.seed = 42
133
133
  Minitest::Test.reset
134
134
  @tu = nil
135
135
  end
@@ -354,7 +354,7 @@ class TestMinitestAssertions < Minitest::Test
354
354
  @@ -1,3 +1,3 @@
355
355
  -# encoding: UTF-8
356
356
  -# valid: false
357
- +# encoding: ASCII-8BIT
357
+ +# encoding: #{Encoding::BINARY.name}
358
358
  +# valid: true
359
359
  "bad-utf8-\\xF1.txt"
360
360
  EOM
@@ -373,7 +373,7 @@ class TestMinitestAssertions < Minitest::Test
373
373
  @@ -1,3 +1,3 @@
374
374
  -# encoding: US-ASCII
375
375
  -# valid: false
376
- +# encoding: ASCII-8BIT
376
+ +# encoding: #{Encoding::BINARY.name}
377
377
  +# valid: true
378
378
  "bad-utf8-\\xF1.txt"
379
379
  EOM
@@ -484,7 +484,10 @@ class TestMinitestAssertions < Minitest::Test
484
484
 
485
485
  def test_assert_match
486
486
  @assertion_count = 2
487
- @tc.assert_match(/\w+/, "blah blah blah")
487
+ m = @tc.assert_match(/\w+/, "blah blah blah")
488
+
489
+ assert_kind_of MatchData, m
490
+ assert_equal "blah", m[0]
488
491
  end
489
492
 
490
493
  def test_assert_match_matchee_to_str
@@ -804,7 +807,7 @@ class TestMinitestAssertions < Minitest::Test
804
807
  # *sigh* This is quite an odd scenario, but it is from real (albeit
805
808
  # ugly) test code in ruby-core:
806
809
 
807
- # http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259
810
+ # https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=29259
808
811
 
809
812
  def test_assert_raises_skip
810
813
  @assertion_count = 0
@@ -1059,6 +1062,66 @@ class TestMinitestAssertions < Minitest::Test
1059
1062
  end
1060
1063
  end
1061
1064
 
1065
+ def test_assert_pattern
1066
+ if RUBY_VERSION > "3" then
1067
+ @tc.assert_pattern do
1068
+ exp = if RUBY_VERSION.start_with? "3.0"
1069
+ "(eval):1: warning: One-line pattern matching is experimental, and the behavior may change in future versions of Ruby!\n"
1070
+ else
1071
+ ""
1072
+ end
1073
+ assert_output nil, exp do
1074
+ eval "[1,2,3] => [Integer, Integer, Integer]" # eval to escape parser for ruby<3
1075
+ end
1076
+ end
1077
+ else
1078
+ @assertion_count = 0
1079
+
1080
+ assert_raises NotImplementedError do
1081
+ @tc.assert_pattern do
1082
+ # do nothing
1083
+ end
1084
+ end
1085
+ end
1086
+ end
1087
+
1088
+ def test_assert_pattern_traps_nomatchingpatternerror
1089
+ skip unless RUBY_VERSION > "3"
1090
+ exp = if RUBY_VERSION.start_with? "3.0" then
1091
+ "[1, 2, 3]" # terrible error message!
1092
+ else
1093
+ /length mismatch/
1094
+ end
1095
+
1096
+ assert_triggered exp do
1097
+ @tc.assert_pattern do
1098
+ capture_io do # 3.0 is noisy
1099
+ eval "[1,2,3] => [Integer, Integer]" # eval to escape parser for ruby<3
1100
+ end
1101
+ end
1102
+ end
1103
+ end
1104
+
1105
+ def test_assert_pattern_raises_other_exceptions
1106
+ skip unless RUBY_VERSION >= "3.0"
1107
+
1108
+ @assertion_count = 0
1109
+
1110
+ assert_raises RuntimeError do
1111
+ @tc.assert_pattern do
1112
+ raise "boom"
1113
+ end
1114
+ end
1115
+ end
1116
+
1117
+ def test_assert_pattern_with_no_block
1118
+ skip unless RUBY_VERSION >= "3.0"
1119
+
1120
+ assert_triggered "assert_pattern requires a block to capture errors." do
1121
+ @tc.assert_pattern
1122
+ end
1123
+ end
1124
+
1062
1125
  def test_capture_io
1063
1126
  @assertion_count = 0
1064
1127
 
@@ -1311,6 +1374,56 @@ class TestMinitestAssertions < Minitest::Test
1311
1374
  end
1312
1375
  end
1313
1376
 
1377
+ def test_refute_pattern
1378
+ if RUBY_VERSION >= "3.0"
1379
+ @tc.refute_pattern do
1380
+ capture_io do # 3.0 is noisy
1381
+ eval "[1,2,3] => [Integer, Integer, String]"
1382
+ end
1383
+ end
1384
+ else
1385
+ @assertion_count = 0
1386
+
1387
+ assert_raises NotImplementedError do
1388
+ @tc.refute_pattern do
1389
+ eval "[1,2,3] => [Integer, Integer, String]"
1390
+ end
1391
+ end
1392
+ end
1393
+ end
1394
+
1395
+ def test_refute_pattern_expects_nomatchingpatternerror
1396
+ skip unless RUBY_VERSION > "3"
1397
+
1398
+ assert_triggered(/NoMatchingPatternError expected, but nothing was raised./) do
1399
+ @tc.refute_pattern do
1400
+ capture_io do # 3.0 is noisy
1401
+ eval "[1,2,3] => [Integer, Integer, Integer]"
1402
+ end
1403
+ end
1404
+ end
1405
+ end
1406
+
1407
+ def test_refute_pattern_raises_other_exceptions
1408
+ skip unless RUBY_VERSION >= "3.0"
1409
+
1410
+ @assertion_count = 0
1411
+
1412
+ assert_raises RuntimeError do
1413
+ @tc.refute_pattern do
1414
+ raise "boom"
1415
+ end
1416
+ end
1417
+ end
1418
+
1419
+ def test_refute_pattern_with_no_block
1420
+ skip unless RUBY_VERSION >= "3.0"
1421
+
1422
+ assert_triggered "refute_pattern requires a block to capture errors." do
1423
+ @tc.refute_pattern
1424
+ end
1425
+ end
1426
+
1314
1427
  def test_refute_predicate
1315
1428
  @tc.refute_predicate "42", :empty?
1316
1429
  end
@@ -1528,14 +1641,14 @@ class TestMinitestAssertionHelpers < Minitest::Test
1528
1641
 
1529
1642
  def test_mu_pp_for_diff_str_encoding
1530
1643
  str = "A\nB".b
1531
- exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\nB\""
1644
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\nB\""
1532
1645
 
1533
1646
  assert_mu_pp_for_diff exp, str, :raw
1534
1647
  end
1535
1648
 
1536
1649
  def test_mu_pp_for_diff_str_encoding_both
1537
1650
  str = "A\\n\nB".b
1538
- exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\\\n\\nB\""
1651
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\\\\n\\nB\""
1539
1652
 
1540
1653
  assert_mu_pp_for_diff exp, str, :raw
1541
1654
  end
@@ -1575,7 +1688,7 @@ class TestMinitestAssertionHelpers < Minitest::Test
1575
1688
 
1576
1689
  def test_mu_pp_str_encoding
1577
1690
  str = "A\nB".b
1578
- exp = "# encoding: ASCII-8BIT\n# valid: true\n\"A\\nB\""
1691
+ exp = "# encoding: #{Encoding::BINARY.name}\n# valid: true\n\"A\\nB\""
1579
1692
 
1580
1693
  assert_mu_pp exp, str, :raw
1581
1694
  end
@@ -3,7 +3,7 @@ require "minitest/benchmark"
3
3
 
4
4
  ##
5
5
  # Used to verify data:
6
- # http://www.wolframalpha.com/examples/RegressionAnalysis.html
6
+ # https://www.wolframalpha.com/examples/RegressionAnalysis.html
7
7
 
8
8
  class TestMinitestBenchmark < Minitest::Test
9
9
  def test_cls_bench_exp
@@ -110,7 +110,7 @@ class TestMinitestBenchmark < Minitest::Test
110
110
  assert_fit :power, x, y, 0.90, 2.6217, 1.4556
111
111
 
112
112
  # income to % of households below income amount
113
- # http://library.wolfram.com/infocenter/Conferences/6461/PowerLaws.nb
113
+ # https://library.wolfram.com/infocenter/Conferences/6461/PowerLaws.nb
114
114
  x = [15_000, 25_000, 35_000, 50_000, 75_000, 100_000]
115
115
  y = [0.154, 0.283, 0.402, 0.55, 0.733, 0.843]
116
116