Skip to content

Commit

Permalink
Add Deprecator.skip_warning? ability to silence deprecators on tests (
Browse files Browse the repository at this point in the history
#2956)

* Added skip_warning ability to deprecators for tests

---------

Co-authored-by: Stefanni Brasil <[email protected]>
Co-authored-by: Thiago Araujo <[email protected]>
  • Loading branch information
3 people authored Jul 9, 2024
1 parent f7a839b commit 2b5df17
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/faker/locations/australia.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

module Faker
extend Deprecator

class Locations
class Australia < Base
class << self
Expand Down Expand Up @@ -48,5 +46,7 @@ def state
end
end
end

include Deprecator
deprecate_generator('Australia', Locations::Australia)
end
25 changes: 24 additions & 1 deletion lib/helpers/deprecator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def self.included(base)
extension = Module.new do
def const_missing(missing_const_name)
if class_variable_defined?(:@@_deprecated_constants) && (replacement = class_variable_get(:@@_deprecated_constants)[missing_const_name.to_s])
$stdout.puts("DEPRECATION WARNING: #{name}::#{replacement[:old_generator]} is deprecated. Use #{replacement[:new_constant]} instead.")
unless Faker::Deprecator.skip_warning?
$stdout.puts("DEPRECATION WARNING: #{name}::#{replacement[:old_generator]} is deprecated. Use #{replacement[:new_constant]} instead.")
end

return replacement[:new_constant]
end

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

base.singleton_class.prepend extension
end

def self.skip_warning
original = Faker::Deprecator.skip
Faker::Deprecator.skip = true
yield
ensure
Faker::Deprecator.skip = original
end

def self.skip_warning?
skip == true
end

def self.skip
@skip ||= false
end

def self.skip=(value)
@skip = value
end
end
end
# rubocop:enable Style/ClassVars
4 changes: 3 additions & 1 deletion test/faker/default/test_faker_id_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ def assert_valid_south_african_id_number(sample)

class TestFakerIDNumber < Test::Unit::TestCase
def setup
@tester = Faker::IDNumber
Faker::Deprecator.skip_warning do
@tester = Faker::IDNumber
end
end

def test_valid_ssn
Expand Down
12 changes: 9 additions & 3 deletions test/faker/default/test_faker_theater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ def test_play

# with test_faker_show.rb
class TestFakerShow < Test::Unit::TestCase
def setup
Faker::Deprecator.skip_warning do
@tester = Faker::Show
end
end

def test_adult_musical
assert_match(/\w+/, Faker::Show.adult_musical)
assert_match(/\w+/, @tester.adult_musical)
end

def test_kids_musical
assert_match(/\w+/, Faker::Show.kids_musical)
assert_match(/\w+/, @tester.kids_musical)
end

def test_play
assert_match(/\w+/, Faker::Show.play)
assert_match(/\w+/, @tester.play)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

class TestFakerFmaBrotherhood < Test::Unit::TestCase
def setup
@tester = Faker::JapaneseMedia::FmaBrotherhood
Faker::Deprecator.skip_warning do
@tester = Faker::JapaneseMedia::FmaBrotherhood
end
end

def test_character
Expand Down
12 changes: 9 additions & 3 deletions test/faker/locations/test_faker_australia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ def test_state
end

class TestFakerAustralia < Test::Unit::TestCase
def setup
Faker::Deprecator.skip_warning do
@tester = Faker::Australia
end
end

def test_deprecated_location
assert_match(/\w+/, Faker::Australia.location)
assert_match(/\w+/, @tester.location)
end

def test_deprecated_animal
assert_match(/\w+/, Faker::Australia.animal)
assert_match(/\w+/, @tester.animal)
end

def test_state
assert_match(/\w+/, Faker::Australia.state)
assert_match(/\w+/, @tester.state)
end
end

0 comments on commit 2b5df17

Please sign in to comment.