Skip to content

Commit

Permalink
Test installer
Browse files Browse the repository at this point in the history
This commit adds test coverage for the installer Rake task and application template. The installer is run against a freshly generated Rails app using the version of Rails that is currently loaded. Thus the installer can be tested with different versions of Rails in CI.

Similar to hotwired/stimulus-rails#136.

The motivation is to be able to move these tests from railties, see: rails/rails#49679

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
  • Loading branch information
zzak and jonathanhefner committed Nov 4, 2024
1 parent 59e1f48 commit bac8965
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
- name: Run Bug Template Tests
run: ruby bug_report_template.rb || ruby bug_report_template.rb

- uses: oven-sh/setup-bun@v1
- name: Run tests
id: test
run: bundle exec rake TESTOPT=-vdc
Expand Down
44 changes: 44 additions & 0 deletions test/app_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "rails"
require "rails/test_help"
require "fileutils"

module RailsAppHelpers
private
def create_new_rails_app(app_dir, options=[])
require "rails/generators/rails/app/app_generator"
Rails::Generators::AppGenerator.start([app_dir, *options, "--skip-bundle", "--skip-bootsnap", "--quiet"])

Dir.chdir(app_dir) do
gemfile = File.read("Gemfile")

gemfile.gsub!(/^gem ["']turbo-rails["'].*/, "")
gemfile << %(gem "turbo-rails", path: #{File.expand_path("..", __dir__).inspect}\n)

if Rails::VERSION::PRE == "alpha"
gemfile.gsub!(/^gem ["']rails["'].*/, "")
gemfile << %(gem "rails", path: #{Gem.loaded_specs["rails"].full_gem_path.inspect}\n)
end

File.write("Gemfile", gemfile)

run_command("bundle", "install")
end
end

def with_new_rails_app(options=[], &block)
require "digest/sha1"
variant = [RUBY_VERSION, Gem.loaded_specs["rails"].full_gem_path,]
app_name = "turbo_test_app_#{Digest::SHA1.hexdigest(variant.to_s)}"

Dir.mktmpdir do |tmpdir|
create_new_rails_app("#{tmpdir}/#{app_name}", *options)
Dir.chdir("#{tmpdir}/#{app_name}", &block)
end
end

def run_command(*command)
Bundler.with_unbundled_env do
capture_subprocess_io { system(*command, exception: true) }
end
end
end
57 changes: 57 additions & 0 deletions test/installer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'app_helper'

class InstallerTest < ActiveSupport::TestCase
include RailsAppHelpers
include ActiveSupport::Testing::Isolation

test "installer" do
with_new_rails_app do
if Rails::VERSION::MAJOR >= 7 && File.read("Gemfile").match?(/importmap-rails/)
run_command("bin/rails", "importmap:install")
end
run_command("bin/rails", "turbo:install")

assert_match %(import "@hotwired/turbo-rails"\n), File.read("app/javascript/application.js")

if Rails::VERSION::MAJOR >= 7
assert_match %(pin "@hotwired/turbo-rails", to: "turbo.min.js"), File.read("config/importmap.rb")
else
assert_match "@hotwired/turbo-rails", File.read("package.json")
assert_match "@hotwired/turbo-rails", File.read("yarn.lock")
end
end
end

test "installer with no javascript" do
with_new_rails_app %w[--skip-javascript] do
out, = run_command("bin/rails", "turbo:install")

assert_match "You must either be running with node (package.json) or importmap-rails (config/importmap.rb) to use this gem.", out
end
end

test "installer with pre-existing application.js" do
with_new_rails_app do
if Rails::VERSION::MAJOR >= 7 && File.read("Gemfile").match?(/importmap-rails/)
run_command("bin/rails", "importmap:install")
end
File.write("app/javascript/application.js", "// pre-existing")
run_command("bin/rails", "turbo:install")

assert_match "// pre-existing", File.read("app/javascript/application.js")
end
end

if Gem::Version.new(Rails.version) >= Gem::Version.new("7.1")
test "installer with bun" do
with_new_rails_app %w[--javascript=bun] do
run_command("bin/rails", "javascript:install:bun")
run_command("bin/rails", "turbo:install")

assert_match %(import "@hotwired/turbo-rails"\n), File.read("app/javascript/application.js")

assert_match "@hotwired/turbo-rails", File.read("package.json")
end
end
end
end

0 comments on commit bac8965

Please # to comment.