Skip to content

Commit 1d8d8e1

Browse files
committed
Stub rails_command to prevent app:template command from being run
When rails#48269 was merged any gem installed during `rails new` which calls `app:template` would cause the install command to be executed and consequently `bundle install` would also run. We want to avoid running these commands in our tests because they are very expensive. It is up to the gem (importmap, etc) to test the behavior of the install command, not railties. Before ``` $ bin/test test/generators/plugin_generator_test.rb test/generators/app_generator_test.rb Finished in 320.803659s, 0.8541 runs/s, 7.1913 assertions/s. 274 runs, 2307 assertions, 14 failures, 0 errors, 0 skips ``` After ``` Finished in 70.316250s, 3.9251 runs/s, 34.3164 assertions/s. 276 runs, 2413 assertions, 0 failures, 0 errors, 0 skips ```
1 parent 9283b9e commit 1d8d8e1

File tree

2 files changed

+65
-82
lines changed

2 files changed

+65
-82
lines changed

railties/test/generators/app_generator_test.rb

Lines changed: 59 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -660,41 +660,54 @@ def test_ci_files_are_skipped_if_required
660660
end
661661

662662
def test_inclusion_of_kamal_files
663-
run_generator_and_bundler [destination_root]
663+
generator [destination_root]
664+
run_generator_instance
664665

665666
assert_file "config/deploy.yml"
666667
assert_file ".env.erb"
667668
end
668669

669670
def test_kamal_files_are_skipped_if_required
670-
run_generator_and_bundler [destination_root, "--skip-kamal"]
671+
generator [destination_root], ["--skip-kamal"]
672+
run_generator_instance
673+
674+
assert_empty @bundle_commands.grep("binstubs kamal")
675+
assert_empty @bundle_commands.grep("exec kamal init")
671676

672677
assert_no_file "config/deploy.yml"
673678
assert_no_file ".env.erb"
674679
end
675680

676681
def test_inclusion_of_kamal_storage_volume
677-
run_generator_and_bundler [destination_root]
682+
generator [destination_root]
683+
run_generator_instance
684+
685+
assert_equal 1, @bundle_commands.count("binstubs kamal")
686+
assert_equal 1, @bundle_commands.count("exec kamal init")
678687

679688
assert_file "config/deploy.yml" do |content|
680689
assert_match(%r{storage:/rails/storage}, content)
681690
end
682691
end
683692

684693
def test_inclusion_of_kamal_storage_volume_if_only_skip_active_storage_is_given
685-
run_generator_and_bundler [destination_root, "--skip-active-storage"]
694+
generator [destination_root], ["--skip-active-storage"]
695+
run_generator_instance
696+
697+
assert_equal 1, @bundle_commands.count("binstubs kamal")
698+
assert_equal 1, @bundle_commands.count("exec kamal init")
686699

687700
assert_file "config/deploy.yml" do |content|
688701
assert_match(%r{storage:/rails/storage}, content)
689702
end
690703
end
691704

692705
def test_kamal_storage_volume_is_skipped_if_required
693-
run_generator_and_bundler [
694-
destination_root,
695-
"--skip-active-storage",
696-
"--database=postgresql"
697-
]
706+
generator [destination_root], ["--skip-active-storage", "--database=postgresql"]
707+
run_generator_instance
708+
709+
assert_equal 1, @bundle_commands.count("binstubs kamal")
710+
assert_equal 1, @bundle_commands.count("exec kamal init")
698711

699712
assert_file "config/deploy.yml" do |content|
700713
assert_no_match(%r{storage:/rails/storage}, content)
@@ -814,15 +827,10 @@ def test_skip_active_job_option
814827
def test_skip_javascript_option
815828
generator([destination_root], skip_javascript: true)
816829

817-
command_check = -> command, *_ do
818-
if command == "importmap:install"
819-
flunk "`importmap:install` expected to not be called."
820-
end
821-
end
830+
run_generator_instance
822831

823-
generator.stub(:rails_command, command_check) do
824-
run_generator_instance
825-
end
832+
assert_not_includes @rails_commands, "importmap:install", "`importmap:install` expected to not be called."
833+
assert_not_includes @rails_commands, "turbo:install stimulus:install", "`turbo:install stimulus:install` expected to not be called."
826834

