Skip to content

Commit c88ca3b

Browse files
Merge pull request #47242 from skipkayhil/hm-rm-extra-engine-require
Remove unneeded require in plugin application.rb
2 parents f6cccfc + 1d241f0 commit c88ca3b

File tree

7 files changed

+75
-40
lines changed

7 files changed

+75
-40
lines changed

railties/lib/rails/generators/rails/plugin/plugin_generator.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ def generate_test_dummy(force = false)
124124
def test_dummy_config
125125
template "rails/boot.rb", "#{dummy_path}/config/boot.rb", force: true
126126

127-
insert_into_file "#{dummy_path}/config/application.rb", <<~RUBY, after: /^Bundler\.require.+\n/
128-
require #{namespaced_name.inspect}
129-
RUBY
130-
131127
if mountable?
132128
template "rails/routes.rb", "#{dummy_path}/config/routes.rb", force: true
133129
end

railties/test/engine/commands_test.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
require "abstract_unit"
44
require "console_helpers"
5+
require "plugin_helpers"
56

67
class Rails::Engine::CommandsTest < ActiveSupport::TestCase
78
include ConsoleHelpers
9+
include PluginHelpers
810

911
def setup
1012
@destination_root = Dir.mktmpdir("bukkits")
11-
Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --mountable` }
13+
generate_plugin("#{@destination_root}/bukkits", "--mountable")
1214
end
1315

1416
def teardown
@@ -17,14 +19,14 @@ def teardown
1719

1820
def test_help_command_work_inside_engine
1921
output = capture(:stderr) do
20-
Dir.chdir(plugin_path) { `bin/rails --help` }
22+
in_plugin_context(plugin_path) { `bin/rails --help` }
2123
end
2224
assert_no_match "NameError", output
2325
end
2426

2527
def test_runner_command_work_inside_engine
2628
output = capture(:stdout) do
27-
Dir.chdir(plugin_path) { system("bin/rails runner 'puts Rails.env'") }
29+
in_plugin_context(plugin_path) { system({ "RAILS_ENV" => "test" }, "bin/rails runner 'puts Rails.env'") }
2830
end
2931

3032
assert_equal "test", output.strip
@@ -63,10 +65,9 @@ def plugin_path
6365
end
6466

6567
def spawn_command(command, fd)
66-
Process.spawn(
67-
"#{plugin_path}/bin/rails #{command}",
68-
in: fd, out: fd, err: fd
69-
)
68+
in_plugin_context(plugin_path) do
69+
Process.spawn("bin/rails #{command}", in: fd, out: fd, err: fd)
70+
end
7071
end
7172

7273
def kill(pid)

railties/test/engine/test_test.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# frozen_string_literal: true
22

33
require "abstract_unit"
4+
require "plugin_helpers"
45

56
class Rails::Engine::TestTest < ActiveSupport::TestCase
7+
include PluginHelpers
8+
69
setup do
710
@destination_root = Dir.mktmpdir("bukkits")
8-
Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --mountable` }
11+
generate_plugin("#{@destination_root}/bukkits", "--mountable")
912
end
1013

1114
teardown do
1215
FileUtils.rm_rf(@destination_root)
1316
end
1417

