1
1
# frozen_string_literal: true
2
2
3
3
require "isolation/abstract_unit"
4
- require "chdir_helpers"
5
4
require "env_helpers"
6
5
7
6
module ApplicationTests
8
7
module RakeTests
9
8
class RakeDbsTest < ActiveSupport ::TestCase
10
- include ActiveSupport ::Testing ::Isolation , ChdirHelpers , EnvHelpers
9
+ include ActiveSupport ::Testing ::Isolation , EnvHelpers
11
10
12
11
def setup
13
12
build_app
@@ -29,7 +28,7 @@ def set_database_url
29
28
end
30
29
31
30
def db_create_and_drop ( expected_database , environment_loaded : true )
32
- chdir ( app_path ) do
31
+ Dir . chdir ( app_path ) do
33
32
output = rails ( "db:create" )
34
33
assert_match ( /Created database/ , output )
35
34
assert File . exist? ( expected_database )
@@ -42,7 +41,7 @@ def db_create_and_drop(expected_database, environment_loaded: true)
42
41
end
43
42
44
43
def db_create_with_warning ( expected_database )
45
- chdir ( app_path ) do
44
+ Dir . chdir ( app_path ) do
46
45
output = rails ( "db:create" )
47
46
assert_match ( /Rails couldn't infer whether you are using multiple databases/ , output )
48
47
assert_match ( /Created database/ , output )
@@ -208,7 +207,7 @@ def db_create_with_warning(expected_database)
208
207
end
209
208
210
209
def with_database_existing
211
- chdir ( app_path ) do
210
+ Dir . chdir ( app_path ) do
212
211
set_database_url
213
212
rails "db:create"
214
213
yield
@@ -224,7 +223,7 @@ def with_database_existing
224
223
end
225
224
226
225
def with_bad_permissions
227
- chdir ( app_path ) do
226
+ Dir . chdir ( app_path ) do
228
227
skip "Can't avoid permissions as root" if Process . uid . zero?
229
228
230
229
set_database_url
@@ -272,7 +271,7 @@ def with_bad_permissions
272
271
end
273
272
274
273
test "db:truncate_all truncates all non-internal tables" do
275
- chdir ( app_path ) do
274
+ Dir . chdir ( app_path ) do
276
275
rails "generate" , "model" , "book" , "title:string"
277
276
rails "db:migrate"
278
277
require "#{ app_path } /config/environment"
@@ -297,7 +296,7 @@ def with_bad_permissions
297
296
298
297
test "db:truncate_all does not truncate any tables when environment is protected" do
299
298
with_rails_env "production" do
300
- chdir ( app_path ) do
299
+ Dir . chdir ( app_path ) do
301
300
rails "generate" , "model" , "book" , "title:string"
302
301
rails "db:migrate"
303
302
require "#{ app_path } /config/environment"
@@ -345,7 +344,7 @@ def db_migrate_and_status(expected_database)
345
344
end
346
345
347
346
def db_schema_dump
348
- chdir ( app_path ) do
347
+ Dir . chdir ( app_path ) do
349
348
args = [ "generate" , "model" , "book" , "title:string" ]
350
349
rails args
351
350
rails "db:migrate" , "db:schema:dump"
@@ -363,7 +362,7 @@ def db_schema_dump
363
362
end
364
363
365
364
def db_schema_cache_dump ( filename = "db/schema_cache.yml" )
366
- chdir ( app_path ) do
365
+ Dir . chdir ( app_path ) do
367
366
rails "db:schema:cache:dump"
368
367
369
368
cache_size = lambda { rails ( "runner" , "p ActiveRecord::Base.connection.schema_cache.size" ) . strip }
@@ -381,7 +380,7 @@ def db_schema_cache_dump(filename = "db/schema_cache.yml")
381
380
end
382
381
383
382
test "db:schema:cache:dump with custom filename" do
384
- chdir ( app_path ) do
383
+ Dir . chdir ( app_path ) do
385
384
File . open ( "#{ app_path } /config/database.yml" , "w" ) do |f |
386
385
f . puts <<-YAML
387
386
default: &default
@@ -414,7 +413,7 @@ def db_schema_cache_dump(filename = "db/schema_cache.yml")
414
413
end
415
414
416
415
test "db:schema:cache:dump first config wins" do
417
- chdir ( app_path ) do
416
+ Dir . chdir ( app_path ) do
418
417
File . open ( "#{ app_path } /config/database.yml" , "w" ) do |f |
419
418
f . puts <<-YAML
420
419
default: &default
@@ -440,7 +439,7 @@ def db_schema_cache_dump(filename = "db/schema_cache.yml")
440
439
end
441
440
442
441
def db_fixtures_load ( expected_database )
443
- chdir ( app_path ) do
442
+ Dir . chdir ( app_path ) do
444
443
rails "generate" , "model" , "book" , "title:string"
445
444
reload
446
445
rails "db:migrate" , "db:fixtures:load"
@@ -473,7 +472,7 @@ def db_fixtures_load(expected_database)
473
472
end
474
473
475
474
def db_structure_dump_and_load ( expected_database )
476
- chdir ( app_path ) do
475
+ Dir . chdir ( app_path ) do
477
476
rails "generate" , "model" , "book" , "title:string"
478
477
rails "db:migrate" , "db:structure:dump"
479
478
structure_dump = File . read ( "db/structure.sql" )
@@ -585,7 +584,7 @@ def db_structure_dump_and_load(expected_database)
585
584
end
586
585
587
586
def db_test_load_structure
588
- chdir ( app_path ) do
587
+ Dir . chdir ( app_path ) do
589
588
rails "generate" , "model" , "book" , "title:string"
590
589
rails "db:migrate" , "db:structure:dump" , "db:test:load_structure"
591
590
ActiveRecord ::Base . configurations = Rails . application . config . database_configuration
@@ -675,7 +674,7 @@ def db_test_load_structure
675
674
end
676
675
677
676
test "db:seed:replant truncates all non-internal tables and loads the seeds" do
678
- chdir ( app_path ) do
677
+ Dir . chdir ( app_path ) do
679
678
rails "generate" , "model" , "book" , "title:string"
680
679
rails "db:migrate"
681
680
require "#{ app_path } /config/environment"
@@ -708,7 +707,7 @@ def db_test_load_structure
708
707
709
708
test "db:seed:replant does not truncate any tables and does not load the seeds when environment is protected" do
710
709
with_rails_env "production" do
711
- chdir ( app_path ) do
710
+ Dir . chdir ( app_path ) do
712
711
rails "generate" , "model" , "book" , "title:string"
713
712
rails "db:migrate"
714
713
require "#{ app_path } /config/environment"
@@ -741,7 +740,7 @@ def db_test_load_structure
741
740
end
742
741
743
742
test "db:prepare setup the database" do
744
- chdir ( app_path ) do
743
+ Dir . chdir ( app_path ) do
745
744
rails "generate" , "model" , "book" , "title:string"
746
745
output = rails ( "db:prepare" )
747
746
assert_match ( /CreateBooks: migrated/ , output )
@@ -757,7 +756,7 @@ def db_test_load_structure
757
756
end
758
757
759
758
test "db:prepare does not touch schema when dumping is disabled" do
760
- chdir ( app_path ) do
759
+ Dir . chdir ( app_path ) do
761
760
rails "generate" , "model" , "book" , "title:string"
762
761
rails "db:create" , "db:migrate"
763
762
0 commit comments