Skip to content

Commit

Permalink
Merge pull request #718 from FestaLab/already-digested-fix
Browse files Browse the repository at this point in the history
Improve detection of files already digested
  • Loading branch information
dhh authored Nov 5, 2021
2 parents 3aa96f7 + 9e3d2df commit 3d1171d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
8 changes: 2 additions & 6 deletions lib/sprockets/digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,9 @@ def hexdigest_integrity_uri(hexdigest)
#
# name - The name of the asset
#
# Returns true if the name contains a digest recognized by one of the valid digest classes
# Returns true if the name contains a digest like string and .digested before the extension
def already_digested?(name)
return if name.nil?

if hexdigest = name.scan(/[a-fA-F0-9]+\z/).last
detect_digest_class(unpack_hexdigest(hexdigest))
end
return name =~ /-([0-9a-f]{7,128})\.digested/
end

private
Expand Down
6 changes: 3 additions & 3 deletions test/test_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1136,11 +1136,11 @@ def setup
test "digest path" do
path = File.expand_path("test/fixtures/asset/application")
original = "#{path}.js"
digested = "#{path}-d41d8cd98f00b204e9800998ecf8427e.js"
digested = "#{path}-d41d8cd98f00b204e9800998ecf8427e.digested.js"
FileUtils.cp(original, digested)

assert_equal "application-d41d8cd98f00b204e9800998ecf8427e.js",
asset("application-d41d8cd98f00b204e9800998ecf8427e.js").digest_path
assert_equal "application-d41d8cd98f00b204e9800998ecf8427e.digested.js",
asset("application-d41d8cd98f00b204e9800998ecf8427e.digested.js").digest_path
ensure
FileUtils.rm(digested) if File.exist?(digested)
end
Expand Down
14 changes: 5 additions & 9 deletions test/test_digest_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,10 @@ def test_hexdigest_integrity_uri

def test_already_digested
refute already_digested?(nil)
refute already_digested?("application-d41d8cd98f00b204e9800998ecf8427z")
refute already_digested?("application-d41d8cd98f00b204e9800998ecf8427ef")
refute already_digested?("application-d41d8cd98f00b204e9800998ecf8427e-")

assert already_digested?("application-" + Digest::MD5.new.hexdigest)
assert already_digested?("application-" + Digest::SHA1.new.hexdigest)
assert already_digested?("application-" + Digest::SHA256.new.hexdigest)
assert already_digested?("application-" + Digest::SHA384.new.hexdigest)
assert already_digested?("application-" + Digest::SHA512.new.hexdigest)
refute already_digested?("application-abc123.digested")
refute already_digested?("application-abcd1234")

assert already_digested?("application-abcd1234.digested")
assert already_digested?("application-abcd1234.digested.map")
end
end

0 comments on commit 3d1171d

Please sign in to comment.