/
deprecation_test.rb
827 lines (649 loc) · 24.6 KB
/
deprecation_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
# frozen_string_literal: true
require_relative "abstract_unit"
require "logger"
require "stringio"
require "active_support/core_ext/enumerable"
require "active_support/testing/stream"
class Deprecatee
attr_accessor :fubar, :foo_bar
def zero() 0 end
def one(a) a end
def multi(a, b, c) [a, b, c] end
end
module Undeprecated
module Foo
BAR = "foo bar"
end
class Error < StandardError; end
end
class DeprecationTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Stream
def setup
@deprecator = ActiveSupport::Deprecation.new
end
test "assert_deprecated" do
assert_deprecated(/fubar/, @deprecator) do
@deprecator.warn("using fubar is deprecated")
end
assert_deprecated(@deprecator) do
@deprecator.warn("whatever")
end
end
test "assert_not_deprecated" do
assert_not_deprecated(@deprecator) do
1 + 1
end
end
test "collect_deprecations returns the return value of the block and the deprecations collected" do
result = collect_deprecations(@deprecator) do
@deprecator.warn
:result
end
assert_equal 2, result.size
assert_equal :result, result.first
assert_match "DEPRECATION WARNING:", result.last.sole
end
test "Module::deprecate" do
klass = Class.new(Deprecatee)
klass.deprecate :zero, :one, :multi, deprecator: @deprecator
assert_deprecated(/zero is deprecated/, @deprecator) do
assert_equal 0, klass.new.zero
end
assert_deprecated(/one is deprecated/, @deprecator) do
assert_equal 1, klass.new.one(1)
end
assert_deprecated(/multi is deprecated/, @deprecator) do
assert_equal [1, 2, 3], klass.new.multi(1, 2, 3)
end
end
test "Module::deprecate does not expand Hash positional argument" do
klass = Class.new(Deprecatee)
klass.deprecate :one, :one!, deprecator: @deprecator
klass.alias_method :one!, :one
hash = { k: 1 }
assert_deprecated(/one is deprecated/, @deprecator) do
assert_same hash, klass.new.one(hash)
end
assert_deprecated(/one! is deprecated/, @deprecator) do
assert_same hash, klass.new.one!(hash)
end
end
test "DeprecatedObjectProxy" do
deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, ":bomb:", @deprecator)
assert_deprecated(/:bomb:/, @deprecator) { deprecated_object.to_s }
end
test "nil behavior is ignored" do
@deprecator.behavior = nil
assert_deprecated("fubar", @deprecator) { @deprecator.warn("fubar") }
end
test "behavior callbacks" do
assert_callbacks_called_with(deprecator: @deprecator, message: /fubar/) do |callbacks|
@deprecator.behavior = callbacks
@deprecator.warn("fubar")
end
end
test "behavior callbacks with callable objects" do
assert_callbacks_called_with(deprecator: @deprecator, message: /fubar/) do |callbacks|
callbacks.select!(&:lambda?)
assert_not_empty callbacks
@deprecator.behavior = callbacks.map do |callback|
Object.new.tap { |object| object.define_singleton_method(:call, &callback) }
end
@deprecator.warn("fubar")
end
end
test ":raise behavior" do
@deprecator.behavior = :raise
message = "Revise this deprecated stuff now!"
callstack = caller_locations
e = assert_raise ActiveSupport::DeprecationException do
@deprecator.behavior.first.call(message, callstack, @deprecator)
end
assert_equal message, e.message
assert_equal callstack.map(&:to_s), e.backtrace.map(&:to_s)
end
test ":stderr behavior" do
@deprecator.behavior = :stderr
behavior = @deprecator.behavior.first
output = capture(:stderr) do
behavior.call("Some error!", ["call stack!"], @deprecator)
end
assert_match "Some error!", output
assert_no_match "call stack!", output
end
test ":stderr behavior with debug" do
@deprecator.behavior = :stderr
behavior = @deprecator.behavior.first
@deprecator.debug = true
output = capture(:stderr) do
behavior.call("Some error!", ["call stack!"], @deprecator)
end
assert_match "Some error!", output
assert_match "call stack!", output
end
test ":stderr behavior with #warn" do
@deprecator.behavior = :stderr
output = capture(:stderr) do
@deprecator.warn("Instance error!", ["instance call stack!"])
end
assert_match(/Instance error!/, output)
assert_match(/instance call stack!/, output)
end
test ":log behavior" do
@deprecator.behavior = :log
output = StringIO.new
with_rails_logger(Logger.new(output)) do
@deprecator.behavior.first.call("fubar", ["call stack!"], @deprecator)
end
assert_match "fubar", output.string
assert_no_match "call stack!", output.string
end
test ":log behavior with debug" do
@deprecator.behavior = :log
@deprecator.debug = true
output = StringIO.new
with_rails_logger(Logger.new(output)) do
@deprecator.behavior.first.call("fubar", ["call stack!"], @deprecator)
end
assert_match "fubar", output.string
assert_match "call stack!", output.string
end
test ":log behavior without Rails.logger" do
@deprecator.behavior = :log
output = capture(:stderr) do
with_rails_logger(nil) do
@deprecator.behavior.first.call("fubar", ["call stack!"], @deprecator)
end
end
assert_match "fubar", output
end
test ":silence behavior" do
@deprecator.behavior = :silence
behavior = @deprecator.behavior.first
output = capture(:stderr) do
behavior.call("Some error!", ["call stack!"], @deprecator)
end
assert_empty output
end
test ":notify behavior" do
@deprecator = ActiveSupport::Deprecation.new("horizon", "MyGem::Custom")
@deprecator.behavior = :notify
behavior = @deprecator.behavior.first
begin
events = []
ActiveSupport::Notifications.subscribe("deprecation.my_gem_custom") { |*args|
events << args.extract_options!
}
behavior.call("Some error!", ["call stack!"], @deprecator)
assert_equal 1, events.size
assert_equal "Some error!", events.first[:message]
assert_equal ["call stack!"], events.first[:callstack]
assert_equal "horizon", events.first[:deprecation_horizon]
assert_equal "MyGem::Custom", events.first[:gem_name]
ensure
ActiveSupport::Notifications.unsubscribe("deprecation.my_gem_custom")
end
end
test "invalid behavior" do
e = assert_raises(ArgumentError) do
@deprecator.behavior = :invalid
end
assert_equal ":invalid is not a valid deprecation behavior.", e.message
end
test "DeprecatedInstanceVariableProxy" do
instance = Deprecatee.new
instance.fubar = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(instance, :foo_bar, "@fubar", @deprecator)
instance.foo_bar = "foo bar!"
fubar_size = assert_deprecated("@fubar.size", @deprecator) { instance.fubar.size }
assert_equal instance.foo_bar.size, fubar_size
fubar_s = assert_deprecated("@fubar.to_s", @deprecator) { instance.fubar.to_s }
assert_equal instance.foo_bar.to_s, fubar_s
end
test "DeprecatedInstanceVariableProxy does not warn on inspect" do
instance = Deprecatee.new
instance.fubar = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(instance, :foo_bar, "@fubar", @deprecator)
instance.foo_bar = "foo bar!"
fubar_inspected = assert_not_deprecated(@deprecator) { instance.fubar.inspect }
assert_equal instance.foo_bar.inspect, fubar_inspected
end
test "DeprecatedConstantProxy" do
proxy = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("FUBAR", "Undeprecated::Foo::BAR", @deprecator)
assert_deprecated("FUBAR", @deprecator) do
assert_equal Undeprecated::Foo::BAR, proxy
end
end
test "DeprecatedConstantProxy does not warn on .class" do
proxy = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("FUBAR", "Undeprecated::Foo::BAR", @deprecator)
fubar_class = assert_not_deprecated(@deprecator) { proxy.class }
assert_equal Undeprecated::Foo::BAR.class, fubar_class
end
test "DeprecatedConstantProxy with child constant" do
proxy = ActiveSupport::Deprecation::DeprecatedConstantProxy.new("Fuu", "Undeprecated::Foo", @deprecator)
assert_deprecated("Fuu", @deprecator) do
assert_equal Undeprecated::Foo::BAR, proxy::BAR
end
assert_deprecated("Fuu", @deprecator) do
assert_raises(NameError) { proxy::DOES_NOT_EXIST }
end
end
test "deprecate_constant" do
legacy = Module.new { def self.name; "Legacy"; end }
legacy.include ActiveSupport::Deprecation::DeprecatedConstantAccessor
legacy.deprecate_constant "FUBAR", "Undeprecated::Foo::BAR", deprecator: @deprecator
assert_deprecated("Legacy::FUBAR", @deprecator) do
assert_equal Undeprecated::Foo::BAR, legacy::FUBAR
end
end
test "deprecate_constant when rescuing a deprecated error" do
legacy = Module.new { def self.name; "Legacy"; end }
legacy.include ActiveSupport::Deprecation::DeprecatedConstantAccessor
legacy.deprecate_constant "Error", "Undeprecated::Error", deprecator: @deprecator
assert_deprecated("Legacy::Error", @deprecator) do
assert_nothing_raised do
raise Undeprecated::Error
rescue legacy::Error
end
end
end
test "assert_deprecated raises when no deprecation warning" do
assert_raises(Minitest::Assertion) do
assert_deprecated(@deprecator) { 1 + 1 }
end
end
test "assert_not_deprecated raises when some deprecation warning" do
assert_raises(Minitest::Assertion) do
assert_not_deprecated(@deprecator) { @deprecator.warn }
end
end
test "assert_deprecated without match argument" do
assert_deprecated do
ActiveSupport::Deprecation.warn
end
end
test "assert_deprecated matches any warning from block" do
assert_deprecated("abc", @deprecator) do
@deprecator.warn "abc"
@deprecator.warn "def"
end
rescue Minitest::Assertion
flunk "assert_deprecated should match any warning in block, not just the last one"
end
test "assert_not_deprecated returns the result of the block" do
assert_equal 123, assert_not_deprecated(@deprecator) { 123 }
end
test "assert_deprecated returns the result of the block" do
result = assert_deprecated("abc", @deprecator) do
@deprecator.warn "abc"
123
end
assert_equal 123, result
end
test "silence" do
assert_not @deprecator.silenced
@deprecator.silence do
assert_not_deprecated(@deprecator) { @deprecator.warn }
end
assert_deprecated(@deprecator) { @deprecator.warn }
@deprecator.silenced = true
assert @deprecator.silenced
assert_not_deprecated(@deprecator) { @deprecator.warn }
end
test "silence returns the result of the block" do
assert_equal 123, @deprecator.silence { 123 }
end
test "silence ensures silencing is reverted after an error is raised" do
assert_raises do
@deprecator.silence { raise }
end
assert_deprecated(@deprecator) { @deprecator.warn }
end
test "silence only affects the current thread" do
@deprecator.silence do
assert_not_deprecated(@deprecator) { @deprecator.warn }
Thread.new do
assert_deprecated(@deprecator) { @deprecator.warn }
@deprecator.silence do
assert_not_deprecated(@deprecator) { @deprecator.warn }
end
assert_deprecated(@deprecator) { @deprecator.warn }
end.join
assert_not_deprecated(@deprecator) { @deprecator.warn }
end
end
test "Module::deprecate with method name only" do
klass = Class.new(Deprecatee)
klass.deprecate :fubar, :fubar=, deprecator: @deprecator
assert_deprecated(@deprecator) { klass.new.fubar }
assert_deprecated(@deprecator) { klass.new.fubar = :foo }
end
test "Module::deprecate with alternative method" do
klass = Class.new(Deprecatee)
klass.deprecate fubar: :foo_bar, deprecator: @deprecator
assert_deprecated(/use foo_bar instead/, @deprecator) { klass.new.fubar }
end
test "Module::deprecate with message" do
klass = Class.new(Deprecatee)
klass.deprecate fubar: "this is the old way", deprecator: @deprecator
assert_deprecated(/this is the old way/, @deprecator) { klass.new.fubar }
end
test "delegating to ActiveSupport::Deprecation" do
messages = []
klass = Class.new do
delegate :warn, :behavior=, to: ActiveSupport::Deprecation
end
o = klass.new
o.behavior = Proc.new { |message, callstack| messages << message }
assert_difference("messages.size") do
o.warn("warning")
end
end
test "overriding deprecated_method_warning" do
deprecator = deprecator_with_messages
class << deprecator
private
def deprecated_method_warning(method, message)
"deprecator.deprecated_method_warning.#{method}"
end
end
deprecatee = Class.new do
def method
end
deprecate :method, deprecator: deprecator
end
deprecatee.new.method
assert deprecator.messages.first.match("DEPRECATION WARNING: deprecator.deprecated_method_warning.method")
end
test "Module::deprecate with custom deprecator" do
custom_deprecator = Struct.new(:deprecation_warning).new
assert_called_with(custom_deprecator, :deprecation_warning, [:method, nil]) do
klass = Class.new do
def method
end
deprecate :method, deprecator: custom_deprecator
end
klass.new.method
end
end
test "DeprecatedConstantProxy with explicit deprecator" do
deprecator = deprecator_with_messages
klass = Class.new
klass.const_set(:OLD, ActiveSupport::Deprecation::DeprecatedConstantProxy.new("klass::OLD", "Object", deprecator))
assert_difference("deprecator.messages.size") do
klass::OLD.to_s
end
end
test "DeprecatedConstantProxy with message" do
deprecator = deprecator_with_messages
klass = Class.new
klass.const_set(:OLD, ActiveSupport::Deprecation::DeprecatedConstantProxy.new("klass::OLD", "Object", deprecator, message: "foo"))
klass::OLD.to_s
assert_match "foo", deprecator.messages.last
end
test "default deprecation_horizon is greater than the current Rails version" do
assert_operator ActiveSupport::Deprecation.new.deprecation_horizon, :>, ActiveSupport::VERSION::STRING
end
test "default gem_name is Rails" do
deprecator = ActiveSupport::Deprecation.new
deprecator.send(:deprecated_method_warning, :deprecated_method, "You are calling deprecated method").tap do |message|
assert_match(/is deprecated and will be removed from Rails/, message)
end
end
test "custom gem_name" do
deprecator = ActiveSupport::Deprecation.new("2.0", "Custom")
deprecator.send(:deprecated_method_warning, :deprecated_method, "You are calling deprecated method").tap do |message|
assert_match(/is deprecated and will be removed from Custom/, message)
end
end
test "Module::deprecate can be called before the target method is defined" do
klass = Class.new(Deprecatee)
klass.deprecate :multi!, deprecator: @deprecator
klass.alias_method :multi!, :multi
assert_deprecated(/multi! is deprecated/, @deprecator) do
assert_equal [1, 2, 3], klass.new.multi!(1, 2, 3)
end
end
test "warn with empty callstack" do
@deprecator.behavior = :silence
assert_nothing_raised do
@deprecator.warn("message", [])
Thread.new { @deprecator.warn("message") }.join
end
end
test "disallowed_warnings is empty by default" do
assert_equal @deprecator.disallowed_warnings, []
end
test "disallowed_warnings can be configured" do
config_warnings = ["unsafe_method is going away"]
@deprecator.disallowed_warnings = config_warnings
assert_equal @deprecator.disallowed_warnings, config_warnings
end
test "disallowed_behavior does not trigger when disallowed_warnings is empty" do
@deprecator.disallowed_behavior = proc { flunk }
assert_deprecated(/fubar/, @deprecator) do
@deprecator.warn("using fubar is deprecated")
end
end
test "disallowed_behavior does not trigger when disallowed_warnings does not match the warning" do
@deprecator.disallowed_behavior = proc { flunk }
@deprecator.disallowed_warnings = ["foo bar"]
assert_deprecated(/fubar/, @deprecator) do
@deprecator.warn("using fubar is deprecated")
end
end
test "disallowed_warnings can match using a substring" do
@deprecator.disallowed_warnings = ["fubar"]
assert_disallowed(/fubar/, @deprecator) do
@deprecator.warn("using fubar is deprecated")
end
end
test "disallowed_warnings can match using a substring as a symbol" do
@deprecator.disallowed_warnings = [:fubar]
assert_disallowed(/fubar/, @deprecator) do
@deprecator.warn("using fubar is deprecated")
end
end
test "disallowed_warnings can match using a regexp" do
@deprecator.disallowed_warnings = [/f[aeiou]+bar/]
assert_disallowed(/fubar/, @deprecator) do
@deprecator.warn("using fubar is deprecated")
end
end
test "disallowed_warnings matches all warnings when set to :all" do
@deprecator.disallowed_warnings = :all
assert_disallowed(/fubar/, @deprecator) do
@deprecator.warn("using fubar is deprecated")
end
end
test "different behaviors for allowed and disallowed warnings" do
@deprecator.disallowed_warnings = :all
@deprecator.behavior = proc { flunk }
assert_disallowed(/fubar/, @deprecator) do
@deprecator.warn("using fubar is deprecated")
end
end
test "disallowed_warnings with the default warning message" do
@deprecator.disallowed_warnings = :all
assert_disallowed(@deprecator) { @deprecator.warn }
@deprecator.disallowed_warnings = ["fubar"]
assert_deprecated(@deprecator) { @deprecator.warn }
end
test "disallowed_behavior callbacks" do
assert_callbacks_called_with(deprecator: @deprecator, message: /fubar/) do |callbacks|
@deprecator.disallowed_behavior = callbacks
@deprecator.disallowed_warnings = ["fubar"]
@deprecator.warn("fubar")
end
end
test "allow" do
@deprecator.disallowed_warnings = :all
assert_disallowed(@deprecator) { @deprecator.warn }
@deprecator.allow do
assert_deprecated(@deprecator) { @deprecator.warn }
end
end
test "allow only allows matching warnings using a substring" do
@deprecator.disallowed_warnings = :all
@deprecator.allow(["foo bar", "baz qux"]) do
assert_deprecated(/foo bar/, @deprecator) { @deprecator.warn("foo bar") }
assert_deprecated(/baz qux/, @deprecator) { @deprecator.warn("baz qux") }
assert_disallowed(/fubar/, @deprecator) { @deprecator.warn("fubar") }
end
end
test "allow only allows matching warnings using a substring as a symbol" do
@deprecator.disallowed_warnings = :all
@deprecator.allow([:"foo bar", :"baz qux"]) do
assert_deprecated(/foo bar/, @deprecator) { @deprecator.warn("foo bar") }
assert_deprecated(/baz qux/, @deprecator) { @deprecator.warn("baz qux") }
assert_disallowed(/fubar/, @deprecator) { @deprecator.warn("fubar") }
end
end
test "allow only allows matching warnings using a regexp" do
@deprecator.disallowed_warnings = :all
@deprecator.allow([/(foo|baz) (bar|qux)/]) do
assert_deprecated(/foo bar/, @deprecator) { @deprecator.warn("foo bar") }
assert_deprecated(/baz qux/, @deprecator) { @deprecator.warn("baz qux") }
assert_disallowed(/fubar/, @deprecator) { @deprecator.warn("fubar") }
end
end
test "allow only affects its block" do
@deprecator.disallowed_warnings = :all
@deprecator.allow do
assert_deprecated(@deprecator) { @deprecator.warn }
end
assert_disallowed(@deprecator) { @deprecator.warn }
end
test "allow only affects the current thread" do
@deprecator.disallowed_warnings = :all
@deprecator.allow do
assert_deprecated(@deprecator) { @deprecator.warn }
Thread.new do
assert_disallowed(@deprecator) { @deprecator.warn }
@deprecator.allow do
assert_deprecated(@deprecator) { @deprecator.warn }
end
assert_disallowed(@deprecator) { @deprecator.warn }
end.join
assert_deprecated(@deprecator) { @deprecator.warn }
end
end
test "allow with :if option" do
@deprecator.disallowed_warnings = :all
@deprecator.allow("fubar", if: true) do
assert_deprecated(/fubar/, @deprecator) { @deprecator.warn("fubar") }
end
@deprecator.allow("fubar", if: false) do
assert_disallowed(/fubar/, @deprecator) { @deprecator.warn("fubar") }
end
end
test "allow with :if option as a proc" do
@deprecator.disallowed_warnings = :all
@deprecator.allow("fubar", if: -> { true }) do
assert_deprecated(/fubar/, @deprecator) { @deprecator.warn("fubar") }
end
@deprecator.allow("fubar", if: -> { false }) do
assert_disallowed(/fubar/, @deprecator) { @deprecator.warn("fubar") }
end
end
test "allow with the default warning message" do
@deprecator.disallowed_warnings = :all
@deprecator.allow(:all) do
assert_deprecated(@deprecator) { @deprecator.warn }
end
@deprecator.allow(["fubar"]) do
assert_disallowed(@deprecator) { @deprecator.warn }
end
end
private
def deprecator_with_messages
klass = Class.new(ActiveSupport::Deprecation)
deprecator = klass.new
deprecator.behavior = Proc.new { |message, callstack| deprecator.messages << message }
def deprecator.messages
@messages ||= []
end
deprecator
end
module ::Rails; end
def with_rails_logger(logger)
::Rails.singleton_class.class_eval do
alias_method :__original_logger, :logger if method_defined?(:logger)
define_method(:logger) { logger }
end
yield logger
ensure
::Rails.singleton_class.class_eval do
if method_defined?(:__original_logger)
alias_method :logger, :__original_logger
undef_method :__original_logger
else
undef_method :logger
end
end
end
# a la collect_deprecations
def collect_disallowed(deprecator)
deprecator ||= ActiveSupport::Deprecation
original_disallowed_behavior = deprecator.disallowed_behavior
disallowed = []
deprecator.disallowed_behavior = proc { |message| disallowed << message }
result = yield
[result, disallowed]
ensure
deprecator.disallowed_behavior = original_disallowed_behavior
end
# a la assert_deprecated
def assert_disallowed(match = nil, deprecator = nil, &block)
match, deprecator = nil, match if match.is_a?(ActiveSupport::Deprecation)
result, disallowed = collect_disallowed(deprecator, &block)
assert_not_empty disallowed, "Expected a disallowed deprecation within the block but received none"
if match
match = Regexp.new(Regexp.escape(match)) unless match.is_a?(Regexp)
assert disallowed.any?(match), "No disallowed deprecations matched #{match}: #{disallowed.inspect}"
end
result
end
def assert_callbacks_called_with(matchers = {})
matchers[:message] ||= String
matchers[:callstack] ||= Array
matchers[:deprecator]&.tap do |deprecator|
matchers[:deprecation_horizon] ||= deprecator.deprecation_horizon
matchers[:gem_name] ||= deprecator.gem_name
end
bindings = []
callbacks = [
lambda { |message, callstack, deprecator| bindings << binding },
proc { |message, callstack, deprecator| bindings << binding },
lambda { |message, callstack, deprecation_horizon, gem_name| bindings << binding },
proc { |message, callstack, deprecation_horizon, gem_name| bindings << binding },
lambda { |message, callstack| bindings << binding },
proc { |message, callstack| bindings << binding },
proc { |message| bindings << binding },
proc { bindings << binding },
lambda do |*args|
message, callstack, deprecator = args
bindings << binding
[message, callstack, deprecator]
end,
lambda do |message, *other|
callstack, deprecator = other
bindings << binding
[callstack, deprecator]
end,
lambda do |message, callstack, *details|
deprecation_horizon, gem_name = details
bindings << binding
[deprecation_horizon, gem_name]
end,
]
yield callbacks
assert_equal callbacks.size, bindings.size
bindings.each do |bound|
matchers.each do |name, matcher|
if bound.local_variable_defined?(name)
assert_operator matcher, :===, bound.local_variable_get(name),
"Unexpected #{name} in callback defined at #{bound.source_location.join(":")}"
end
end
end
end
end