1518
test "automatically synchronize test schema" do
16-
Dir.chdir(plugin_path) do
19+
in_plugin_context(plugin_path) do
1720
# In order to confirm that migration files are loaded, generate multiple migration files.
1821
`bin/rails generate model user name:string;
1922
bin/rails generate model todo name:string;

railties/test/generators/plugin_generator_test.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require "plugin_helpers"
34
require "generators/generators_test_helper"
45
require "rails/generators/rails/plugin/plugin_generator"
56
require "generators/shared_generator_tests"
@@ -22,6 +23,7 @@
2223
)
2324

2425
class PluginGeneratorTest < Rails::Generators::TestCase
26+
include PluginHelpers
2527
include GeneratorsTestHelper
2628
destination File.join(destination_root, "bukkits")
2729
arguments [destination_root]
@@ -282,10 +284,13 @@ def test_template_from_dir_pwd
282284

283285
def test_ensure_that_migration_tasks_work_with_mountable_option
284286
run_generator [destination_root, "--mountable"]
285-
FileUtils.cd destination_root
286-
quietly { system "bundle install" }
287-
output = `bin/rails db:migrate 2>&1`
288-
assert $?.success?, "Command failed: #{output}"
287+
prepare_plugin(destination_root)
288+
289+
in_plugin_context(destination_root) do
290+
quietly { system "bundle install" }
291+
output = `bin/rails db:migrate 2>&1`
292+
assert $?.success?, "Command failed: #{output}"
293+
end
289294
end
290295

291296
def test_creating_engine_in_full_mode
@@ -554,10 +559,14 @@ def test_creating_dummy_without_tests_but_with_dummy_path
554559
end
555560
end
556561

557-
def test_dummy_application_loads_plugin
562+
def test_plugin_passes_generated_test
558563
run_generator
564+
prepare_plugin(destination_root)
559565

560-
assert_file "test/dummy/config/application.rb", /^require "bukkits"/
566+
in_plugin_context(destination_root) do
567+
output = `bin/test 2>&1`
568+
assert $?.success?, "Command failed: #{output}"
569+
end
561570
end
562571

563572
def test_dummy_application_sets_include_all_helpers_to_false_for_mountable

railties/test/generators/scaffold_controller_generator_test.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require "plugin_helpers"
34
require "generators/generators_test_helper"
45
require "rails/generators/rails/scaffold_controller/scaffold_controller_generator"
56

@@ -9,6 +10,7 @@ module Generators
910
end
1011

1112
class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
13+
include PluginHelpers
1214
include GeneratorsTestHelper
1315
arguments %w(User name:string age:integer)
1416

@@ -270,23 +272,19 @@ def test_model_name_option
270272
end
271273

272274
def test_controller_tests_pass_by_default_inside_mountable_engine
273-
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --mountable` }
274-
275275
engine_path = File.join(destination_root, "bukkits")
276276

277-
Dir.chdir(engine_path) do
277+
with_new_plugin(engine_path, "--mountable") do
278278
quietly { `bin/rails g controller dashboard foo` }
279279
quietly { `bin/rails db:migrate RAILS_ENV=test` }
280280
assert_match(/2 runs, 2 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)
281281
end
282282
end
283283

284284
def test_controller_tests_pass_by_default_inside_full_engine
285-
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --full` }
286-
287285
engine_path = File.join(destination_root, "bukkits")
288286

289-
Dir.chdir(engine_path) do
287+
with_new_plugin(engine_path, "--full") do
290288
quietly { `bin/rails g controller dashboard foo` }
291289
quietly { `bin/rails db:migrate RAILS_ENV=test` }
292290
assert_match(/2 runs, 2 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)

railties/test/generators/scaffold_generator_test.rb

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# frozen_string_literal: true
22

3+
require "plugin_helpers"
34
require "generators/generators_test_helper"
45
require "rails/generators/rails/scaffold/scaffold_generator"
56

67
class ScaffoldGeneratorTest < Rails::Generators::TestCase
8+
include PluginHelpers
79
include GeneratorsTestHelper
810
arguments %w(product_line title:string approved:boolean product:belongs_to user:references)
911

@@ -565,11 +567,9 @@ def test_scaffold_generator_password_digest
565567
end
566568

567569
def test_scaffold_tests_pass_by_default_inside_mountable_engine
568-
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --mountable` }
569-
570570
engine_path = File.join(destination_root, "bukkits")
571571

572-
Dir.chdir(engine_path) do
572+
with_new_plugin(engine_path, "--mountable") do
573573
quietly do
574574
`bin/rails g scaffold User name:string age:integer;
575575
bin/rails db:migrate`
@@ -579,11 +579,9 @@ def test_scaffold_tests_pass_by_default_inside_mountable_engine
579579
end
580580

