minitest 5.16.3 → 5.25.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,10 +13,6 @@ class TestMinitestReporter < MetaMetaMetaTestCase
13
13
  attr_accessor :r, :io
14
14
 
15
15
  def new_composite_reporter
16
- # Ruby bug in older versions of 2.2 & 2.3 on all platforms
17
- # Latest Windows builds were 2.2.6 and 2.3.3. Latest Ruby releases were
18
- # 2.2.10 and 2.3.8.
19
- skip if windows? && RUBY_VERSION < '2.4'
20
16
  reporter = Minitest::CompositeReporter.new
21
17
  reporter << Minitest::SummaryReporter.new(self.io)
22
18
  reporter << Minitest::ProgressReporter.new(self.io)
@@ -31,13 +27,13 @@ class TestMinitestReporter < MetaMetaMetaTestCase
31
27
 
32
28
  def setup
33
29
  super
34
- self.io = StringIO.new("")
30
+ self.io = StringIO.new(+"")
35
31
  self.r = new_composite_reporter
36
32
  end
37
33
 
38
34
  def error_test
39
35
  unless defined? @et then
40
- @et = Minitest::Test.new(:woot)
36
+ @et = Minitest::Test.new :woot
41
37
  @et.failures << Minitest::UnexpectedError.new(begin
42
38
  raise "no"
43
39
  rescue => e
@@ -48,9 +44,28 @@ class TestMinitestReporter < MetaMetaMetaTestCase
48
44
  @et
49
45
  end
50
46
 
47
+ def system_stack_error_test
48
+ unless defined? @sse then
49
+
50
+ ex = SystemStackError.new
51
+
52
+ pre = ("a".."c").to_a
53
+ mid = ("aa".."ad").to_a * 67
54
+ post = ("d".."f").to_a
55
+ ary = pre + mid + post
56
+
57
+ ex.set_backtrace ary
58
+
59
+ @sse = Minitest::Test.new :woot
60
+ @sse.failures << Minitest::UnexpectedError.new(ex)
61
+ @sse = Minitest::Result.from @sse
62
+ end
63
+ @sse
64
+ end
65
+
51
66
  def fail_test
52
67
  unless defined? @ft then
53
- @ft = Minitest::Test.new(:woot)
68
+ @ft = Minitest::Test.new :woot
54
69
  @ft.failures << begin
55
70
  raise Minitest::Assertion, "boo"
56
71
  rescue Minitest::Assertion => e
@@ -65,9 +80,15 @@ class TestMinitestReporter < MetaMetaMetaTestCase
65
80
  @pt ||= Minitest::Result.from Minitest::Test.new(:woot)
66
81
  end
67
82
 
83
+ def passing_test_with_metadata
84
+ test = Minitest::Test.new :woot
85
+ test.metadata[:meta] = :data
86
+ @pt ||= Minitest::Result.from test
87
+ end
88
+
68
89
  def skip_test
69
90
  unless defined? @st then
70
- @st = Minitest::Test.new(:woot)
91
+ @st = Minitest::Test.new :woot
71
92
  @st.failures << begin
72
93
  raise Minitest::Skip
73
94
  rescue Minitest::Assertion => e
@@ -166,6 +187,29 @@ class TestMinitestReporter < MetaMetaMetaTestCase
166
187
  assert_equal 0, r.assertions
167
188
  end
168
189
 
190
+ def test_record_pass_with_metadata
191
+ reporter = self.r
192
+
193
+ def reporter.metadata
194
+ @metadata
195
+ end
196
+
197
+ def reporter.record result
198
+ super
199
+ @metadata = result.metadata if result.metadata?
200
+ end
201
+
202
+ r.record passing_test_with_metadata
203
+
204
+ exp = { :meta => :data }
205
+ assert_equal exp, reporter.metadata
206
+
207
+ assert_equal ".", io.string
208
+ assert_empty r.results
209
+ assert_equal 1, r.count
210
+ assert_equal 0, r.assertions
211
+ end
212
+
169
213
  def test_record_fail
170
214
  fail_test = self.fail_test
171
215
  r.record fail_test
@@ -200,7 +244,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
200
244
  r.start
201
245
  r.report
202
246
 
203
- exp = clean <<-EOM
247
+ exp = <<~EOM
204
248
  Run options:
205
249
 
206
250
  # Running:
@@ -220,7 +264,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
220
264
  r.record passing_test
221
265
  r.report
222
266
 
223
- exp = clean <<-EOM
267
+ exp = <<~EOM
224
268
  Run options:
225
269
 
226
270
  # Running:
@@ -240,7 +284,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
240
284
  r.record fail_test
241
285
  r.report
242
286
 
243
- exp = clean <<-EOM
287
+ exp = <<~EOM
244
288
  Run options:
245
289
 
246
290
  # Running:
@@ -264,7 +308,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
264
308
  r.record error_test
265
309
  r.report
266
310
 
267
- exp = clean <<-EOM
311
+ exp = <<~EOM
268
312
  Run options:
269
313
 
270
314
  # Running:
@@ -276,8 +320,44 @@ class TestMinitestReporter < MetaMetaMetaTestCase
276
320
  1) Error:
