Skip to content

Commit

Permalink
Add tests that confirm the new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
justin808 committed Sep 7, 2016
1 parent 7ecdc36 commit 168f3c8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/react_on_rails/assets_precompile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ def symlink_file(target, symlink)
target_exists = File.exist?(target_path)
raise SymlinkTargetDoesNotExistException, "Target Path was: #{target_path}" unless target_exists

# File.exist?(symlink_path) will check the file the sym is pointing to is existing
# File.lstat(symlink_path).symlink? confirms that this is a symlink
valid_symlink_already_exists = File.exist?(symlink_path) && File.lstat(symlink_path).symlink?

if valid_symlink_already_exists
if symlink_and_points_to_existing_file?(symlink_path)
puts "React On Rails: Digested version of #{symlink} already exists indicating #{target} did not change."
return
end
Expand Down Expand Up @@ -120,7 +116,15 @@ def clobber

private

def symlink_and_points_to_existing_file?(symlink_path)
# File.exist?(symlink_path) will check the file the sym is pointing to is existing
# File.lstat(symlink_path).symlink? confirms that this is a symlink
File.exist?(symlink_path) && File.lstat(symlink_path).symlink?
end

def file_or_symlink_exists_at_path?(path)
# We use lstat and not stat, we we don't want to visit the file that the symlink maybe
# pointing to. We can't use File.exist?, as that would check the file pointed at by the symlink.
File.lstat(path)
true
rescue
Expand Down
42 changes: 42 additions & 0 deletions spec/react_on_rails/assets_precompile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,48 @@ module ReactOnRails
end.to raise_exception(AssetsPrecompile::SymlinkTargetDoesNotExistException)
end
end

it "creates a proper symlink when a file exists at destination" do
filename = File.basename(Tempfile.new("tempfile", assets_path))
existing_filename = File.basename(Tempfile.new("tempfile", assets_path))
digest_filename = existing_filename

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 a symlink file exists at destination" do
filename = File.basename(Tempfile.new("tempfile", assets_path))
existing_filename = File.basename(Tempfile.new("tempfile", assets_path))
digest_file = Tempfile.new("tempfile", assets_path)
digest_filename = File.basename(digest_file)
File.delete(digest_file)
File.symlink(existing_filename, digest_filename)

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
File.delete(digest_filename)
end

it "creates a proper symlink when an invalid symlink exists at destination" do
filename = File.basename(Tempfile.new("tempfile", assets_path))
existing_file = Tempfile.new("tempfile", assets_path)
existing_filename = File.basename(existing_file)
digest_file = Tempfile.new("tempfile", assets_path)
digest_filename = File.basename(digest_file)
File.symlink(existing_filename, digest_filename)
File.delete(existing_file) # now digest_filename is an invalid link

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
File.delete(digest_filename)
end
end

describe "symlink_non_digested_assets" do
Expand Down

0 comments on commit 168f3c8

Please sign in to comment.