|
| 1 | +require "test_helper" |
| 2 | +require "rails/generators/rails/app/app_generator" |
| 3 | + |
| 4 | +class InstallerTest < ActiveSupport::TestCase |
| 5 | + include ActiveSupport::Testing::Isolation |
| 6 | + |
| 7 | + test "installer task" do |
| 8 | + with_new_rails_app do |
| 9 | + run_command("bin/rails", "importmap:install") |
| 10 | + |
| 11 | + assert_match %r{<%= javascript_importmap_tags %>.*</head>}m, File.read("app/views/layouts/application.html.erb") |
| 12 | + assert_match "// ", File.read("app/javascript/application.js") |
| 13 | + assert_equal 0, File.size("vendor/javascript/.keep") |
| 14 | + assert_equal File.read("#{__dir__}/../lib/install/config/importmap.rb"), File.read("config/importmap.rb") |
| 15 | + assert_equal File.read("#{__dir__}/../lib/install/bin/importmap"), File.read("bin/importmap") |
| 16 | + assert_equal 0700, File.stat("bin/importmap").mode & 0700 |
| 17 | + |
| 18 | + if defined?(Sprockets) |
| 19 | + manifest = File.read("app/assets/config/manifest.js") |
| 20 | + assert_match "//= link_tree ../../javascript .js", manifest |
| 21 | + assert_match "//= link_tree ../../../vendor/javascript .js", manifest |
| 22 | + end |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + test "installer task when no application layout" do |
| 27 | + with_new_rails_app do |
| 28 | + FileUtils.rm("app/views/layouts/application.html.erb") |
| 29 | + out, err = run_command("bin/rails", "importmap:install") |
| 30 | + assert_match "Add <%= javascript_importmap_tags %> within the <head> tag", out |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + private |
| 35 | + def with_new_rails_app |
| 36 | + # Unset testing dummy app so app generator doesn't get confused in Rails 6.1 and 7.0. |
| 37 | + Rails.app_class = nil |
| 38 | + Rails.application = nil |
| 39 | + |
| 40 | + Dir.mktmpdir do |tmpdir| |
| 41 | + app_dir = "#{tmpdir}/my_cool_app" |
| 42 | + |
| 43 | + Rails::Generators::AppGenerator.start([app_dir, "--quiet", "--skip-bundle", "--skip-bootsnap"]) |
| 44 | + |
| 45 | + Dir.chdir(app_dir) do |
| 46 | + gemfile = File.read("Gemfile") |
| 47 | + gemfile.gsub!(/^gem "importmap-rails".*/, "") |
| 48 | + gemfile << %(gem "importmap-rails", path: #{File.expand_path("..", __dir__).inspect}\n) |
| 49 | + File.write("Gemfile", gemfile) |
| 50 | + |
| 51 | + run_command("bundle", "install") |
| 52 | + |
| 53 | + yield |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + def run_command(*command) |
| 59 | + Bundler.with_unbundled_env do |
| 60 | + capture_subprocess_io { system(*command, exception: true) } |
| 61 | + end |
| 62 | + end |
| 63 | +end |
0 commit comments