Skip to content

Commit

Permalink
move #called_externally to database_cleaner/deprecation, and test.
Browse files Browse the repository at this point in the history
  • Loading branch information
botandrose-machine authored and botandrose committed Apr 4, 2020
1 parent 4dffa84 commit 3f35961
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
10 changes: 3 additions & 7 deletions lib/database_cleaner/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ def clean_with!
# 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=
Expand All @@ -105,7 +105,7 @@ def set_strategy_db(strategy, desired_db)
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
Expand Down Expand Up @@ -149,9 +149,5 @@ def require_orm_strategy(orm, strategy)
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
12 changes: 3 additions & 9 deletions lib/database_cleaner/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def init_cleaners
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?
Expand All @@ -119,23 +119,17 @@ def connections
# 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
5 changes: 5 additions & 0 deletions lib/database_cleaner/deprecation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ def deprecate message
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 = {}
Expand Down
26 changes: 26 additions & 0 deletions spec/database_cleaner/deprecation_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3f35961

Please sign in to comment.