Permalink
Browse files

Asset digest and length should be of its source

  • Loading branch information...
josh committed Nov 3, 2011
1 parent 2209ed2 commit 6fd49f93008fb0210902fdcc23c46126af78759a
Showing with 27 additions and 7 deletions.
  1. +2 −2 lib/sprockets/bundled_asset.rb
  2. +19 −3 lib/sprockets/processed_asset.rb
  3. +6 −2 test/test_asset.rb
@@ -38,7 +38,7 @@ def init_with(environment, coder)
@processed_asset = environment.find_asset(pathname, :bundle => false)
@required_assets = @processed_asset.required_assets
- if @processed_asset.digest != coder['self_digest']
+ if @processed_asset.dependency_digest != coder['required_assets_digest']
raise UnserializeError, "processed asset belongs to a stale environment"
end
@@ -50,7 +50,7 @@ def encode_with(coder)
super
coder['source'] = source
- coder['self_digest'] = @processed_asset.digest
+ coder['required_assets_digest'] = @processed_asset.dependency_digest
end
# Get asset's own processed contents. Excludes any of its required
@@ -11,21 +11,29 @@ def initialize(environment, logical_path, pathname)
context = environment.context_class.new(environment, logical_path, pathname)
@source = context.evaluate(pathname)
@length = Rack::Utils.bytesize(source)
+ @digest = environment.digest.update(source).hexdigest
build_required_assets(environment, context)
build_dependency_paths(environment, context)
+ @dependency_digest = compute_dependency_digest(environment)
+
elapsed_time = ((Time.now.to_f - start_time) * 1000).to_i
environment.logger.info "Compiled #{logical_path} (#{elapsed_time}ms) (pid #{Process.pid})"
end
+ # Interal: Used to check equality
+ attr_reader :dependency_digest
+
attr_reader :source
# Initialize `BundledAsset` from serialized `Hash`.
def init_with(environment, coder)
super
- @source = coder['source']
+ @source = coder['source']
+ @dependency_digest = coder['dependency_digest']
+
@required_assets = coder['required_paths'].map { |p|
p = expand_root_path(p)
@@ -44,8 +52,10 @@ def init_with(environment, coder)
def encode_with(coder)
super
- coder['source'] = source
- coder['required_paths'] = required_assets.map { |a|
+ coder['source'] = source
+ coder['dependency_digest'] = dependency_digest
+
+ coder['required_paths'] = required_assets.map { |a|
relativize_root_path(a.pathname).to_s
}
coder['dependency_paths'] = dependency_paths.map { |d|
@@ -128,5 +138,11 @@ def build_dependency_paths(environment, context)
@dependency_paths = dependency_paths.keys
end
+
+ def compute_dependency_digest(environment)
+ required_assets.inject(environment.digest) { |digest, asset|
+ digest.update asset.digest
+ }.hexdigest
+ end
end
end
View
@@ -14,8 +14,12 @@ def self.test(name, &block)
assert @asset.mtime
end
- test "digest" do
- assert @asset.digest
+ test "digest is source digest" do
+ assert_equal @env.digest.update(@asset.to_s).hexdigest, @asset.digest
+ end
+
+ test "length is source length" do
+ assert_equal @asset.to_s.length, @asset.length
end
test "stale?" do

0 comments on commit 6fd49f9

Please sign in to comment.