From 213ff7ca0c27739715bda83a9c6237c03a577c2b Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Sun, 26 Apr 2015 22:17:05 -0500 Subject: Add tests to verify exit status for create/drop failures * Running the db:create task when the database already exists isn't really an error case. That is processing may proceed in this case because the database exists as requested. So let's validate that behavior with a test. * Likewise, if the database doesn't exist when running the db:drop task processing may continue as the requested condition is already met. Thus a test. --- railties/test/application/rake/dbs_test.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'railties/test/application/rake') diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index e7beab8b5e..8731b7d86b 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -49,6 +49,34 @@ module ApplicationTests db_create_and_drop database_url_db_name end + def with_database_existing + Dir.chdir(app_path) do + set_database_url + `bin/rake db:create` + yield + `bin/rake db:drop` + end + end + + test 'db:create failure because database exists' do + with_database_existing do + output = `bin/rake db:create 2>&1` + assert_match /already exists/, output + assert_equal 0, $?.exitstatus + end + end + + test 'db:drop failure because database does not exist' do + Dir.chdir(app_path) do + output = `bin/rake db:drop 2>&1` + # This assertion should work, but it does not. The SQLite3 adapter + # does not raise an error when nothing exists to drop. + # https://github.com/rails/rails/blob/f00554a8226b9529c38be1f3e61b6b1888682fb4/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L34-L37 + # assert_match /does not exist/, output + assert_equal 0, $?.exitstatus + end + end + def db_migrate_and_status(expected_database) Dir.chdir(app_path) do `bin/rails generate model book title:string; -- cgit v1.2.3 From 1cd35be3eb04da88d790493d3ce9807efeaeddba Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Fri, 7 Aug 2015 15:05:36 -0500 Subject: Fix test of drop failure * Previously the sqlite3 adapter could not "fail" on drop. Now an error is raised when no file exists. * Also updates purge to be resilient of drop failures. This is how purge is expected to behave. --- railties/test/application/rake/dbs_test.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'railties/test/application/rake') diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index 8731b7d86b..9b62764db2 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -69,10 +69,7 @@ module ApplicationTests test 'db:drop failure because database does not exist' do Dir.chdir(app_path) do output = `bin/rake db:drop 2>&1` - # This assertion should work, but it does not. The SQLite3 adapter - # does not raise an error when nothing exists to drop. - # https://github.com/rails/rails/blob/f00554a8226b9529c38be1f3e61b6b1888682fb4/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L34-L37 - # assert_match /does not exist/, output + assert_match /does not exist/, output assert_equal 0, $?.exitstatus end end -- cgit v1.2.3 From 2893e6c0a459a91a033d357cd15cc4d14e7acbc1 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Sun, 26 Apr 2015 22:20:08 -0500 Subject: Exit with non-zero status when db:create fails * If the create task fails for a reason other than the database already existing, processing should end. This is indicated by a non-zero exit status. * Since the backtrace is already printed to screen, we forgo printing it again by using an explicit call to `exit`. * :warning: This modifies the behavior of the db:create task slightly in that the stack trace is no longer printed by default. If the `--trace` option is used, it will print the trace _after_ the error message. --- railties/test/application/rake/dbs_test.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'railties/test/application/rake') diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index 9b62764db2..bd885b520e 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -66,6 +66,23 @@ module ApplicationTests end end + def with_bad_permissions + Dir.chdir(app_path) do + set_database_url + FileUtils.chmod("-w", "db") + yield + FileUtils.chmod("+w", "db") + end + end + + test 'db:create failure because bad permissions' do + with_bad_permissions do + output = `bin/rake db:create 2>&1` + assert_match /Couldn't create database/, output + assert_equal 1, $?.exitstatus + end + end + test 'db:drop failure because database does not exist' do Dir.chdir(app_path) do output = `bin/rake db:drop 2>&1` -- cgit v1.2.3 From c2e597a7362fd0b497da7c622717406bb70cd360 Mon Sep 17 00:00:00 2001 From: Jay Hayes Date: Sun, 26 Apr 2015 22:21:54 -0500 Subject: Exit with non-zero status when db:drop fails * If the drop task fails for a reason other than the database not existing, processing should end. This is indicated by a non-zero exit status. * Since the backtrace is already printed to screen, we forgo printing it again by using an explicit call to `exit`. * :warning: This modifies the behavior of the db:create task slightly in that the stack trace is no longer printed by default. If the `--trace` option is used, it will print the trace _after_ the error message. --- railties/test/application/rake/dbs_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'railties/test/application/rake') diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index bd885b520e..f94d08673a 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -91,6 +91,16 @@ module ApplicationTests end end + test 'db:drop failure because bad permissions' do + with_database_existing do + with_bad_permissions do + output = `bin/rake db:drop 2>&1` + assert_match /Couldn't drop/, output + assert_equal 1, $?.exitstatus + end + end + end + def db_migrate_and_status(expected_database) Dir.chdir(app_path) do `bin/rails generate model book title:string; -- cgit v1.2.3