827835
assert_no_gem "importmap-rails"
828836
assert_no_gem "jsbundling-rails"
@@ -840,39 +848,19 @@ def test_skip_javascript_option
840848
def test_webpack_option
841849
generator([destination_root], javascript: "webpack")
842850

843-
webpack_called = 0
844-
command_check = -> command, *_ do
845-
case command
846-
when "javascript:install:webpack"
847-
webpack_called += 1
848-
end
849-
end
850-
851-
generator.stub(:rails_command, command_check) do
852-
run_generator_instance
853-
end
851+
run_generator_instance
854852

855-
assert_equal 1, webpack_called, "`javascript:install:webpack` expected to be called once, but was called #{webpack_called} times."
853+
assert_includes @rails_commands, "javascript:install:webpack", "`javascript:install:webpack` expected to be called, but wasn't."
856854
assert_gem "jsbundling-rails"
857855
assert_node_files
858856
end
859857

860858
def test_esbuild_option
861859
generator([destination_root], javascript: "esbuild")
862860

863-
esbuild_called = 0
864-
command_check = -> command, *_ do
865-
case command
866-
when "javascript:install:esbuild"
867-
esbuild_called += 1
868-
end
869-
end
870-
871-
generator.stub(:rails_command, command_check) do
872-
run_generator_instance
873-
end
861+
run_generator_instance
874862

875-
assert_equal 1, esbuild_called, "`javascript:install:esbuild` expected to be called once, but was called #{esbuild_called} times."
863+
assert_includes @rails_commands, "javascript:install:esbuild", "`javascript:install:esbuild` expected to be called, but wasn't."
876864
assert_gem "jsbundling-rails"
877865
assert_node_files
878866
end
@@ -895,19 +883,9 @@ def test_esbuild_option_with_js_argument
895883
def test_bun_option
896884
generator([destination_root], javascript: "bun")
897885

898-
bun_called = 0
899-
command_check = -> command, *_ do
900-
case command
901-
when "javascript:install:bun"
902-
bun_called += 1
903-
end
904-
end
905-
906-
generator.stub(:rails_command, command_check) do
907-
run_generator_instance
908-
end
886+
run_generator_instance
909887

910-
assert_equal 1, bun_called, "`javascript:install:bun` expected to be called once, but was called #{bun_called} times."
888+
assert_includes @rails_commands, "javascript:install:bun", "`javascript:install:bun` expected to be called, but wasn't."
911889
assert_gem "jsbundling-rails"
912890
end
913891

@@ -948,20 +926,20 @@ def test_skip_javascript_option_with_skip_js_argument
948926
end
949927

950928
def test_hotwire
951-
run_generator_and_bundler [destination_root]
929+
generator [destination_root]
930+
run_generator_instance
931+
932+
assert_includes @rails_commands, "turbo:install stimulus:install", "`turbo:install stimulus:install` expected to be called, but wasn't."
952933
assert_gem "turbo-rails"
953934
assert_gem "stimulus-rails"
954935
assert_file "app/views/layouts/application.html.erb" do |content|
955936
assert_match(/data-turbo-track/, content)
956937
end
957-
assert_file "app/javascript/application.js" do |content|
958-
assert_match(/turbo/, content)
959-
assert_match(/controllers/, content)
960-
end
961938
end
962939

963940
def test_skip_hotwire
964-
run_generator [destination_root, "--skip-hotwire"]
941+
generator [destination_root], ["--skip-hotwire"]
942+
run_generator_instance
965943

966944
assert_no_gem "turbo-rails"
967945
assert_file "app/views/layouts/application.html.erb" do |content|
@@ -971,11 +949,11 @@ def test_skip_hotwire
971949
end
972950