277
321
  Minitest::Test#woot:
278
322
  RuntimeError: no
279
- FILE:LINE:in `error_test'
280
- FILE:LINE:in `test_report_error'
323
+ FILE:LINE:in 'error_test'
324
+ FILE:LINE:in 'test_report_error'
325
+
326
+ 1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
327
+ EOM
328
+
329
+ assert_equal exp, normalize_output(io.string)
330
+ end
331
+
332
+ def test_report_error__sse
333
+ r.start
334
+ r.record system_stack_error_test
335
+ r.report
336
+
337
+ exp = <<~EOM
338
+ Run options:
339
+
340
+ # Running:
341
+
342
+ E
343
+
344
+ Finished in 0.00
345
+
346
+ 1) Error:
347
+ Minitest::Test#woot:
348
+ SystemStackError: 274 -> 12
349
+ a
350
+ b
351
+ c
352
+ +->> 67 cycles of 4 lines:
353
+ | aa
354
+ | ab
355
+ | ac
356
+ | ad
357
+ +-<<
358
+ d
359
+ e
360
+ f
281
361
 
282
362
  1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
283
363
  EOM
@@ -293,7 +373,7 @@ class TestMinitestReporter < MetaMetaMetaTestCase
293
373
  r.report
294
374
  end
295
375
 
296
- exp = clean <<-EOM
376
+ exp = <<~EOM
297
377
  Run options:
298
378
 
299
379
  # Running:
@@ -309,4 +389,48 @@ class TestMinitestReporter < MetaMetaMetaTestCase
309
389
 
310
390
  assert_equal exp, normalize_output(io.string)
311
391
  end
392
+
393
+ def test_report_failure_uses_backtrace_filter
394
+ filter = Minitest::BacktraceFilter.new
395
+ def filter.filter _bt
396
+ ["foo.rb:123:in 'foo'"]
397
+ end
398
+
399
+ with_backtrace_filter filter do
400
+ r.start
401
+ r.record fail_test
402
+ r.report
403
+ end
404
+
405
+ exp = "Minitest::Test#woot [foo.rb:123]"
406
+
407
+ assert_includes io.string, exp
408
+ end
409
+
410
+ def test_report_failure_uses_backtrace_filter_complex_sorbet
411
+ backtrace = <<~EOBT
412
+ /Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/assertions.rb:183:in 'assert'
413
+ example_test.rb:9:in 'assert_false'
414
+ /Users/user/.gem/ruby/3.2.2/gems/sorbet-runtime-0.5.11068/lib/types/private/methods/call_validation.rb:256:in 'bind_call'
415
+ /Users/user/.gem/ruby/3.2.2/gems/sorbet-runtime-0.5.11068/lib/types/private/methods/call_validation.rb:256:in 'validate_call'
416
+ /Users/user/.gem/ruby/3.2.2/gems/sorbet-runtime-0.5.11068/lib/types/private/methods/_methods.rb:275:in 'block in _on_method_added'
417
+ example_test.rb:25:in 'test_something'
418
+ /Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/test.rb:94:in 'block (3 levels) in run'
419
+ /Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/test.rb:191:in 'capture_exceptions'
420
+ /Users/user/.gem/ruby/3.2.2/gems/minitest-5.20.0/lib/minitest/test.rb:89:in 'block (2 levels) in run'
421
+ ... so many lines ...
422
+ EOBT
423
+
424
+ filter = Minitest::BacktraceFilter.new %r%lib/minitest|gems/sorbet%
425
+
426
+ with_backtrace_filter filter do
427
+ begin
428
+ assert_equal 1, 2
429
+ rescue Minitest::Assertion => e
430
+ e.set_backtrace backtrace.lines.map(&:chomp)
431
+
432
+ assert_match "example_test.rb:25", e.location
433
+ end
434
+ end
435
+ end
312
436
  end
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  require "minitest/metametameta"
3
2
  require "stringio"
