Skip to content

Commit

Permalink
Avoid chdir error in Railties tests on Ruby master
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneius committed Oct 3, 2020
1 parent 2bd1ae2 commit ae5ecfe
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 49 deletions.
37 changes: 19 additions & 18 deletions railties/test/application/rake/dbs_test.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

require "isolation/abstract_unit"
require "chdir_helpers"
require "env_helpers"

module ApplicationTests
module RakeTests
class RakeDbsTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation, EnvHelpers
include ActiveSupport::Testing::Isolation, ChdirHelpers, EnvHelpers

def setup
build_app
Expand All @@ -28,7 +29,7 @@ def set_database_url
end

def db_create_and_drop(expected_database, environment_loaded: true)
Dir.chdir(app_path) do
chdir(app_path) do
output = rails("db:create")
assert_match(/Created database/, output)
assert File.exist?(expected_database)
Expand All @@ -41,7 +42,7 @@ def db_create_and_drop(expected_database, environment_loaded: true)
end

def db_create_with_warning(expected_database)
Dir.chdir(app_path) do
chdir(app_path) do
output = rails("db:create")
assert_match(/Rails couldn't infer whether you are using multiple databases/, output)
assert_match(/Created database/, output)
Expand Down Expand Up @@ -207,7 +208,7 @@ def db_create_with_warning(expected_database)
end

def with_database_existing
Dir.chdir(app_path) do
chdir(app_path) do
set_database_url
rails "db:create"
yield
Expand All @@ -223,7 +224,7 @@ def with_database_existing
end

def with_bad_permissions
Dir.chdir(app_path) do
chdir(app_path) do
skip "Can't avoid permissions as root" if Process.uid.zero?

set_database_url
Expand Down Expand Up @@ -271,7 +272,7 @@ def with_bad_permissions
end

test "db:truncate_all truncates all non-internal tables" do
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:migrate"
require "#{app_path}/config/environment"
Expand All @@ -296,7 +297,7 @@ def with_bad_permissions

test "db:truncate_all does not truncate any tables when environment is protected" do
with_rails_env "production" do
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:migrate"
require "#{app_path}/config/environment"
Expand Down Expand Up @@ -344,7 +345,7 @@ def db_migrate_and_status(expected_database)
end

def db_schema_dump
Dir.chdir(app_path) do
chdir(app_path) do
args = ["generate", "model", "book", "title:string"]
rails args
rails "db:migrate", "db:schema:dump"
Expand All @@ -362,7 +363,7 @@ def db_schema_dump
end

def db_schema_cache_dump(filename = "db/schema_cache.yml")
Dir.chdir(app_path) do
chdir(app_path) do
rails "db:schema:cache:dump"

cache_size = lambda { rails("runner", "p ActiveRecord::Base.connection.schema_cache.size").strip }
Expand All @@ -380,7 +381,7 @@ def db_schema_cache_dump(filename = "db/schema_cache.yml")
end

test "db:schema:cache:dump with custom filename" do
Dir.chdir(app_path) do
chdir(app_path) do
File.open("#{app_path}/config/database.yml", "w") do |f|
f.puts <<-YAML
default: &default
Expand Down Expand Up @@ -413,7 +414,7 @@ def db_schema_cache_dump(filename = "db/schema_cache.yml")
end

test "db:schema:cache:dump first config wins" do
Dir.chdir(app_path) do
chdir(app_path) do
File.open("#{app_path}/config/database.yml", "w") do |f|
f.puts <<-YAML
default: &default
Expand All @@ -439,7 +440,7 @@ def db_schema_cache_dump(filename = "db/schema_cache.yml")
end

def db_fixtures_load(expected_database)
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
reload
rails "db:migrate", "db:fixtures:load"
Expand Down Expand Up @@ -472,7 +473,7 @@ def db_fixtures_load(expected_database)
end

def db_structure_dump_and_load(expected_database)
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:migrate", "db:structure:dump"
structure_dump = File.read("db/structure.sql")
Expand Down Expand Up @@ -584,7 +585,7 @@ def db_structure_dump_and_load(expected_database)
end

def db_test_load_structure
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:migrate", "db:structure:dump", "db:test:load_structure"
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
Expand Down Expand Up @@ -674,7 +675,7 @@ def db_test_load_structure
end

test "db:seed:replant truncates all non-internal tables and loads the seeds" do
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:migrate"
require "#{app_path}/config/environment"
Expand Down Expand Up @@ -707,7 +708,7 @@ def db_test_load_structure

test "db:seed:replant does not truncate any tables and does not load the seeds when environment is protected" do
with_rails_env "production" do
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:migrate"
require "#{app_path}/config/environment"
Expand Down Expand Up @@ -740,7 +741,7 @@ def db_test_load_structure
end

test "db:prepare setup the database" do
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
output = rails("db:prepare")
assert_match(/CreateBooks: migrated/, output)
Expand All @@ -756,7 +757,7 @@ def db_test_load_structure
end

test "db:prepare does not touch schema when dumping is disabled" do
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:create", "db:migrate"

Expand Down
5 changes: 3 additions & 2 deletions railties/test/application/rake/log_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

require "isolation/abstract_unit"
require "chdir_helpers"

module ApplicationTests
module RakeTests
class LogTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
include ActiveSupport::Testing::Isolation, ChdirHelpers

def setup
build_app
Expand All @@ -16,7 +17,7 @@ def teardown
end

test "log:clear clear all environments log files by default" do
Dir.chdir(app_path) do
chdir(app_path) do
File.open("config/environments/staging.rb", "w")

File.write("log/staging.log", "staging")
Expand Down
13 changes: 8 additions & 5 deletions railties/test/application/rake/migrations_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

require "isolation/abstract_unit"
require "chdir_helpers"

module ApplicationTests
module RakeTests
class RakeMigrationsTest < ActiveSupport::TestCase
include ChdirHelpers

def setup
build_app
FileUtils.rm_rf("#{app_path}/config/environments")
Expand Down Expand Up @@ -265,7 +268,7 @@ class TwoMigration < ActiveRecord::Migration::Current
end

test "raise error on any move when current migration does not exist" do
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "migration", "add_email_to_users", "email:string"
rails "db:migrate"
Expand Down Expand Up @@ -411,7 +414,7 @@ class TwoMigration < ActiveRecord::Migration::Current
test "schema generation when dump_schema_after_migration is set" do
add_to_config("config.active_record.dump_schema_after_migration = false")

Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
output = rails("generate", "model", "author", "name:string")
version = output =~ %r{[^/]+db/migrate/(\d+)_create_authors\.rb} && $1
Expand All @@ -422,7 +425,7 @@ class TwoMigration < ActiveRecord::Migration::Current

add_to_config("config.active_record.dump_schema_after_migration = true")

Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "reviews", "book_id:integer"
rails "db:migrate"

Expand All @@ -432,7 +435,7 @@ class TwoMigration < ActiveRecord::Migration::Current
end

test "default schema generation after migration" do
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:migrate"

Expand All @@ -442,7 +445,7 @@ class TwoMigration < ActiveRecord::Migration::Current
end

test "migration status migrated file is deleted" do
Dir.chdir(app_path) do
chdir(app_path) do
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "migration", "add_email_to_users", "email:string"
rails "db:migrate"
Expand Down
Loading

0 comments on commit ae5ecfe

Please sign in to comment.