move #called_externally to database_cleaner/deprecation, and test.

This commit is contained in:
Micah Geisel 2020-04-03 10:25:33 -07:00 committed by Micah Geisel
parent 4dffa84b68
commit 3f35961c64
4 changed files with 37 additions and 16 deletions

View file

@ -87,14 +87,14 @@ module DatabaseCleaner
# TODO privatize the following methods in 2.0
def strategy_db=(desired_db)
if called_externally?(caller)
if DatabaseCleaner.called_externally?(__FILE__, caller)
DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].strategy_db=` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].db=` instead."
end
set_strategy_db(strategy, desired_db)
end
def set_strategy_db(strategy, desired_db)
if called_externally?(caller)
if DatabaseCleaner.called_externally?(__FILE__, caller)
DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].set_strategy_db=` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].db=` instead."
end
if strategy.respond_to? :db=
@ -105,7 +105,7 @@ module DatabaseCleaner
end
def create_strategy(*args)
if called_externally?(caller)
if DatabaseCleaner.called_externally?(__FILE__, caller)
DatabaseCleaner.deprecate "Calling `DatabaseCleaner[...].create_strategy` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner[...].strategy=` instead."
end
strategy, *strategy_args = args
@ -149,9 +149,5 @@ module DatabaseCleaner
rescue LoadError
raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
end
def called_externally?(caller)
__FILE__ != caller.first.split(":").first
end
end
end

View file

@ -109,7 +109,7 @@ module DatabaseCleaner
end
def connections
if called_externally?(caller)
if DatabaseCleaner.called_externally?(__FILE__, caller)
DatabaseCleaner.deprecate "Calling `DatabaseCleaner.connections` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner.cleaners`, instead."
end
add_cleaner(:autodetect) if @cleaners.none?
@ -119,23 +119,17 @@ module DatabaseCleaner
# TODO privatize the following methods in 2.0
def add_cleaner(orm, opts = {})
if called_externally?(caller)
if DatabaseCleaner.called_externally?(__FILE__, caller)
DatabaseCleaner.deprecate "Calling `DatabaseCleaner.add_cleaner` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner.[]`, instead."
end
@cleaners.add_cleaner(orm, opts = {})
end
def remove_duplicates
if called_externally?(caller)
if DatabaseCleaner.called_externally?(__FILE__, caller)
DatabaseCleaner.deprecate "Calling `DatabaseCleaner.remove_duplicates` is deprecated, and will be removed in database_cleaner 2.0 with no replacement."
end
@cleaners.remove_duplicates
end
private
def called_externally?(caller)
__FILE__ != caller.first.split(":").first
end
end
end

View file

@ -6,6 +6,11 @@ module DatabaseCleaner
end
module_function :deprecate
def called_externally?(file, caller)
file != caller.first.split(":").first
end
module_function :called_externally?
class Deprecator
def initialize
@methods_already_warned = {}

View file

@ -0,0 +1,26 @@
require "database_cleaner/deprecation"
RSpec.describe DatabaseCleaner do
describe ".called_externally?" do
let(:path) { "/home/DatabaseCleaner/database_cleaner/spec/database_cleaner/deprecation_spec.rb" }
it "returns false if the supplied file is the first file in the backtrace" do
backtrace = [
"/home/DatabaseCleaner/database_cleaner/spec/database_cleaner/deprecation_spec.rb:9 in `it'",
"/home/DatabaseCleaner/lib/rspec/core/configuration.rb:1954:in `load'",
"/home/DatabaseCleaner/lib/rspec/core/configuration.rb:1954:in `load_spec_file_handling_errors'",
"/home/DatabaseCleaner/lib/rspec/core/configuration.rb:1496:in `block in load_spec_files'",
]
expect(DatabaseCleaner.called_externally?(path, backtrace)).to eq false
end
it "returns true if the supplied file is not the first file in the backtrace" do
backtrace = [
"/home/DatabaseCleaner/lib/rspec/core/configuration.rb:1954:in `load'",
"/home/DatabaseCleaner/lib/rspec/core/configuration.rb:1954:in `load_spec_file_handling_errors'",
"/home/DatabaseCleaner/lib/rspec/core/configuration.rb:1496:in `block in load_spec_files'",
]
expect(DatabaseCleaner.called_externally?(path, backtrace)).to eq true
end
end
end