Skip to content

Commit b423960

Browse files
Test dartsass:install command
This adds test coverage for the `dartsass:install` command. The command is tested 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.
1 parent 80e058a commit b423960

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

test/installer_test.rb

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require "test_helper"
2+
3+
class InstallerTest < ActiveSupport::TestCase
4+
include RailsAppHelpers
5+
6+
test "installer" do
7+
with_new_rails_app do
8+
out, _err = run_command("bin/rails", "dartsass:install")
9+
10+
assert_match "<DONE>", out
11+
assert_equal 0, File.size("app/assets/builds/.keep")
12+
assert_match "/app/assets/builds/*\n!/app/assets/builds/.keep", File.read(".gitignore")
13+
assert_equal File.read("#{__dir__}/../lib/install/application.scss"), File.read("app/assets/stylesheets/application.scss")
14+
assert_equal File.read("#{__dir__}/../lib/install/Procfile.dev"), File.read("Procfile.dev")
15+
assert_equal File.read("#{__dir__}/../lib/install/dev"), File.read("bin/dev")
16+
assert_equal 0700, File.stat("bin/dev").mode & 0700
17+
18+
if sprockets?
19+
assert_match "//= link_tree ../builds", File.read("app/assets/config/manifest.js")
20+
assert_no_match "//= link_directory ../stylesheets .css", File.read("app/assets/config/manifest.js")
21+
end
22+
end
23+
end
24+
25+
test "installer with missing .gitignore" do
26+
with_new_rails_app do
27+
FileUtils.rm(".gitignore")
28+
out, _err = run_command("bin/rails", "dartsass:install")
29+
30+
assert_match "<DONE>", out
31+
assert_not File.exist?(".gitignore")
32+
end
33+
end
34+
35+
test "installer with pre-existing application.scss" do
36+
with_new_rails_app do
37+
File.write("app/assets/stylesheets/application.scss", "// pre-existing")
38+
out, _err = run_command("bin/rails", "dartsass:install")
39+
40+
assert_match "<DONE>", out
41+
assert_equal "// pre-existing", File.read("app/assets/stylesheets/application.scss")
42+
end
43+
end
44+
45+
test "installer with pre-existing Procfile" do
46+
with_new_rails_app do
47+
File.write("Procfile.dev", "pre: existing\n")
48+
out, _err = run_command("bin/rails", "dartsass:install")
49+
50+
assert_match "<DONE>", out
51+
assert_equal "pre: existing\ncss: bin/rails dartsass:watch\n", File.read("Procfile.dev")
52+
end
53+
end
54+
55+
private
56+
def with_new_rails_app(&block)
57+
super do
58+
# Override `dartsass:build` to check that installation completed and to reduce test run time.
59+
File.write("Rakefile", <<~RAKEFILE, mode: "a+")
60+
Rake::Task["dartsass:build"].clear
61+
namespace :dartsass do
62+
task build: :environment do
63+
puts "<DONE>"
64+
end
65+
end
66+
RAKEFILE
67+
68+
block.call
69+
end
70+
end
71+
end

test/test_helper.rb

+71
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,74 @@
44
require "fileutils"
55
require "rails"
66
require "rails/test_help"
7+
8+
module RailsAppHelpers
9+
def self.included(base)
10+
base.include ActiveSupport::Testing::Isolation
11+
end
12+
13+
private
14+
def sprockets?
15+
Gem.loaded_specs.key?("sprockets-rails")
16+
end
17+
18+
def propshaft?
19+
Gem.loaded_specs.key?("propshaft")
20+
end
21+
22+
def asset_pipeline_option
23+
if Rails::VERSION::MAJOR > 7
24+
if propshaft?
25+
"--asset-pipeline=propshaft"
26+
elsif sprockets?
27+
"--asset-pipeline=sprockets"
28+
end
29+
end
30+
end
31+
32+
def create_new_rails_app(app_dir)
33+
require "rails/generators/rails/app/app_generator"
34+
Rails::Generators::AppGenerator.start([app_dir, *asset_pipeline_option, "--skip-bundle", "--skip-bootsnap", "--quiet"])
35+
36+
Dir.chdir(app_dir) do
37+
gemfile = File.read("Gemfile")
38+
39+
gemfile.gsub!(/^gem ["']sassc?-rails["'].*/, "") # for Rails 6.1 and 7.0
40+
gemfile.gsub!(/^gem ["']dartsass-rails["'].*/, "")
41+
gemfile << %(gem "dartsass-rails", path: #{File.expand_path("..", __dir__).inspect}\n)
42+
43+
if Rails::VERSION::PRE == "alpha"
44+
gemfile.gsub!(/^gem ["']rails["'].*/, "")
45+
gemfile << %(gem "rails", path: #{Gem.loaded_specs["rails"].full_gem_path.inspect}\n)
46+
end
47+
48+
File.write("Gemfile", gemfile)
49+
50+
run_command("bundle", "install")
51+
end
52+
end
53+
54+
def with_new_rails_app(&block)
55+
require "digest/sha1"
56+
variant = [Gem.loaded_specs["rails"].full_gem_path, asset_pipeline_option]
57+
app_name = "app_#{Digest::SHA1.hexdigest(variant.to_s)}"
58+
cache_dir = "#{__dir__}/../tmp"
59+
60+
Dir.mktmpdir do |tmpdir|
61+
if Dir.exist?("#{cache_dir}/#{app_name}")
62+
FileUtils.cp_r("#{cache_dir}/#{app_name}", tmpdir)
63+
else
64+
create_new_rails_app("#{tmpdir}/#{app_name}")
65+
FileUtils.cp_r("#{tmpdir}/#{app_name}", cache_dir) # Cache app for future runs.
66+
end
67+
68+
Dir.chdir("#{tmpdir}/#{app_name}", &block)
69+
end
70+
end
71+
72+
def run_command(*command)
73+
Bundler.with_unbundled_env do
74+
capture_subprocess_io { system(*command, exception: true) }
75+
end
76+
end
77+
end

0 commit comments

Comments
 (0)