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

Improve detection of files already digested #718

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions test/test_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1136,13 +1136,13 @@ 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.exists?(digested)
FileUtils.rm(digested) if File.exist?(digested)
end

def asset(logical_path, options = {})
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