Skip to content

Commit dd6ab1b

Browse files
zzakjonathanhefnerdhh
authored
Test installer (#553)
* 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]> * Monkey-patch AppName#valid_const? on older Rails (< 7.1) My hunch is that this was fixed in Rails 7.1 by rails/rails#46074, but I'm less concerned here because this test suite uses a Dummy app for all of the tests. This is a problem I've been working on in rails/rails#50427, to remove the dummy applications and replace them with a generated app like we've done here with the installer test. I'm happy to investigate replacing the dummy app here afterwards. * Make sure only supported sqlite3 gem is installed ``` LoadError: Error loading the 'sqlite3' Active Record adapter. Missing a gem it depends on? can't activate sqlite3 (~> 1.4), already activated sqlite3-2.0.0-x86_64-linux-gnu. Make sure all dependencies are added to Gemfile. (LoadError) Caused by: Gem::LoadError: can't activate sqlite3 (~> 1.4), already activated sqlite3-2.0.0-x86_64-linux-gnu. Make sure all dependencies are added to Gemfile. (Gem::LoadError) Tasks: TOP => db:test:prepare => db:load_config (See full trace by running task with --trace) /home/zzak/.rbenv/versions/3.2.3/lib/ruby/gems/3.2.0/gems/bundler-2.5.4/lib/bundler/rubygems_integration.rb:237:in `block (2 levels) in replace_gem': Error loading the 'sqlite3' Active Record adapter. Missing a gem it depends on? can't activate sqlite3 (~> 1.4), already activated sqlite3-2.0.0-x86_64-linux-gnu. Make sure all dependencies are added to Gemfile. (LoadError) ``` * Fix installer test for Rails 6.1 --------- Co-authored-by: Jonathan Hefner <[email protected]> Co-authored-by: David Heinemeier Hansson <[email protected]>
1 parent 59e98c0 commit dd6ab1b

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
- name: Run Bug Template Tests
3535
run: ruby bug_report_template.rb || ruby bug_report_template.rb
3636

37+
- uses: oven-sh/setup-bun@v1
3738
- name: Run tests
3839
id: test
3940
run: bundle exec rake TESTOPT=-vdc

test/app_helper.rb

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

test/installer_test.rb

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

0 commit comments

Comments
 (0)