973951
def test_css_option_with_asset_pipeline_tailwind
974-
run_generator_and_bundler [destination_root, "--asset-pipeline=sprockets", "--css=tailwind"]
952+
generator [destination_root], ["--css=tailwind", "--asset-pipeline=sprockets"]
953+
run_generator_instance
954+
955+
assert_includes @rails_commands, "tailwindcss:install", "`tailwindcss:install` expected to be called, but wasn't."
975956
assert_gem "tailwindcss-rails"
976-
assert_file "app/views/layouts/application.html.erb" do |content|
977-
assert_match(/tailwind/, content)
978-
end
979957
assert_no_node_files
980958
end
981959

@@ -986,9 +964,11 @@ def test_css_option_with_tailwind_uses_cssbundling_gem_when_using_node
986964
end
987965

988966
def test_css_option_with_asset_pipeline_sass
989-
run_generator_and_bundler [destination_root, "--asset-pipeline=sprockets", "--css=sass"]
967+
generator [destination_root], ["--css=sass", "--asset-pipeline=sprockets"]
968+
run_generator_instance
969+
970+
assert_includes @rails_commands, "dartsass:install", "`dartsass:install` expected to be called, but wasn't."
990971
assert_gem "dartsass-rails"
991-
assert_file "app/assets/stylesheets/application.scss"
992972
assert_no_node_files
993973
end
994974

@@ -999,9 +979,11 @@ def test_css_option_with_sass_uses_cssbundling_gem_when_using_node
999979
end
1000980

1001981
def test_css_option_with_cssbundling_gem
1002-
run_generator_and_bundler [destination_root, "--asset-pipeline=sprockets", "--css=postcss"]
982+
generator [destination_root], ["--css=postcss", "--asset-pipeline=sprockets"]
983+
run_generator_instance
984+
985+
assert_includes @rails_commands, "css:install:postcss", "`css:install:postcss` expected to be called, but wasn't."
1003986
assert_gem "cssbundling-rails"
1004-
assert_file "app/assets/stylesheets/application.postcss.css"
1005987
assert_node_files
1006988
end
1007989

@@ -1011,6 +993,16 @@ def test_css_option_with_cssbundling_gem_does_not_force_jsbundling_gem
1011993
assert_gem "importmap-rails"
1012994
end
1013995

996+
def test_default_generator_executes_all_rails_commands
997+
generator [destination_root]
998+
run_generator_instance
999+
1000+
expected_commands = [
1001+
"credentials:diff --enroll", "importmap:install", "turbo:install stimulus:install"
1002+
]
1003+
assert_equal expected_commands, @rails_commands
1004+
end
1005+
10141006
def test_skip_dev_gems
10151007
run_generator [destination_root, "--skip-dev-gems"]
10161008
assert_no_gem "web-console"
@@ -1519,20 +1511,6 @@ def assert_no_node_files
15191511
end
15201512
end
15211513

1522-
def run_generator_and_bundler(args)
1523-
option_args, positional_args = args.partition { |arg| arg.start_with?("--") }
1524-
option_args << "--no-skip-bundle"
1525-
generator(positional_args, option_args)
1526-
1527-
# Stub `rails_gemfile_entry` so that Bundler resolves `gem "rails"` to the
1528-
# current repository instead of searching for an invalid version number
1529-
# (for a version that hasn't been released yet).
1530-
rails_gemfile_entry = Rails::Generators::AppBase::GemfileEntry.path("rails", Rails::Generators::RAILS_DEV_PATH)
1531-
generator.stub(:rails_gemfile_entry, -> { rails_gemfile_entry }) do
1532-
quietly { run_generator_instance }
1533-
end
1534-
end
1535-
15361514
def run_app_update(app_root = destination_root, flags: "--force")
15371515
Dir.chdir(app_root) do
15381516
gemfile_contents = File.read("Gemfile")

railties/test/generators/shared_generator_tests.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,13 @@ def run_generator_instance
413413
@bundle_commands = []
414414
@bundle_command_stub ||= -> (command, *) { @bundle_commands << command }
415415

416+
@rails_commands = []
417+
@rails_command_stub ||= -> (command, *_) { @rails_commands << command }
418+
416419
generator.stub(:bundle_command, @bundle_command_stub) do
417-
super
420+
generator.stub(:rails_command, @rails_command_stub) do
421+
super
422+
end
418423
end
419424
end
420425

0 commit comments

Comments
 (0)