4
3
 
@@ -12,15 +11,12 @@ class ExampleA; end
12
11
  class ExampleB < ExampleA; end
13
12
 
14
13
  describe Minitest::Spec do
15
- # helps to deal with 2.4 deprecation of Fixnum for Integer
16
- Int = 1.class
17
-
18
14
  # do not parallelize this suite... it just can"t handle it.
19
15
 
20
16
  def assert_triggered expected = "blah", klass = Minitest::Assertion
21
17
  @assertion_count += 1
22
18
 
23
- e = assert_raises(klass) do
19
+ e = assert_raises klass do
24
20
  yield
25
21
  end
26
22
 
@@ -29,17 +25,17 @@ describe Minitest::Spec do
29
25
  msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform
30
26
  msg.gsub!(/:0x[Xa-fA-F0-9]{4,}[ @].+?>/, ":0xXXXXXX@PATH>")
31
27
 
32
- if expected
28
+ return unless expected
29
+
30
+ @assertion_count += 1
31
+ case expected
32
+ when String then
33
+ assert_equal expected, msg
34
+ when Regexp then
33
35
  @assertion_count += 1
34
- case expected
35
- when String then
36
- assert_equal expected, msg
37
- when Regexp then
38
- @assertion_count += 1
39
- assert_match expected, msg
40
- else
41
- flunk "Unknown: #{expected.inspect}"
42
- end
36
+ assert_match expected, msg
37
+ else
38
+ flunk "Unknown: #{expected.inspect}"
43
39
  end
44
40
  end
45
41
 
@@ -137,6 +133,46 @@ describe Minitest::Spec do
137
133
  end
138
134
  end
139
135
 
136
+ def good_pattern
137
+ capture_io do # 3.0 is noisy
138
+ eval "[1,2,3] => [Integer, Integer, Integer]" # eval to escape parser for ruby<3
139
+ end
140
+ end
141
+
142
+ def bad_pattern
143
+ capture_io do # 3.0 is noisy
144
+ eval "[1,2,3] => [Integer, Integer]" # eval to escape parser for ruby<3
145
+ end
146
+ end
147
+
148
+ it "needs to pattern match" do
149
+ @assertion_count = 1
150
+
151
+ if RUBY_VERSION > "3" then
152
+ expect { good_pattern }.must_pattern_match
153
+ else
154
+ assert_raises NotImplementedError do
155
+ expect {}.must_pattern_match
156
+ end
157
+ end
158
+ end
159
+
160
+ it "needs to error on bad pattern match" do
161
+ skip unless RUBY_VERSION > "3"
162
+
163
+ @assertion_count = 1
164
+
165
+ exp = if RUBY_VERSION.start_with? "3.0"
166
+ "[1, 2, 3]" # terrible error message!
167
+ else
168
+ /length mismatch/
169
+ end
170
+
171
+ assert_triggered exp do
172
+ expect { bad_pattern }.must_pattern_match
173
+ end
174
+ end
175
+
140
176
  it "needs to ensure silence" do
