Skip to content

Commit d229a54

Browse files
zzakjonathanhefner
andcommitted
Test installer
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 <[email protected]>
1 parent 102a491 commit d229a54

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
rubygems: latest
3030
bundler-cache: true
3131

32+
- uses: oven-sh/setup-bun@v1
3233
- name: Run tests
3334
id: test
3435
run: bundle exec rake TESTOPT=-vdc

test/app_helper.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require "rails"
2+
require "rails/test_help"
3+
require "fileutils"
4+
5+
module RailsAppHelpers
6+
private
7+
def create_new_rails_app(app_dir, options=[])
8+
require "rails/generators/rails/app/app_generator"
9+
Rails::Generators::AppGenerator.start([app_dir, *options, "--skip-bundle", "--skip-bootsnap", "--quiet"])
10+
11+
Dir.chdir(app_dir) do
12+
gemfile = File.read("Gemfile")
13+
14+
gemfile.gsub!(/^gem ["']turbo-rails["'].*/, "")
15+
gemfile << %(gem "turbo-rails", path: #{File.expand_path("..", __dir__).inspect}\n)
16+
17+
if Rails::VERSION::PRE == "alpha"
18+
gemfile.gsub!(/^gem ["']rails["'].*/, "")
19+
gemfile << %(gem "rails", path: #{Gem.loaded_specs["rails"].full_gem_path.inspect}\n)
20+
end
21+
22+
File.write("Gemfile", gemfile)
23+
24+
run_command("bundle", "install")
25+
end
26+
end
27+
28+
def with_new_rails_app(options=[], &block)
29+
require "digest/sha1"
30+
variant = [RUBY_VERSION, Gem.loaded_specs["rails"].full_gem_path,]
31+
app_name = "turbo_test_app_#{Digest::SHA1.hexdigest(variant.to_s)}"
32+
33+
Dir.mktmpdir do |tmpdir|
34+
create_new_rails_app("#{tmpdir}/#{app_name}", *options)
35+
Dir.chdir("#{tmpdir}/#{app_name}", &block)
36+
end
37+
end
38+
39+
def run_command(*command)
40+
Bundler.with_unbundled_env do
41+
capture_subprocess_io { system(*command, exception: true) }
42+
end
43+
end
44+
end

test/installer_test.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'app_helper'
2+
3+
class InstallerTest < ActiveSupport::TestCase
4+
include RailsAppHelpers
5+
include ActiveSupport::Testing::Isolation
6+
7+
test "installer" do
8+
with_new_rails_app do
9+
if Rails::VERSION::MAJOR >= 7 && File.read("Gemfile").match?(/importmap-rails/)
10+
run_command("bin/rails", "importmap:install")
11+
end
12+
run_command("bin/rails", "turbo:install")
13+
14+
assert_match %(import "@hotwired/turbo-rails"\n), File.read("app/javascript/application.js")
15+
16+
if Rails::VERSION::MAJOR >= 7
17+
assert_match %(pin "@hotwired/turbo-rails", to: "turbo.min.js"), File.read("config/importmap.rb")
18+
else
19+
assert_match "@hotwired/turbo-rails", File.read("package.json")
20+
assert_match "@hotwired/turbo-rails", File.read("yarn.lock")
21+
end
22+
end
23+
end
24+
25+
test "installer with no javascript" do
26+
with_new_rails_app %w[--skip-javascript] do
27+
out, = run_command("bin/rails", "turbo:install")
28+
29+
assert_match "You must either be running with node (package.json) or importmap-rails (config/importmap.rb) to use this gem.", out
30+
end
31+
end
32+
33+
test "installer with pre-existing application.js" do
34+
with_new_rails_app do
35+
if Rails::VERSION::MAJOR >= 7 && File.read("Gemfile").match?(/importmap-rails/)
36+
run_command("bin/rails", "importmap:install")
37+
end
38+
File.write("app/javascript/application.js", "// pre-existing")
39+
run_command("bin/rails", "turbo:install")
40+
41+
assert_match "// pre-existing", File.read("app/javascript/application.js")
42+
end
43+
end
44+
45+
if Gem::Version.new(Rails.version) >= Gem::Version.new("7.1")
46+
test "installer with bun" do
47+
with_new_rails_app %w[--javascript=bun] do
48+
run_command("bin/rails", "javascript:install:bun")
49+
run_command("bin/rails", "turbo:install")
50+
51+
assert_match %(import "@hotwired/turbo-rails"\n), File.read("app/javascript/application.js")
52+
53+
assert_match "@hotwired/turbo-rails", File.read("package.json")
54+
end
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)