diff --git a/rakelib/acceptance_test.rake b/rakelib/acceptance_test.rake index 2c3764ee85..290fdac01f 100644 --- a/rakelib/acceptance_test.rake +++ b/rakelib/acceptance_test.rake @@ -80,11 +80,15 @@ namespace :test do end desc "Run acceptance tests for the #{system} without collectstatic or db migrations" - task "#{system}:fast", [:harvest_args] => [ - :clean_reports_dir, ACCEPTANCE_REPORT_DIR, - ] do |t, args| + task "#{system}:fast", [:harvest_args] => [:clean_reports_dir, ACCEPTANCE_REPORT_DIR] do |t, args| args.with_defaults(:harvest_args => '') - run_acceptance_tests(system, args.harvest_args) + + begin + run_acceptance_tests(system, args.harvest_args) + ensure + Rake::Task[:'test:clean_mongo'].reenable + Rake::Task[:'test:clean_mongo'].invoke + end end end end diff --git a/rakelib/tests.rake b/rakelib/tests.rake index e77f129280..559fd2acca 100644 --- a/rakelib/tests.rake +++ b/rakelib/tests.rake @@ -79,7 +79,15 @@ TEST_TASK_DIRS = [] # messing with static files. task "fasttest_#{system}", [:test_id] => [test_id_dir, report_dir, :clean_reports_dir] do |t, args| args.with_defaults(:test_id => nil) - run_tests(system, report_dir, args.test_id) + + + + begin + run_tests(system, report_dir, args.test_id) + ensure + Rake::Task[:'test:clean_mongo'].reenable + Rake::Task[:'test:clean_mongo'].invoke + end end task :fasttest => "fasttest_#{system}" @@ -97,13 +105,18 @@ Dir["common/lib/*"].select{|lib| File.directory?(lib)}.each do |lib| desc "Run tests for common lib #{lib}" task "test_#{lib}", [:test_id] => [ - test_id_dir, report_dir, :clean_test_files, - :clean_reports_dir, :install_prereqs + test_id_dir, report_dir, :clean_test_files, :clean_reports_dir, :install_prereqs ] do |t, args| + args.with_defaults(:test_id => lib) ENV['NOSE_XUNIT_FILE'] = File.join(report_dir, "nosetests.xml") cmd = "nosetests --id-file=#{test_ids} #{args.test_id}" - test_sh(lib, run_under_coverage(cmd, lib)) + begin + test_sh(lib, run_under_coverage(cmd, lib)) + ensure + Rake::Task[:'test:clean_mongo'].reenable + Rake::Task[:'test:clean_mongo'].invoke + end end TEST_TASK_DIRS << lib @@ -128,6 +141,11 @@ end namespace :test do desc "Run all python tests" task :python, [:test_id] + + desc "Drop Mongo databases created by the test suite" + task :clean_mongo do + sh("mongo #{REPO_ROOT}/scripts/delete-mongo-test-dbs.js") + end end desc "Build the html, xml, and diff coverage reports" diff --git a/scripts/delete-mongo-test-dbs.js b/scripts/delete-mongo-test-dbs.js new file mode 100644 index 0000000000..251ed229b4 --- /dev/null +++ b/scripts/delete-mongo-test-dbs.js @@ -0,0 +1,24 @@ +/* +Drop all Mongo test databases. + +Usage example: + + mongo delete-mongo-test-dbs.js + +will drop every database that starts with "test_" or "acceptance_", +but ignore other databases. +*/ + + +String.prototype.startsWith = function(substring) { + return (this.indexOf(substring) == 0); +} + +var dbNameList = db.getMongo().getDBNames(); +for (var i in dbNameList) { + if (dbNameList[i].startsWith('test_') || dbNameList[i].startsWith('acceptance_')) { + dbToDrop = db.getMongo().getDB(dbNameList[i]); + print("Dropping test db " + dbNameList[i]); + dbToDrop.dropDatabase(); + } +}