581581
def test_scaffold_tests_pass_by_default_inside_namespaced_mountable_engine
582-
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits-admin --mountable` }
583-
584582
engine_path = File.join(destination_root, "bukkits-admin")
585583

586-
Dir.chdir(engine_path) do
584+
with_new_plugin(engine_path, "--mountable") do
587585
quietly do
588586
`bin/rails g scaffold User name:string age:integer;
589587
bin/rails db:migrate`
@@ -599,11 +597,9 @@ def test_scaffold_tests_pass_by_default_inside_namespaced_mountable_engine
599597
end
600598

601599
def test_scaffold_tests_pass_by_default_inside_full_engine
602-
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --full` }
603-
604600
engine_path = File.join(destination_root, "bukkits")
605601

606-
Dir.chdir(engine_path) do
602+
with_new_plugin(engine_path, "--full") do
607603
quietly do
608604
`bin/rails g scaffold User name:string age:integer;
609605
bin/rails db:migrate`
@@ -613,11 +609,9 @@ def test_scaffold_tests_pass_by_default_inside_full_engine
613609
end
614610

615611
def test_scaffold_tests_pass_by_default_inside_api_mountable_engine
616-
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --mountable --api` }
617-
618612
engine_path = File.join(destination_root, "bukkits")
619613

620-
Dir.chdir(engine_path) do
614+
with_new_plugin(engine_path, "--mountable", "--api") do
621615
quietly do
622616
`bin/rails g scaffold User name:string age:integer;
623617
bin/rails db:migrate`
@@ -627,11 +621,9 @@ def test_scaffold_tests_pass_by_default_inside_api_mountable_engine
627621
end
628622

629623
def test_scaffold_tests_pass_by_default_inside_api_full_engine
630-
Dir.chdir(destination_root) { `bundle exec rails plugin new bukkits --full --api` }
631-
632624
engine_path = File.join(destination_root, "bukkits")
633625

634-
Dir.chdir(engine_path) do
626+
with_new_plugin(engine_path, "--full", "--api") do
635627
quietly do
636628
`bin/rails g scaffold User name:string age:integer;
637629
bin/rails db:migrate`

railties/test/plugin_helpers.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
module PluginHelpers
4+
def generate_plugin(plugin_path, *args)
5+
system(*%w[bundle exec rails plugin new], plugin_path, *args, out: File::NULL, exception: true)
6+
prepare_plugin(plugin_path)
7+
end
8+
9+
def prepare_plugin(plugin_path)
10+
# Fill placeholders in gemspec to prevent Gem::InvalidSpecificationException
11+
# from being raised. (Some fields require a valid URL, so use one for all.)
12+
gemspec_path = "#{plugin_path}/#{File.basename(plugin_path)}.gemspec"
13+
gemspec = File.read(gemspec_path).gsub(/"TODO.*"/, "http://example.com".inspect)
14+
File.write(gemspec_path, gemspec)
15+
16+
# Resolve `rails` gem to this repo so that Bundler doesn't search for a
17+
# version of Rails that hasn't been released yet.
18+
gemfile_path = "#{plugin_path}/Gemfile"
19+
gemfile = <<~RUBY
20+
#{File.read(gemfile_path).sub(/gem "rails".*/, "")}
21+
gem "rails", path: #{File.expand_path("../..", __dir__).inspect}
22+
RUBY
23+
File.write(gemfile_path, gemfile)
24+
end
25+
26+
def in_plugin_context(plugin_path, &block)
27+
# Run with `Bundler.with_unbundled_env` so that Bundler uses the plugin's
28+
# Gemfile instead of this repo's Gemfile.
29+
Dir.chdir(plugin_path) { Bundler.with_unbundled_env(&block) }
30+
end
31+
32+
def with_new_plugin(plugin_path, *args, &block)
33+
generate_plugin(plugin_path, *args)
34+
in_plugin_context(plugin_path, &block)
35+
end
36+
end

0 commit comments

Comments
 (0)