-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
102a491
commit d229a54
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |