Skip to content

Commit

Permalink
Merge pull request #44016 from jonathanhefner/fix-plugin-dummy-asset-…
Browse files Browse the repository at this point in the history
…pipeline

Fix asset pipeline errors for plugin dummy apps
  • Loading branch information
rafaelfranca committed Jan 5, 2022
2 parents aef6ad9 + 330becb commit 776b0ce
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 79 deletions.
Expand Up @@ -226,6 +226,10 @@ class PluginGenerator < AppBase # :nodoc:
def initialize(*args)
@dummy_path = nil
super

if !engine? || !with_dummy_app?
self.options = options.merge(skip_asset_pipeline: true).freeze
end
end

public_task :set_default_accessors!
Expand Down
39 changes: 12 additions & 27 deletions railties/test/generators/app_generator_test.rb
Expand Up @@ -944,26 +944,25 @@ def test_master_key_is_only_readable_by_the_owner
end

def test_minimal_rails_app
app_root = File.join(destination_root, "myapp")
run_generator [app_root, "--minimal"]
run_generator [destination_root, "--minimal"]

assert_no_file "#{app_root}/config/storage.yml"
assert_no_file "#{app_root}/config/cable.yml"
assert_no_file "#{app_root}/views/layouts/mailer.html.erb"
assert_no_file "#{app_root}/app/jobs/application.rb"
assert_file "#{app_root}/app/views/layouts/application.html.erb" do |content|
assert_no_file "config/storage.yml"
assert_no_file "config/cable.yml"
assert_no_file "views/layouts/mailer.html.erb"
assert_no_file "app/jobs/application.rb"
assert_file "app/views/layouts/application.html.erb" do |content|
assert_no_match(/data-turbo-track/, content)
end
assert_file "#{app_root}/config/environments/development.rb" do |content|
assert_file "config/environments/development.rb" do |content|
assert_no_match(/config\.active_storage/, content)
end
assert_file "#{app_root}/config/environments/production.rb" do |content|
assert_file "config/environments/production.rb" do |content|
assert_no_match(/config\.active_job/, content)
end
assert_file "#{app_root}/config/boot.rb" do |content|
assert_file "config/boot.rb" do |content|
assert_no_match(/require "bootsnap\/setup"/, content)
end
assert_file "#{app_root}/config/application.rb" do |content|
assert_file "config/application.rb" do |content|
assert_match(/#\s+require\s+["']active_job\/railtie["']/, content)
assert_match(/#\s+require\s+["']active_storage\/engine["']/, content)
assert_match(/#\s+require\s+["']action_mailer\/railtie["']/, content)
Expand All @@ -972,8 +971,8 @@ def test_minimal_rails_app
assert_match(/#\s+require\s+["']action_cable\/engine["']/, content)
end

assert_no_gem "jbuilder", app_root
assert_no_gem "web-console", app_root
assert_no_gem "jbuilder"
assert_no_gem "web-console"
end

private
Expand All @@ -985,18 +984,4 @@ def stub_rails_application(root = destination_root, &block)
def action(*args, &block)
capture(:stdout) { generator.send(*args, &block) }
end

def assert_gem(gem, constraint = nil, app_path = ".")
if constraint
assert_file File.join(app_path, "Gemfile"), /^\s*gem\s+["']#{gem}["'], #{constraint}$*/
else
assert_file File.join(app_path, "Gemfile"), /^\s*gem\s+["']#{gem}["']$*/
end
end

def assert_no_gem(gem, app_path = ".")
assert_file File.join(app_path, "Gemfile") do |content|
assert_no_match(gem, content)
end
end
end
88 changes: 41 additions & 47 deletions railties/test/generators/plugin_generator_test.rb
Expand Up @@ -269,20 +269,6 @@ def test_template_from_dir_pwd
assert_match(/It works from file!/, run_generator([destination_root, "-m", "lib/template.rb"]))
end

def test_ensure_that_tests_work
run_generator
FileUtils.cd destination_root
quietly { system "bundle install" }
assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bin/test 2>&1`)
end

def test_ensure_that_tests_works_in_full_mode
run_generator [destination_root, "--full", "--skip_active_record"]
FileUtils.cd destination_root
quietly { system "bundle install" }
assert_match(/1 runs, 1 assertions, 0 failures, 0 errors/, `bin/rails test 2>&1`)
end

def test_ensure_that_migration_tasks_work_with_mountable_option
run_generator [destination_root, "--mountable"]
FileUtils.cd destination_root
Expand All @@ -291,39 +277,6 @@ def test_ensure_that_migration_tasks_work_with_mountable_option
assert $?.success?, "Command failed: #{output}"
end

def test_ensure_that_sprokets_is_required_when_mountable
run_generator [destination_root, "--mountable"]
assert_file "Gemfile", /^gem "sprockets-rails"/
end

def test_ensure_that_sprokets_is_required_when_full
run_generator [destination_root, "--full"]
assert_file "Gemfile", /^gem "sprockets-rails"/
end

def test_ensure_that_sprokets_is_not_required_when_not_mountable_or_full
run_generator
assert_file "Gemfile" do |content|
assert_no_match(/sprockets-rails/, content)
end
end

def test_ensure_that_sprokets_is_not_required_when_assets_pipeline_is_skipped
run_generator [destination_root, "--skip-asset-pipeline", "--mountable"]

assert_file "Gemfile" do |contents|
assert_no_match(/sprockets-rails/, contents)
end
end

def test_ensure_that_sprokets_is_not_required_when_assets_pipeline_is_not_sprockets
run_generator [destination_root, "--asset-pipeline=propshaft", "--mountable"]

assert_file "Gemfile" do |contents|
assert_no_match(/sprockets-rails/, contents)
end
end

def test_creating_engine_in_full_mode
run_generator [destination_root, "--full"]
assert_file "app/assets/stylesheets/bukkits"
Expand Down Expand Up @@ -664,6 +617,47 @@ def test_skipping_test_files
end
end

def test_dummy_application_skips_asset_pipeline_when_simple_railtie
run_generator

assert_no_gem "sprockets-rails"
assert_no_file "test/dummy/config/initializers/assets.rb"
assert_file "test/dummy/config/environments/development.rb" do |content|
assert_no_match "config.assets", content
end
end

def test_dummy_application_configures_asset_pipeline_when_mountable
run_generator [destination_root, "--mountable"]

assert_gem "sprockets-rails"
assert_file "test/dummy/app/assets/config/manifest.js"
end

def test_dummy_application_configures_asset_pipeline_when_full
run_generator [destination_root, "--full"]

assert_gem "sprockets-rails"
assert_file "test/dummy/app/assets/config/manifest.js"
end

def test_dummy_application_skips_asset_pipeline_when_flag_skip_asset_pipeline
run_generator [destination_root, "--mountable", "--skip-asset-pipeline"]

assert_no_gem "sprockets-rails"
assert_no_file "test/dummy/config/initializers/assets.rb"
assert_file "test/dummy/config/environments/development.rb" do |content|
assert_no_match "config.assets", content
end
end

def test_no_asset_pipeline_gem_when_no_dummy_application
run_generator [destination_root, "--mountable", "--skip-test"]

assert_no_gem "sprockets-rails"
assert_no_directory "test/dummy"
end

def test_skipping_gemspec
run_generator [destination_root, "--skip-gemspec"]
assert_no_file "bukkits.gemspec"
Expand Down
11 changes: 11 additions & 0 deletions railties/test/generators/plugin_test_helper.rb
Expand Up @@ -23,4 +23,15 @@ def plugin_file(path, contents, mode: "w")
f.puts contents
end
end

def fill_in_gemspec_fields(gemspec_path = "#{plugin_path}/#{File.basename plugin_path}.gemspec")
# Some fields must be a valid URL.
filled_in = File.read(gemspec_path).gsub(/"TODO.*"/, "http://example.com".inspect)
File.write(gemspec_path, filled_in)
end

def resolve_rails_gem_to_repository(gemfile_path = "#{plugin_path}/Gemfile")
repository_path = File.expand_path("../../..", __dir__)
File.write(gemfile_path, "gem 'rails', path: #{repository_path.inspect}\n", mode: "a")
end
end
16 changes: 13 additions & 3 deletions railties/test/generators/plugin_test_runner_test.rb
@@ -1,20 +1,28 @@
# frozen_string_literal: true

require "generators/plugin_test_helper"
require "env_helpers"

class PluginTestRunnerTest < ActiveSupport::TestCase
include PluginTestHelper
include EnvHelpers

def setup
@destination_root = Dir.mktmpdir("bukkits")
Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --skip-bundle --webpack` }
Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --skip-bundle` }
fill_in_gemspec_fields
resolve_rails_gem_to_repository
plugin_file "test/dummy/db/schema.rb", ""
end

def teardown
FileUtils.rm_rf(@destination_root)
end

def test_run_default
assert_match "0 failures, 0 errors", run_test_command
end

def test_run_single_file
create_test_file "foo"
create_test_file "bar"
Expand Down Expand Up @@ -110,7 +118,9 @@ def plugin_path
"#{@destination_root}/bukkits"
end

def run_test_command(arguments)
Dir.chdir(plugin_path) { `bin/test #{arguments}` }
def run_test_command(arguments = "")
Dir.chdir(plugin_path) do
switch_env("BUNDLE_GEMFILE", "") { `bin/test #{arguments}` }
end
end
end
11 changes: 11 additions & 0 deletions railties/test/generators/shared_generator_tests.rb
Expand Up @@ -387,4 +387,15 @@ def run_generator_using_prerelease(args)
assert_equal gemfile[rails_gem_pattern], bundle_command_rails_gems[1]
end
end

def assert_gem(name, constraint = nil)
constraint_pattern = /, #{Regexp.escape constraint}/ if constraint
assert_file "Gemfile", %r/^\s*gem ["']#{name}["']#{constraint_pattern}/
end

def assert_no_gem(name)
assert_file "Gemfile" do |content|
assert_no_match %r/gem ["']#{name}["']/, content
end
end
end
14 changes: 12 additions & 2 deletions railties/test/generators/test_runner_in_engine_test.rb
@@ -1,20 +1,28 @@
# frozen_string_literal: true

require "generators/plugin_test_helper"
require "env_helpers"

class TestRunnerInEngineTest < ActiveSupport::TestCase
include PluginTestHelper
include EnvHelpers

def setup
@destination_root = Dir.mktmpdir("bukkits")
Dir.chdir(@destination_root) { `bundle exec rails plugin new bukkits --full --skip-bundle` }
fill_in_gemspec_fields
resolve_rails_gem_to_repository
plugin_file "test/dummy/db/schema.rb", ""
end

def teardown
FileUtils.rm_rf(@destination_root)
end

def test_run_default
assert_match "0 failures, 0 errors", run_test_command
end

def test_rerun_snippet_is_relative_path
create_test_file "post", pass: false

Expand All @@ -28,7 +36,9 @@ def plugin_path
"#{@destination_root}/bukkits"
end

def run_test_command(arguments)
Dir.chdir(plugin_path) { `bin/rails test #{arguments}` }
def run_test_command(arguments = "")
Dir.chdir(plugin_path) do
switch_env("BUNDLE_GEMFILE", "") { `bin/rails test #{arguments}` }
end
end
end

0 comments on commit 776b0ce

Please sign in to comment.