forked from shakacode/react_on_rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
symlink tests with tempfs
- Loading branch information
Showing
1 changed file
with
96 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,114 @@ | ||
require_relative "spec_helper" | ||
require "react_on_rails/assets_precompile" | ||
|
||
# require "tmpdir" | ||
# require "tempfile" | ||
require "tmpdir" | ||
require "tempfile" | ||
|
||
module ReactOnRails | ||
RSpec.describe AssetsPrecompile do | ||
describe "symlink_file" do | ||
it "creates a proper symlink" | ||
it "creates a proper symlink if nested" | ||
let(:assets_path) { Pathname.new(Dir.mktmpdir) } | ||
|
||
describe "#symlink_file" do | ||
it "creates a proper symlink" do | ||
filename = File.basename(Tempfile.new("tempfile", assets_path)) | ||
digest_filename = "#{filename}_digest" | ||
AssetsPrecompile.new(assets_path: assets_path) | ||
.symlink_file(filename, digest_filename) | ||
|
||
expect(assets_path.join(digest_filename).lstat.symlink?).to be true | ||
expect(File.identical?(assets_path.join(filename), | ||
assets_path.join(digest_filename))).to be true | ||
end | ||
|
||
it "creates a proper symlink when nested" do | ||
Dir.mkdir assets_path.join("images") | ||
filename = "images/" + File.basename(Tempfile.new("tempfile", | ||
assets_path.join("images"))) | ||
digest_filename = "#{filename}_digest" | ||
AssetsPrecompile.new(assets_path: assets_path) | ||
.symlink_file(filename, digest_filename) | ||
|
||
expect(assets_path.join(digest_filename).lstat.symlink?).to be true | ||
expect(File.identical?(assets_path.join(filename), | ||
assets_path.join(digest_filename))).to be true | ||
end | ||
end | ||
|
||
describe "symlink_non_digested_assets" do | ||
it "creates the necessary symlinks" | ||
let(:digest_filename) { "alfa.12345.js" } | ||
let(:nondigest_filename) { "alfa.js" } | ||
|
||
let(:checker) do | ||
f = File.new(assets_path.join("manifest-alfa.json"), "w") | ||
f.write("{\"assets\":{\"#{nondigest_filename}\": \"#{digest_filename}\"}}") | ||
f.close | ||
|
||
AssetsPrecompile.new(assets_path: assets_path, | ||
symlink_non_digested_assets_regex: Regexp.new('.*\.js$')) | ||
end | ||
|
||
context "correct nondigest filename" do | ||
it "create valid symlink" do | ||
FileUtils.touch assets_path.join(digest_filename) | ||
checker.symlink_non_digested_assets | ||
|
||
expect(assets_path.join(nondigest_filename).lstat.symlink?).to be true | ||
expect(File.identical?(assets_path.join(nondigest_filename), | ||
assets_path.join(digest_filename))).to be true | ||
end | ||
end | ||
|
||
context "zipped nondigest filename" do | ||
it "create valid symlink" do | ||
FileUtils.touch assets_path.join("#{digest_filename}.gz") | ||
checker.symlink_non_digested_assets | ||
|
||
expect(assets_path.join("#{nondigest_filename}.gz").lstat.symlink?).to be true | ||
expect(File.identical?(assets_path.join("#{nondigest_filename}.gz"), | ||
assets_path.join("#{digest_filename}.gz"))).to be true | ||
end | ||
end | ||
|
||
context "wrong nondigest filename" do | ||
it "should not create symlink" do | ||
FileUtils.touch assets_path.join("alfa.12345.jsx") | ||
checker.symlink_non_digested_assets | ||
|
||
expect(assets_path.join("alfa.jsx")).not_to exist | ||
end | ||
end | ||
end | ||
|
||
describe "delete_broken_symlinks" do | ||
it "deletes a broken symlink" | ||
it "deletes a broken symlink" do | ||
filename = File.basename(Tempfile.new("tempfile", assets_path)) | ||
digest_filename = "#{filename}_digest" | ||
|
||
a = AssetsPrecompile.new(assets_path: assets_path) | ||
a.symlink_file(filename, digest_filename) | ||
File.unlink(assets_path.join(filename)) | ||
a.delete_broken_symlinks | ||
|
||
expect(assets_path.join(filename)).not_to exist | ||
expect(assets_path.join(digest_filename)).not_to exist | ||
end | ||
end | ||
|
||
describe "clobber" do | ||
it "deletes files in ReactOnRails.configuration.generated_assets_dir" | ||
it "deletes files in ReactOnRails.configuration.generated_assets_dir" do | ||
allow(Rails).to receive(:root).and_return(Pathname.new(Dir.mktmpdir)) | ||
|
||
generated_assets_dir = "generated_dir" | ||
generated_assets_path = Rails.root.join(generated_assets_dir) | ||
Dir.mkdir generated_assets_path | ||
|
||
filepath = Pathname.new(Tempfile.new("tempfile", generated_assets_path)) | ||
|
||
AssetsPrecompile.new(assets_path: assets_path, | ||
generated_assets_dir: generated_assets_dir).clobber | ||
|
||
expect(filepath).not_to exist | ||
end | ||
end | ||
end | ||
end |