Skip to content

Commit

Permalink
simplify host_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Judahmeek committed Oct 24, 2023
1 parent 6c6d4af commit dc333f0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
16 changes: 4 additions & 12 deletions lib/shakapacker/digest_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,12 @@ def record_compilation_digest

def compilation_digest_path
path = "last-compilation-digest-#{Shakapacker.env}"
path += "-#{generate_host_hash}" if generate_host_hash.present?
if Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"].present?
host_hash = Digest::SHA1.hexdigest(Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"])
path += "-#{host_hash}"
end

config.cache_path.join(path)
end

def generate_host_hash
# Using hash for memoizing the host hash is to make testing easier.
# The default value, prevents generating hash in the situation where no value for asset_host
# and SHAKAPACKER_ASSET_HOST are provided, leading to not add hash to the asset path.
@generated_host_hashes ||= { [nil, nil] => "" }

keys = [Rails.application.config.asset_host, ENV["SHAKAPACKER_ASSET_HOST"]]

@generated_host_hashes[keys] ||= Digest::SHA1.hexdigest(keys.join("-"))
end
end
end
25 changes: 9 additions & 16 deletions spec/shakapacker/digest_strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,31 @@ def remove_compilation_digest_path
end

it "is stale when host changes" do
# allow(Shakapacker.config).to receive(:fetch).with(any_args).and_call_original
# allow(Shakapacker.config).to receive(:fetch).with(:compiler_strategy_asset_host_sensitive).and_return(true)
old_host = Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"]

ENV["SHAKAPACKER_ASSET_HOST"] = "the-host"
Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "the-host"

@digest_strategy.after_compile_hook

ENV["SHAKAPACKER_ASSET_HOST"] = "new-host"
Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "new-host"

expect(@digest_strategy.stale?).to be true
expect(@digest_strategy.fresh?).to be_falsey

ENV["SHAKAPACKER_ASSET_HOST"] = nil
Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = old_host
end

it "generates correct compilation_digest_path" do
actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s
expected_path = "last-compilation-digest-#{Shakapacker.env}"

expect(actual_path).to eq expected_path
end

it "generates correct compilation_digest_path with " do
# allow(Shakapacker.config).to receive(:fetch).with(any_args).and_call_original
# allow(Shakapacker.config).to receive(:fetch).with(:compiler_strategy_asset_host_sensitive).and_return(true)
old_host = Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"]

ENV["SHAKAPACKER_ASSET_HOST"] = "custom-path"
Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = "custom-path"

actual_path = @digest_strategy.send(:compilation_digest_path).basename.to_s
host_hash = Digest::SHA1.hexdigest("-custom-path")
host_hash = Digest::SHA1.hexdigest("custom-path")
expected_path = "last-compilation-digest-#{Shakapacker.env}-#{host_hash}"

expect(actual_path).to eq expected_path

Shakapacker::Compiler.env["SHAKAPACKER_ASSET_HOST"] = old_host
end
end

0 comments on commit dc333f0

Please sign in to comment.