Skip to content

Commit 5dd8f54

Browse files
rafaelfrancazzak
authored andcommitted
Merge tests that are doing the same thing together
This will make sure we have less tests to run since those are slow. Generate the app once and run many assertions on it instead of generating the same app multiple times.
1 parent d350e04 commit 5dd8f54

File tree

3 files changed

+94
-108
lines changed

3 files changed

+94
-108
lines changed

railties/test/generators/app_generator_test.rb

Lines changed: 35 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,18 @@ def test_application_name_is_detected_if_it_exists_and_app_folder_renamed
168168
assert_file "#{app_moved_root}/config/environment.rb", /Rails\.application\.initialize!/
169169
end
170170

171-
def test_new_application_load_defaults
171+
def test_app_update
172172
run_generator
173-
assert_file "config/application.rb", /\s+config\.load_defaults #{Rails::VERSION::STRING.to_f}/
174-
end
175173

176-
def test_app_update_create_new_framework_defaults
177174
defaults_path = "config/initializers/new_framework_defaults_#{Rails::VERSION::MAJOR}_#{Rails::VERSION::MINOR}.rb"
178175

179-
run_generator
180176
assert_no_file defaults_path
177+
assert_no_file "config/initializers/cors.rb"
181178

182179
run_app_update
180+
183181
assert_file defaults_path
182+
assert_no_file "config/initializers/cors.rb"
184183
end
185184

186185
def test_app_update_supports_skip
@@ -211,13 +210,6 @@ def test_app_update_supports_pretend
211210
end
212211
end
213212

214-
def test_app_update_does_not_create_rack_cors
215-
run_generator
216-
run_app_update
217-
218-
assert_no_file "config/initializers/cors.rb"
219-
end
220-
221213
def test_app_update_does_not_remove_rack_cors_if_already_present
222214
run_generator
223215
FileUtils.touch("#{destination_root}/config/initializers/cors.rb")
@@ -331,23 +323,9 @@ def test_app_update_preserves_sprockets
331323
end
332324
end
333325

334-
def test_gem_for_active_storage
335-
run_generator
336-
assert_file "Gemfile", /^# gem "image_processing"/
337-
end
338-
339-
def test_gem_for_active_storage_when_skip_active_storage_is_given
340-
run_generator [destination_root, "--skip-active-storage"]
341-
342-
assert_no_gem "image_processing"
343-
344-
assert_file "Dockerfile" do |content|
345-
assert_no_match(/libvips/, content)
346-
end
347-
end
348-
349326
def test_app_update_does_not_generate_active_storage_contents_when_skip_active_storage_is_given
350327
run_generator [destination_root, "--skip-active-storage"]
328+
351329
run_app_update
352330

