Skip to content

A platform for community discussion. Free, open, simple.

PERF: Memoize `fs_asset_cachebuster` in the test environment (#39937)

David Taylor501b2e3efeab
diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb
index 2b7f2a727ea..1bec3c78398 100644
--- a/lib/stylesheet/manager.rb
+++ b/lib/stylesheet/manager.rb
@@ -161,7 +161,7 @@ class Stylesheet::Manager
private_class_method :manifest_full_path
def self.use_file_hash_for_cachebuster?
- Rails.env.production?
+ Rails.env.production? || Rails.env.test?
end
private_class_method :use_file_hash_for_cachebuster?
diff --git a/spec/lib/stylesheet/manager_spec.rb b/spec/lib/stylesheet/manager_spec.rb
index e90caf34a04..36f51005f39 100644
--- a/spec/lib/stylesheet/manager_spec.rb
+++ b/spec/lib/stylesheet/manager_spec.rb
@@ -1062,8 +1062,25 @@ RSpec.describe Stylesheet::Manager do
end
describe ".fs_asset_cachebuster" do
- it "returns a number in test/development mode" do
- expect(Stylesheet::Manager.fs_asset_cachebuster).to match(/\A.*:[0-9]+\z/)
+ context "when memoizing via the filesystem manifest (production / test)" do
+ after do
+ path = Stylesheet::Manager.send(:manifest_full_path)
+ File.delete(path) if File.exist?(path)
+ Stylesheet::Manager.recalculate_fs_asset_cachebuster!
+ end
+
+ it "returns a content hash" do
+ expect(Stylesheet::Manager.fs_asset_cachebuster).to match(/\A.*:[0-9a-f]{40}\z/)
+ end
+ end
+
+ context "when memoizing in-process via mtime (development)" do
+ before { Stylesheet::Manager.stubs(:use_file_hash_for_cachebuster?).returns(false) }
+ after { Stylesheet::Manager.recalculate_fs_asset_cachebuster! }
+
+ it "returns an mtime number" do
+ expect(Stylesheet::Manager.fs_asset_cachebuster).to match(/\A.*:[0-9]+\z/)
+ end
end
context "with production mode enabled" do
@@ -1074,11 +1091,6 @@ RSpec.describe Stylesheet::Manager do
File.delete(path) if File.exist?(path)
end
- it "returns a hash" do
- cachebuster = Stylesheet::Manager.fs_asset_cachebuster
- expect(cachebuster).to match(/\A.*:[0-9a-f]{40}\z/)
- end
-
it "caches the value on the filesystem" do
initial_cachebuster = Stylesheet::Manager.recalculate_fs_asset_cachebuster!
Stylesheet::Manager.stubs(:list_files).never