141
177
  @assertion_count -= 1 # no msg
142
178
  @assertion_count += 2 # assert_output is 2 assertions
@@ -156,7 +192,7 @@ describe Minitest::Spec do
156
192
  methods = Minitest::Expectations.public_instance_methods.grep(/must|wont/)
157
193
  methods.map!(&:to_s) if Symbol === methods.first
158
194
 
159
- musts, wonts = methods.sort.partition { |m| m =~ /must/ }
195
+ musts, wonts = methods.sort.partition { |m| m.include? "must" }
160
196
 
161
197
  expected_musts = %w[must_be
162
198
  must_be_close_to
@@ -172,6 +208,7 @@ describe Minitest::Spec do
172
208
  must_include
173
209
  must_match
174
210
  must_output
211
+ must_pattern_match
175
212
  must_raise
176
213
  must_respond_to
177
214
  must_throw
@@ -179,7 +216,7 @@ describe Minitest::Spec do
179
216
 
180
217
  bad = %w[not raise throw send output be_silent]
181
218
 
182
- expected_wonts = expected_musts.map { |m| m.sub(/must/, "wont") }.sort
219
+ expected_wonts = expected_musts.map { |m| m.sub("must", "wont") }.sort
183
220
  expected_wonts.reject! { |m| m =~ /wont_#{Regexp.union(*bad)}/ }
184
221
 
185
222
  _(musts).must_equal expected_musts
@@ -243,18 +280,14 @@ describe Minitest::Spec do
243
280
  end
244
281
 
245
282
  it "needs to warn on equality with nil" do
246
- @assertion_count += 1 # extra test
283
+ @assertion_count = 3
284
+ @assertion_count += 2 unless error_on_warn? # 2 extra assertions
285
+
286
+ exp = /DEPRECATED: Use assert_nil if expecting nil from .* This will fail in Minitest 6./
247
287
 
248
- out, err = capture_io do
288
+ assert_deprecation exp do
249
289
  assert_success _(nil).must_equal(nil)
250
290
  end
251
-
252
- exp = "DEPRECATED: Use assert_nil if expecting nil from #{__FILE__}:#{__LINE__-3}. " \
253
- "This will fail in Minitest 6.\n"
254
- exp = "" if $-w.nil?
255
-
256
- assert_empty out
257
- assert_equal exp, err
258
291
  end
259
292
 
260
293
  it "needs to verify floats outside a delta" do
@@ -362,12 +395,12 @@ describe Minitest::Spec do
362
395
  it "needs to verify instances of a class" do
363
396
  assert_success _(42).wont_be_instance_of(String)
364
397
 
365
- assert_triggered "Expected 42 to not be a kind of #{Int.name}." do
366
- _(42).wont_be_kind_of Int
398
+ assert_triggered "Expected 42 to not be a kind of Integer." do
399
+ _(42).wont_be_kind_of Integer
367
400
  end
368
401
 
369
- assert_triggered "msg.\nExpected 42 to not be an instance of #{Int.name}." do
370
- _(42).wont_be_instance_of Int, "msg"
402
+ assert_triggered "msg.\nExpected 42 to not be an instance of Integer." do
403
+ _(42).wont_be_instance_of Integer, "msg"
371
404
  end
372
405
  end
373
406
 
@@ -377,26 +410,26 @@ describe Minitest::Spec do
377
410
  assert_success _(42).wont_be_kind_of(String)
378
411
  assert_success _(proc {}).wont_be_kind_of(String)
379
412
 
380
- assert_triggered "Expected 42 to not be a kind of #{Int.name}." do
381
- _(42).wont_be_kind_of Int
413
+ assert_triggered "Expected 42 to not be a kind of Integer." do
414
+ _(42).wont_be_kind_of Integer
382
415
  end
383
416
 
384
- assert_triggered "msg.\nExpected 42 to not be a kind of #{Int.name}." do
385
- _(42).wont_be_kind_of Int, "msg"
417
+ assert_triggered "msg.\nExpected 42 to not be a kind of Integer." do
418
+ _(42).wont_be_kind_of Integer, "msg"
386
419
  end
387
420
  end
388
421
 
389
422
  it "needs to verify kinds of objects" do
390
423
  @assertion_count += 3 # extra test
391
424
 
392
- assert_success _(6 * 7).must_be_kind_of(Int)
425
+ assert_success _(6 * 7).must_be_kind_of(Integer)
393
426
  assert_success _(6 * 7).must_be_kind_of(Numeric)
394
427
 
395
- assert_triggered "Expected 42 to be a kind of String, not #{Int.name}." do
428
+ assert_triggered "Expected 42 to be a kind of String, not Integer." do
396
429
  _(6 * 7).must_be_kind_of String
397
430
  end
398
431
 
399
- assert_triggered "msg.\nExpected 42 to be a kind of String, not #{Int.name}." do
432
+ assert_triggered "msg.\nExpected 42 to be a kind of String, not Integer." do
400
433
  _(6 * 7).must_be_kind_of String, "msg"
401
434
  end
402
435
 
@@ -535,7 +568,8 @@ describe Minitest::Spec do
535
568
 
536
569
  it "can NOT use must_equal in a thread. It must use expect in a thread" do
537
570
  skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
538
- assert_raises RuntimeError do
571
+
572
+ assert_raises RuntimeError, Minitest::UnexpectedWarning do
539
573
  capture_io do
540
574
  Thread.new { (1 + 1).must_equal 2 }.join
541
575
  end
@@ -545,29 +579,33 @@ describe Minitest::Spec do
545
579
  it "fails gracefully when expectation used outside of `it`" do
546
580
  skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
547
581
 
548
- @assertion_count += 1
582
+ @assertion_count += 2 # assert_match is compound
549
583
 
550
- e = assert_raises RuntimeError do
584
+ e = assert_raises RuntimeError, Minitest::UnexpectedWarning do
551
585
  capture_io do
552
586
  Thread.new { # forces ctx to be nil
553
- describe("woot") do
587
+ describe "woot" do
554
588
  (1 + 1).must_equal 2
555
589
  end
556
590
  }.join
557
591
  end
558
592
  end
559
593
 
560
- assert_equal "Calling #must_equal outside of test.", e.message
594
+ exp = "Calling #must_equal outside of test."
595
+ exp = "DEPRECATED: global use of must_equal from" if error_on_warn?
596
+
597
+ assert_match exp, e.message
561
598
  end
562
599
 
563
600
  it "deprecates expectation used without _" do
564
601
  skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
565
602
 
566
- @assertion_count += 3
603
+ @assertion_count += 1
604
+ @assertion_count += 2 unless error_on_warn?
567
605
 
568
606
  exp = /DEPRECATED: global use of must_equal from/
569
607
 
570
- assert_output "", exp do
608
+ assert_deprecation exp do
571
609
  (1 + 1).must_equal 2
572
610
  end
573
611
  end
@@ -577,12 +615,13 @@ describe Minitest::Spec do
577
615
  it "deprecates expectation used without _ with empty backtrace_filter" do
578
616
  skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
579
617
 
580
- @assertion_count += 3
618
+ @assertion_count += 1
619
+ @assertion_count += 2 unless error_on_warn?
581
620
 
582
621
  exp = /DEPRECATED: global use of must_equal from/
583
622
 
584
623
  with_empty_backtrace_filter do
585
- assert_output "", exp do
624
+ assert_deprecation exp do
586
625
  (1 + 1).must_equal 2
587
626
  end
588
627
  end
@@ -613,9 +652,9 @@ describe Minitest::Spec do
613
652
  end
614
653
 
615
654
  it "needs to verify types of objects" do
616
- assert_success _(6 * 7).must_be_instance_of(Int)
655
+ assert_success _(6 * 7).must_be_instance_of(Integer)
617
656
 
618
- exp = "Expected 42 to be an instance of String, not #{Int.name}."
657
+ exp = "Expected 42 to be an instance of String, not Integer."
619
658
 
620
659
  assert_triggered exp do
621
660
  _(6 * 7).must_be_instance_of String
@@ -642,7 +681,7 @@ describe Minitest::Spec do
642
681
  assert_success _(41).must_be(:<, 42)
643
682
 
644
683
  assert_triggered "Expected 42 to be < 41." do
645
- _(42).must_be(:<, 41)
684
+ _(42).must_be :<, 41
646
685
  end
647
686
  end
648
687
 
@@ -659,11 +698,11 @@ describe Minitest::Spec do
659
698
  it "needs to verify using respond_to" do
660
699
  assert_success _(42).must_respond_to(:+)
661
700
 
662
- assert_triggered "Expected 42 (#{Int.name}) to respond to #clear." do
701
+ assert_triggered "Expected 42 (Integer) to respond to #clear." do
663
702
  _(42).must_respond_to :clear
664
703
  end
665
704
 
666
- assert_triggered "msg.\nExpected 42 (#{Int.name}) to respond to #clear." do
705
+ assert_triggered "msg.\nExpected 42 (Integer) to respond to #clear." do
667
706
  _(42).must_respond_to :clear, "msg"
668
707
  end
669
708
  end
@@ -710,9 +749,9 @@ describe Minitest::Spec, :let do
710
749
  it "doesn't raise an error if it is just another let" do
711
750
  v = proc do
712
751
  describe :outer do
713
- let(:bar)
752
+ let :bar
714
753
  describe :inner do
715
- let(:bar)
754
+ let :bar
716
755
  end
717
756
  end
718
757
  :good
@@ -975,8 +1014,9 @@ class TestMeta < MetaMetaMetaTestCase
975
1014
  z = describe "second thingy" do end
976
1015
  end
977
1016
 
978
- test_methods = ["test_0001_top level it",
979
- "test_0002_не латинские &いった α, β, γ, δ, ε hello!!! world",
1017
+ test_methods = [
1018
+ "test_0001_top level it",
1019
+ "test_0002_не латинские &いった α, β, γ, δ, ε hello!!! world",
980
1020
  ].sort
981
1021
 
982
1022
  assert_equal test_methods, [x1, x2]
@@ -1068,3 +1108,38 @@ class ValueMonadTest < Minitest::Test
1068
1108
  assert_equal "c", struct.expect
1069
1109
  end
1070
1110
  end
1111
+
1112
+ describe Minitest::Spec, :infect_an_assertion do
1113
+ class << self
1114
+ attr_accessor :infect_mock
1115
+ end
1116
+
1117
+ def assert_infects exp, act, msg = nil, foo: nil, bar: nil
1118
+ self.class.infect_mock.assert_infects exp, act, msg, foo: foo, bar: bar
1119
+ end
1120
+
1121
+ infect_an_assertion :assert_infects, :must_infect
1122
+ infect_an_assertion :assert_infects, :must_infect_without_flipping, :dont_flip
1123
+
1124
+ it "infects assertions with kwargs" do
1125
+ mock = Minitest::Mock.new
1126
+ mock.expect :assert_infects, true, [:exp, :act, nil], foo: :foo, bar: :bar
1127
+
1128
+ self.class.infect_mock = mock
1129
+
1130
+ _(:act).must_infect :exp, foo: :foo, bar: :bar
1131
+
1132
+ assert_mock mock
1133
+ end
1134
+
1135
+ it "infects assertions with kwargs (dont_flip)" do
1136
+ mock = Minitest::Mock.new
1137
+ mock.expect :assert_infects, true, [:act, :exp, nil], foo: :foo, bar: :bar
1138
+
1139
+ self.class.infect_mock = mock
1140
+
1141
+ _(:act).must_infect_without_flipping :exp, foo: :foo, bar: :bar
1142
+
1143
+ assert_mock mock
1144
+ end
1145
+ end