From cafe8d053e9cff30d07a94f372f69e996a88be16 Mon Sep 17 00:00:00 2001 From: Will Leinweber Date: Tue, 6 Nov 2018 16:14:01 -0800 Subject: [PATCH] Allow postgres:///dbname as a local url --- lib/database_cleaner/safeguard.rb | 13 +++++++++++-- spec/database_cleaner/safeguard_spec.rb | 8 ++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/database_cleaner/safeguard.rb b/lib/database_cleaner/safeguard.rb index 64d8072..900353d 100644 --- a/lib/database_cleaner/safeguard.rb +++ b/lib/database_cleaner/safeguard.rb @@ -39,7 +39,7 @@ module DatabaseCleaner class RemoteDatabaseUrl - LOCAL = %w(localhost .local 127.0.0.1 sqlite3:) + LOCAL = %w(localhost 127.0.0.1) def run raise Error::RemoteDatabaseUrl if !skip? && given? @@ -52,7 +52,16 @@ module DatabaseCleaner end def remote?(url) - url && !LOCAL.any? { |str| url.include?(str) } + return false unless url + + parsed = URI.parse(url) + return false if parsed.scheme == 'sqlite3:' + + host = parsed.host + return false unless host + return false if LOCAL.include?(host) + return false if host.end_with? '.local' + true end def skip? diff --git a/spec/database_cleaner/safeguard_spec.rb b/spec/database_cleaner/safeguard_spec.rb index 837510f..87861e4 100644 --- a/spec/database_cleaner/safeguard_spec.rb +++ b/spec/database_cleaner/safeguard_spec.rb @@ -21,6 +21,14 @@ module DatabaseCleaner end end + describe 'to a local, empty-host url' do + let(:database_url) { 'postgres:///' } + + it 'does not raise' do + expect { cleaner.start }.to_not raise_error + end + end + describe 'to a local tld url' do let(:database_url) { 'postgres://postgres.local' }