From ae234c4e1a18fc639a21a1871376b147b5212aff Mon Sep 17 00:00:00 2001 From: Micah Geisel Date: Thu, 26 Apr 2018 09:59:07 -0700 Subject: [PATCH] prevent specs from leaking global state modifications. --- lib/database_cleaner/active_record/base.rb | 2 +- .../active_record/base_spec.rb | 1 + .../active_record/transaction_spec.rb | 21 ------------------- .../active_record/truncation/mysql2_spec.rb | 6 +++--- .../active_record/truncation/mysql_spec.rb | 6 +++--- .../truncation/postgresql_spec.rb | 10 ++++----- .../active_record/truncation/sqlite3_spec.rb | 4 ++-- spec/database_cleaner/base_spec.rb | 6 ------ .../data_mapper/truncation/sqlite3_spec.rb | 6 +++--- spec/spec_helper.rb | 6 ------ spec/support/active_record/mysql2_setup.rb | 5 ++--- spec/support/active_record/mysql_setup.rb | 5 ++--- .../support/active_record/postgresql_setup.rb | 5 ++--- spec/support/active_record/sqlite3_setup.rb | 5 ++--- spec/support/data_mapper/sqlite3_setup.rb | 4 +--- 15 files changed, 27 insertions(+), 65 deletions(-) diff --git a/lib/database_cleaner/active_record/base.rb b/lib/database_cleaner/active_record/base.rb index 0c7d439..434ed55 100644 --- a/lib/database_cleaner/active_record/base.rb +++ b/lib/database_cleaner/active_record/base.rb @@ -79,7 +79,7 @@ module DatabaseCleaner if ::ActiveRecord::Base.respond_to?(:descendants) database_name = connection_hash["database"] || connection_hash[:database] models = ::ActiveRecord::Base.descendants - models.detect { |m| m.connection_pool.spec.config[:database] == database_name } + models.select(&:connection_pool).detect { |m| m.connection_pool.spec.config[:database] == database_name } end end diff --git a/spec/database_cleaner/active_record/base_spec.rb b/spec/database_cleaner/active_record/base_spec.rb index 9692691..a0829e9 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' require 'active_record' require 'database_cleaner/active_record/base' require 'database_cleaner/shared_strategy' +require 'support/active_record/schema_setup' class FakeModel def self.connection diff --git a/spec/database_cleaner/active_record/transaction_spec.rb b/spec/database_cleaner/active_record/transaction_spec.rb index 6a4002e..c91e3bd 100644 --- a/spec/database_cleaner/active_record/transaction_spec.rb +++ b/spec/database_cleaner/active_record/transaction_spec.rb @@ -21,23 +21,19 @@ module DatabaseCleaner before do connection.stub(:transaction) connection.stub(begin_transaction_method) - connection.stub(:respond_to?).with(:begin_transaction).and_return(:begin_transaction == begin_transaction_method) end it "should increment open transactions if possible" do - connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true) connection.should_receive(:increment_open_transactions) Transaction.new.start end it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do - connection.stub(:respond_to?).with(:increment_open_transactions).and_return(false) ::ActiveRecord::Base.should_receive(:increment_open_transactions) Transaction.new.start end it "should start a transaction" do - connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true) connection.stub(:increment_open_transactions) connection.should_receive(begin_transaction_method) connection.should_receive(:transaction) @@ -61,9 +57,6 @@ module DatabaseCleaner it "should decrement open transactions if possible" do connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(true) - connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection.stub(:rollback_db_transaction) connection.should_receive(:decrement_open_transactions) @@ -78,9 +71,6 @@ module DatabaseCleaner it "should decrement connection via ActiveRecord::Base if connection won't" do connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection.stub(:rollback_db_transaction) ::ActiveRecord::Base.should_receive(:decrement_open_transactions) @@ -91,15 +81,9 @@ module DatabaseCleaner connection_pool.stub(:connections).and_return([connection, connection_2]) connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection.stub(:rollback_db_transaction) connection_2.should_receive(:open_transactions).and_return(1) - connection_2.stub(:respond_to?).with(:decrement_open_transactions).and_return(false) - connection_2.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection_2.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection_2.stub(:rollback_db_transaction) ::ActiveRecord::Base.should_receive(:decrement_open_transactions).twice @@ -110,9 +94,6 @@ module DatabaseCleaner connection_pool.stub(:connections).and_return([connection, connection_2]) connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(false) connection.stub(:rollback_db_transaction) connection_2.should_receive(:open_transactions).and_return(0) @@ -150,8 +131,6 @@ module DatabaseCleaner it "should decrement connection via ActiveRecord::Base if connection won't" do connection.should_receive(:open_transactions).and_return(1) - connection.stub(:respond_to?).with(:rollback_transaction_records, true).and_return(false) - connection.stub(:respond_to?).with(:rollback_transaction).and_return(true) connection.stub(:rollback_transaction) ::ActiveRecord::Base.should_not_receive(:decrement_open_transactions) diff --git a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb index 0a36ab8..e0aab62 100644 --- a/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql2_spec.rb @@ -7,9 +7,9 @@ require 'database_cleaner/active_record/truncation/shared_fast_truncation' module ActiveRecord module ConnectionAdapters describe do - before(:all) { active_record_mysql2_setup } + before(:all) { MySQL2Helper.active_record_mysql2_setup } - let(:connection) { active_record_mysql2_connection } + let(:connection) { MySQL2Helper.active_record_mysql2_connection } describe "#truncate_table" do it "should truncate the table" do @@ -30,7 +30,7 @@ module ActiveRecord end it_behaves_like "an adapter with pre-count truncation" do - let(:connection) { active_record_mysql2_connection } + let(:connection) { MySQL2Helper.active_record_mysql2_connection } end end end diff --git a/spec/database_cleaner/active_record/truncation/mysql_spec.rb b/spec/database_cleaner/active_record/truncation/mysql_spec.rb index a6eb8e9..4865c62 100644 --- a/spec/database_cleaner/active_record/truncation/mysql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/mysql_spec.rb @@ -7,9 +7,9 @@ require 'database_cleaner/active_record/truncation/shared_fast_truncation' module ActiveRecord module ConnectionAdapters describe do - before(:all) { active_record_mysql_setup } + before(:all) { MySQLHelper.active_record_mysql_setup } - let(:connection) { active_record_mysql_connection } + let(:connection) { MySQLHelper.active_record_mysql_connection } describe "#truncate_table" do it "should truncate the table" do @@ -30,7 +30,7 @@ module ActiveRecord end it_behaves_like "an adapter with pre-count truncation" do - let(:connection) { active_record_mysql_connection } + let(:connection) { MySQLHelper.active_record_mysql_connection } end end end diff --git a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb index 15baf49..41ed1d6 100644 --- a/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +++ b/spec/database_cleaner/active_record/truncation/postgresql_spec.rb @@ -8,18 +8,18 @@ module ActiveRecord module ConnectionAdapters describe "schema_migrations table" do it "is not truncated" do - active_record_pg_migrate + PostgreSQLHelper.active_record_pg_migrate DatabaseCleaner::ActiveRecord::Truncation.new.clean - result = active_record_pg_connection.execute("select count(*) from schema_migrations;") + result = PostgreSQLHelper.active_record_pg_connection.execute("select count(*) from schema_migrations;") result.values.first.should eq ["2"] end end describe do - before(:all) { active_record_pg_setup } + before(:all) { PostgreSQLHelper.active_record_pg_setup } let(:connection) do - active_record_pg_connection + PostgreSQLHelper.active_record_pg_connection end before(:each) do @@ -67,7 +67,7 @@ module ActiveRecord end it_behaves_like "an adapter with pre-count truncation" do - let(:connection) { active_record_pg_connection } + let(:connection) { PostgreSQLHelper.active_record_pg_connection } end end diff --git a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb index 75c212b..82e0678 100644 --- a/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb @@ -6,10 +6,10 @@ require 'database_cleaner/active_record/truncation' module ActiveRecord module ConnectionAdapters describe do - before(:all) { active_record_sqlite3_setup } + before(:all) { SQLite3Helper.active_record_sqlite3_setup } let(:connection) do - active_record_sqlite3_connection + SQLite3Helper.active_record_sqlite3_connection end before(:each) do diff --git a/spec/database_cleaner/base_spec.rb b/spec/database_cleaner/base_spec.rb index d077ed1..a8b8585 100644 --- a/spec/database_cleaner/base_spec.rb +++ b/spec/database_cleaner/base_spec.rb @@ -302,14 +302,11 @@ module DatabaseCleaner end it "should check that strategy supports db specification" do - strategy.should_receive(:respond_to?).with(:db=).and_return(true) strategy.stub(:db=) subject.strategy_db = :a_db end context "when strategy supports db specification" do - before(:each) { strategy.stub(:respond_to?).with(:db=).and_return true } - it "should pass db to the strategy" do strategy.should_receive(:db=).with(:a_db) subject.strategy_db = :a_db @@ -317,8 +314,6 @@ module DatabaseCleaner end context "when strategy doesn't supports db specification" do - before(:each) { strategy.stub(:respond_to?).with(:db=).and_return false } - it "should check to see if db is :default" do db = double("default") db.should_receive(:==).with(:default).and_return(true) @@ -540,7 +535,6 @@ module DatabaseCleaner end it "should use available_strategies (for the error message) if its available" do - strategy_class.stub(:respond_to?).with(:available_strategies).and_return(true) strategy_class.should_receive(:available_strategies).and_return([]) subject.stub(:orm_module).and_return(strategy_class) diff --git a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb index e294169..c5863a5 100644 --- a/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +++ b/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb @@ -1,18 +1,18 @@ require 'spec_helper' require 'dm-core' require 'dm-sqlite-adapter' -require File.expand_path('../../../../support/data_mapper/sqlite3_setup', __FILE__) +require 'support/data_mapper/sqlite3_setup' require 'database_cleaner/data_mapper/truncation' module DataMapper module ConnectionAdapters describe do - before(:all) { data_mapper_sqlite3_setup } + before(:all) { DataMapperSQLite3Helper.data_mapper_sqlite3_setup } let(:adapter) { DataMapperSQLite3Adapter } let(:connection) do - data_mapper_sqlite3_connection + DataMapperSQLite3Helper.data_mapper_sqlite3_connection end before(:each) do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0d64961..9558e58 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,16 +23,10 @@ RSpec.configure do |config| config.run_all_when_everything_filtered = true config.expect_with :rspec do |expectations| - # Enable only the newer, non-monkey-patching expect syntax. - # For more details, see: - # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax expectations.syntax = [:should, :expect] end config.mock_with :rspec do |mocks| - # Enable only the newer, non-monkey-patching expect syntax. - # For more details, see: - # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ mocks.syntax = [:should, :expect] end end diff --git a/spec/support/active_record/mysql2_setup.rb b/spec/support/active_record/mysql2_setup.rb index d0caf24..6e76ad6 100644 --- a/spec/support/active_record/mysql2_setup.rb +++ b/spec/support/active_record/mysql2_setup.rb @@ -2,6 +2,8 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' module MySQL2Helper + extend self + puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql2" # require 'logger' @@ -39,6 +41,3 @@ module MySQL2Helper end end -RSpec.configure do |c| - c.include MySQL2Helper -end diff --git a/spec/support/active_record/mysql_setup.rb b/spec/support/active_record/mysql_setup.rb index 7e3d20a..e0ade73 100644 --- a/spec/support/active_record/mysql_setup.rb +++ b/spec/support/active_record/mysql_setup.rb @@ -2,6 +2,8 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' module MySQLHelper + extend self + puts "Active Record #{ActiveRecord::VERSION::STRING}, mysql" # require 'logger' @@ -39,6 +41,3 @@ module MySQLHelper end end -RSpec.configure do |c| - c.include MySQLHelper -end diff --git a/spec/support/active_record/postgresql_setup.rb b/spec/support/active_record/postgresql_setup.rb index 26ec12b..fb851ef 100644 --- a/spec/support/active_record/postgresql_setup.rb +++ b/spec/support/active_record/postgresql_setup.rb @@ -2,6 +2,8 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' module PostgreSQLHelper + extend self + puts "Active Record #{ActiveRecord::VERSION::STRING}, pg" # ActiveRecord::Base.logger = Logger.new(STDERR) @@ -43,6 +45,3 @@ module PostgreSQLHelper end end -RSpec.configure do |c| - c.include PostgreSQLHelper -end diff --git a/spec/support/active_record/sqlite3_setup.rb b/spec/support/active_record/sqlite3_setup.rb index 0ba264a..8047c83 100644 --- a/spec/support/active_record/sqlite3_setup.rb +++ b/spec/support/active_record/sqlite3_setup.rb @@ -2,6 +2,8 @@ require 'support/active_record/database_setup' require 'support/active_record/schema_setup' module SQLite3Helper + extend self + puts "Active Record #{ActiveRecord::VERSION::STRING}, sqlite3" # ActiveRecord::Base.logger = Logger.new(STDERR) @@ -35,6 +37,3 @@ module SQLite3Helper end end -RSpec.configure do |c| - c.include SQLite3Helper -end diff --git a/spec/support/data_mapper/sqlite3_setup.rb b/spec/support/data_mapper/sqlite3_setup.rb index a133210..98b29be 100644 --- a/spec/support/data_mapper/sqlite3_setup.rb +++ b/spec/support/data_mapper/sqlite3_setup.rb @@ -2,6 +2,7 @@ require 'support/active_record/database_setup' require 'support/data_mapper/schema_setup' module DataMapperSQLite3Helper + extend self puts "DataMapper #{DataMapper::VERSION}, sqlite3" @@ -34,6 +35,3 @@ module DataMapperSQLite3Helper end end -RSpec.configure do |c| - c.include(DataMapperSQLite3Helper) -end