Skip to content

Commit

Permalink
Merge pull request #239 from calebowens/fix-rakefile-loading-twice
Browse files Browse the repository at this point in the history
Invoke app:template via Rake::Task[].invoke to avoid reloading rakefile
  • Loading branch information
rafaelfranca authored Apr 19, 2024
2 parents 2ecde30 + 6e4b036 commit 61054a1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/tasks/importmap_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
namespace :importmap do
desc "Setup Importmap for the app"
task :install do
system RbConfig.ruby, "./bin/rails", "app:template", "LOCATION=#{File.expand_path("../install/install.rb", __dir__)}"
previous_location = ENV["LOCATION"]
ENV["LOCATION"] = File.expand_path("../install/install.rb", __dir__)
Rake::Task["app:template"].invoke
ENV["LOCATION"] = previous_location
end
end
14 changes: 13 additions & 1 deletion test/installer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ class InstallerTest < ActiveSupport::TestCase
end
end

test "doesn't load rakefile twice" do
with_new_rails_app do |app_dir|
rakefile = File.read("#{app_dir}/Rakefile")
rakefile = "puts \"I've been logged twice!\" \n" + rakefile
File.write("#{app_dir}/Rakefile", rakefile)

out, err = run_command("bin/rails", "importmap:install")

assert_equal 1, out.scan(/I've been logged twice!/).size
end
end

private
def with_new_rails_app
# Unset testing dummy app so app generator doesn't get confused in Rails 6.1 and 7.0.
Expand All @@ -54,7 +66,7 @@ def with_new_rails_app

run_command("bundle", "install")

yield
yield(app_dir)
end
end
end
Expand Down

0 comments on commit 61054a1

Please sign in to comment.