Skip to content

Commit 2b5df17

Browse files
keshavbiswasbs-catsthdaraujo
authored
Add Deprecator.skip_warning? ability to silence deprecators on tests (#2956)
* Added skip_warning ability to deprecators for tests --------- Co-authored-by: Stefanni Brasil <[email protected]> Co-authored-by: Thiago Araujo <[email protected]>
1 parent f7a839b commit 2b5df17

File tree

6 files changed

+50
-11
lines changed

6 files changed

+50
-11
lines changed

lib/faker/locations/australia.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
module Faker
4-
extend Deprecator
5-
64
class Locations
75
class Australia < Base
86
class << self
@@ -48,5 +46,7 @@ def state
4846
end
4947
end
5048
end
49+
50+
include Deprecator
5151
deprecate_generator('Australia', Locations::Australia)
5252
end

lib/helpers/deprecator.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ def self.included(base)
1010
extension = Module.new do
1111
def const_missing(missing_const_name)
1212
if class_variable_defined?(:@@_deprecated_constants) && (replacement = class_variable_get(:@@_deprecated_constants)[missing_const_name.to_s])
13-
$stdout.puts("DEPRECATION WARNING: #{name}::#{replacement[:old_generator]} is deprecated. Use #{replacement[:new_constant]} instead.")
13+
unless Faker::Deprecator.skip_warning?
14+
$stdout.puts("DEPRECATION WARNING: #{name}::#{replacement[:old_generator]} is deprecated. Use #{replacement[:new_constant]} instead.")
15+
end
16+
1417
return replacement[:new_constant]
1518
end
1619

@@ -25,6 +28,26 @@ def deprecate_generator(old_generator_name, new_generator_constant)
2528

2629
base.singleton_class.prepend extension
2730
end
31+
32+
def self.skip_warning
33+
original = Faker::Deprecator.skip
34+
Faker::Deprecator.skip = true
35+
yield
36+
ensure
37+
Faker::Deprecator.skip = original
38+
end
39+
40+
def self.skip_warning?
41+
skip == true
42+
end
43+
44+
def self.skip
45+
@skip ||= false
46+
end
47+
48+
def self.skip=(value)
49+
@skip = value
50+
end
2851
end
2952
end
3053
# rubocop:enable Style/ClassVars

test/faker/default/test_faker_id_number.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ def assert_valid_south_african_id_number(sample)
291291

292292
class TestFakerIDNumber < Test::Unit::TestCase
293293
def setup
294-
@tester = Faker::IDNumber
294+
Faker::Deprecator.skip_warning do
295+
@tester = Faker::IDNumber
296+
end
295297
end
296298

297299
def test_valid_ssn

test/faker/default/test_faker_theater.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,21 @@ def test_play
1818

1919
# with test_faker_show.rb
2020
class TestFakerShow < Test::Unit::TestCase
21+
def setup
22+
Faker::Deprecator.skip_warning do
23+
@tester = Faker::Show
24+
end
25+
end
26+
2127
def test_adult_musical
22-
assert_match(/\w+/, Faker::Show.adult_musical)
28+
assert_match(/\w+/, @tester.adult_musical)
2329
end
2430

2531
def test_kids_musical
26-
assert_match(/\w+/, Faker::Show.kids_musical)
32+
assert_match(/\w+/, @tester.kids_musical)
2733
end
2834

2935
def test_play
30-
assert_match(/\w+/, Faker::Show.play)
36+
assert_match(/\w+/, @tester.play)
3137
end
3238
end

test/faker/japanese_media/test_faker_fullmetal_alchemist_brotherhood.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
class TestFakerFmaBrotherhood < Test::Unit::TestCase
66
def setup
7-
@tester = Faker::JapaneseMedia::FmaBrotherhood
7+
Faker::Deprecator.skip_warning do
8+
@tester = Faker::JapaneseMedia::FmaBrotherhood
9+
end
810
end
911

1012
def test_character

test/faker/locations/test_faker_australia.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@ def test_state
1717
end
1818

1919
class TestFakerAustralia < Test::Unit::TestCase
20+
def setup
21+
Faker::Deprecator.skip_warning do
22+
@tester = Faker::Australia
23+
end
24+
end
25+
2026
def test_deprecated_location
21-
assert_match(/\w+/, Faker::Australia.location)
27+
assert_match(/\w+/, @tester.location)
2228
end
2329

2430
def test_deprecated_animal
25-
assert_match(/\w+/, Faker::Australia.animal)
31+
assert_match(/\w+/, @tester.animal)
2632
end
2733

2834
def test_state
29-
assert_match(/\w+/, Faker::Australia.state)
35+
assert_match(/\w+/, @tester.state)
3036
end
3137
end

0 commit comments

Comments
 (0)