353331
assert_file "config/environments/development.rb" do |content|
@@ -389,31 +367,11 @@ def test_generator_skips_action_mailbox_when_skip_action_mailbox_is_given
389367
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_mailbox\/engine["']/
390368
end
391369

392-
def test_generator_skips_action_mailbox_when_skip_active_record_is_given
393-
run_generator [destination_root, "--skip-active-record"]
394-
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_mailbox\/engine["']/
395-
end
396-
397-
def test_generator_skips_action_mailbox_when_skip_active_storage_is_given
398-
run_generator [destination_root, "--skip-active-storage"]
399-
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_mailbox\/engine["']/
400-
end
401-
402370
def test_generator_skips_action_text_when_skip_action_text_is_given
403371
run_generator [destination_root, "--skip-action-text"]
404372
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_text\/engine["']/
405373
end
406374

407-
def test_generator_skips_action_text_when_skip_active_record_is_given
408-
run_generator [destination_root, "--skip-active-record"]
409-
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_text\/engine["']/
410-
end
411-
412-
def test_generator_skips_action_text_when_skip_active_storage_is_given
413-
run_generator [destination_root, "--skip-active-storage"]
414-
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_text\/engine["']/
415-
end
416-
417375
def test_app_update_does_not_change_config_target_version
418376
run_generator
419377

@@ -798,14 +756,6 @@ def test_bundler_binstub
798756
assert_equal 1, @bundle_commands.count("binstubs bundler")
799757
end
800758

801-
def test_skip_active_record_option
802-
run_generator [destination_root, "--skip-active-record"]
803-
804-
assert_file ".gitattributes" do |content|
805-
assert_no_match(/schema.rb/, content)
806-
end
807-
end
808-
809759
def test_skip_active_job_option
810760
run_generator [destination_root, "--skip-active-job"]
811761

@@ -1483,6 +1433,36 @@ def test_no_devcontainer_by_default
14831433
end
14841434

14851435
private
1436+
def assert_load_defaults
1437+
assert_file "config/application.rb", /\s+config\.load_defaults #{Rails::VERSION::STRING.to_f}/
1438+
end
1439+
1440+
def assert_gem_for_active_storage
1441+
assert_file "Gemfile", /^# gem "image_processing"/
1442+
end
1443+
1444+
def assert_frameworks_are_not_required_when_active_storage_is_skipped
1445+
super
1446+
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_mailbox\/engine["']/
1447+
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']action_text\/engine["']/
1448+
end
1449+
1450+
def assert_dockerfile_when_active_storage_is_skipped
1451+
assert_file "Dockerfile" do |content|
1452+
assert_no_match(/libvips/, content)
1453+
end
1454+
end
1455+
1456+
def assert_gems_when_active_storage_is_skipped
1457+
assert_no_gem "image_processing"
1458+
end
1459+
1460+
def assert_gitattributes_does_not_have_schema_file
1461+
assert_file ".gitattributes" do |content|
1462+
assert_no_match(/schema.rb/, content)
1463+
end
1464+
end
1465+
14861466
def assert_node_files
14871467
assert_file ".node-version" do |content|
14881468
assert_match %r/\d+\.\d+\.\d+/, content

railties/test/generators/plugin_generator_test.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,6 @@ def test_custom_database_dependency
247247
assert_file "Gemfile", /mysql/
248248
end
249249

250-
def test_skip_database_dependency
251-
run_generator [destination_root, "--skip-active-record"]
252-
assert_file "Gemfile" do |contents|
253-
assert_no_match(/sqlite/, contents)
254-
end
255-
end
256-
257250
def test_ensure_that_skip_active_record_option_is_passed_to_app_generator
258251
run_generator [destination_root, "--skip_active_record"]
259252
assert_file "test/test_helper.rb" do |contents|

railties/test/generators/shared_generator_tests.rb

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_implied_options_with_conflicting_option
5050
assert_match %r/conflicting option/i, error.message
5151
end
5252

53-
def test_skeleton_is_created
53+
def test_codebase_is_created
5454
run_generator
5555

5656
generated_files_and_folders = []
@@ -78,6 +78,25 @@ def test_skeleton_is_created
7878
assert_equal generated_files_and_folders.sort, default_files, "The expected list of generated files is not alphabetical"
7979

8080
default_files.each { |path| assert_file path }
81+
82+
assert_file "#{application_path}/config/application.rb", /\s+require\s+["']rails\/all["']/
83+
84+
assert_file "#{application_path}/config/environments/development.rb" do |content|
85+
assert_match(/config\.action_mailer\.raise_delivery_errors = false/, content)
86+
assert_match(/config\.active_storage/, content)
87+
end
88+
assert_file "#{application_path}/config/environments/test.rb" do |content|
89+
assert_match(/config\.action_mailer\.delivery_method = :test/, content)
90+
assert_match(/config\.active_storage/, content)
91+
end
92+
assert_file "#{application_path}/config/environments/production.rb" do |content|
93+
assert_match(/# config\.action_mailer\.raise_delivery_errors = false/, content)
94+
assert_match(/^ # config\.require_master_key = true/, content)
95+
assert_match(/config\.active_storage/, content)
96+
end
97+
98+
assert_load_defaults
99+
assert_gem_for_active_storage
81100
end
82101

83102
def test_plugin_new_generate_pretend
@@ -223,59 +242,25 @@ def test_default_frameworks_are_required_when_others_are_removed
223242
assert_file "#{application_path}/config/application.rb", /^require\s+["']rails\/test_unit\/railtie["']/
224243
end
225244

226-
def test_generator_without_skips
227-
run_generator
228-
assert_file "#{application_path}/config/application.rb", /\s+require\s+["']rails\/all["']/
229-
assert_file "#{application_path}/config/environments/development.rb" do |content|
230-
assert_match(/config\.action_mailer\.raise_delivery_errors = false/, content)
231-
end
232-
assert_file "#{application_path}/config/environments/test.rb" do |content|
233-
assert_match(/config\.action_mailer\.delivery_method = :test/, content)
234-
end
235-
assert_file "#{application_path}/config/environments/production.rb" do |content|
236-
assert_match(/# config\.action_mailer\.raise_delivery_errors = false/, content)
237-
assert_match(/^ # config\.require_master_key = true/, content)
238-
end
239-
end
240-
241245
def test_generator_if_skip_active_record_is_given
242246
run_generator [destination_root, "--skip-active-record"]
243247
assert_no_directory "#{application_path}/db/"
244248
assert_no_file "#{application_path}/config/database.yml"
245249
assert_no_file "#{application_path}/app/models/application_record.rb"
246-
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_record\/railtie["']/
250+
251+
assert_file "#{application_path}/config/application.rb" do |content|
252+
assert_match(/#\s+require\s+["']active_record\/railtie["']/, content)
253+
assert_match(/#\s+require\s+["']active_storage\/engine["']/, content)
254+
assert_match(/#\s+require\s+["']action_mailbox\/engine["']/, content)
255+
assert_match(/#\s+require\s+["']action_text\/engine["']/, content)
256+
end
257+
247258
assert_file "test/test_helper.rb" do |helper_content|
248259
assert_no_match(/fixtures :all/, helper_content)
249260
end
250261
assert_file "#{application_path}/bin/setup" do |setup_content|
251262
assert_no_match(/db:prepare/, setup_content)
252263
end
253-
end
254-
255-
def test_generator_for_active_storage
256-
run_generator([destination_root])
257-
258-
assert_file "#{application_path}/config/environments/development.rb" do |content|
259-
assert_match(/config\.active_storage/, content)
260-
end
261-
262-
assert_file "#{application_path}/config/environments/production.rb" do |content|
263-
assert_match(/config\.active_storage/, content)
264-
end
265-
266-
assert_file "#{application_path}/config/environments/test.rb" do |content|
267-
assert_match(/config\.active_storage/, content)
268-
end
269-
270-
assert_file "#{application_path}/config/storage.yml"
271-
assert_directory "#{application_path}/storage"
272-
assert_directory "#{application_path}/tmp/storage"
273-
end
274-
275-
def test_generator_if_skip_active_storage_is_given
276-
run_generator [destination_root, "--skip-active-storage"]
277-
278-
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_storage\/engine["']/
279264

280265
assert_file "#{application_path}/config/environments/development.rb" do |content|
281266
assert_no_match(/config\.active_storage/, content)
@@ -290,12 +275,18 @@ def test_generator_if_skip_active_storage_is_given
290275
end
291276

292277
assert_no_file "#{application_path}/config/storage.yml"
278+
279+
assert_gitattributes_does_not_have_schema_file
280+
281+
assert_file "Gemfile" do |contents|
282+
assert_no_match(/sqlite/, contents)
283+
end
293284
end
294285

295-
def test_generator_does_not_generate_active_storage_contents_if_skip_active_record_is_given
296-
run_generator [destination_root, "--skip-active-record"]
286+
def test_generator_if_skip_active_storage_is_given
287+
run_generator [destination_root, "--skip-active-storage"]
297288

298-
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_storage\/engine["']/
289+
assert_frameworks_are_not_required_when_active_storage_is_skipped
299290

300291
assert_file "#{application_path}/config/environments/development.rb" do |content|
301292
assert_no_match(/config\.active_storage/, content)
@@ -310,6 +301,9 @@ def test_generator_does_not_generate_active_storage_contents_if_skip_active_reco
310301
end
311302

312303
assert_no_file "#{application_path}/config/storage.yml"
304+
305+
assert_gems_when_active_storage_is_skipped
306+
assert_dockerfile_when_active_storage_is_skipped
313307
end
314308

315309
def test_generator_does_not_create_storage_dir_if_skip_active_storage_is_given_and_not_using_sqlite
@@ -405,6 +399,25 @@ def test_target_rails_prerelease_with_relative_app_path
405399
end
406400

407401
private
402+
def assert_load_defaults
403+
end
404+
405+
def assert_gem_for_active_storage
406+
end
407+
408+
def assert_frameworks_are_not_required_when_active_storage_is_skipped
409+
assert_file "#{application_path}/config/application.rb", /#\s+require\s+["']active_storage\/engine["']/
410+
end
411+
412+
def assert_gems_when_active_storage_is_skipped
413+
end
414+
415+
def assert_dockerfile_when_active_storage_is_skipped
416+
end
417+
418+
def assert_gitattributes_does_not_have_schema_file
419+
end
420+
408421
def fixtures_root
409422
File.expand_path("../fixtures", __dir__)
410423
end

0 commit comments

Comments
 (0)