Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test installer #553

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ group :test do
gem 'capybara'
gem 'rexml'
gem 'cuprite', '~> 0.9', require: 'capybara/cuprite'
gem 'sqlite3', '~> 1.4'
gem 'sqlite3', '< 2.0'
end
53 changes: 53 additions & 0 deletions test/app_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require "rails"
require "rails/test_help"
require "fileutils"

if Gem::Version.new(Rails.version) < Gem::Version.new("7.1")
module Rails::Generators::AppName
private
def valid_const?
true
end
end
end

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
60 changes: 60 additions & 0 deletions test/installer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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")

if Gem::Version.new(Rails.version) >= Gem::Version.new("7.0")
assert_match %(import "@hotwired/turbo-rails"\n), File.read("app/javascript/application.js")
end

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")
assert_match %(import "@hotwired/turbo-rails"\n), 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
Loading