From 5530cb574b912be00d88aa7023df5dfe2973b9ae Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Wed, 3 Jan 2024 13:15:35 +0800 Subject: [PATCH] DEV: Fix test incorrectly removing stylesheet cache of other processes (#25103) Why this change? The `can survive cache miss` test in `spec/requests/stylesheets_controller_spec.rb` was failing because the file was not found on disk for the cache to be regenerated. This is because a test in `spec/lib/stylesheet/manager_spec.rb` was removing the entire `tmp/stylesheet-cache` directory which is incorrect because the folder in the test environment further segretates the stylesheet caches based on the process of the test. What does this change do? 1. Introduce `Stylesheet::Manager.rm_cache_folder` method for the test environment to properly clean up the cache folder. 2. Make `Stylesheet::Manager::CACHE_PATH` a private constant since the cache path should be obtained from the `Stylesheet::Manager.cache_fullpath` method. --- lib/stylesheet/manager.rb | 10 +++++++++- spec/lib/stylesheet/manager_spec.rb | 2 +- spec/requests/stylesheets_controller_spec.rb | 6 +++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb index aaf7db8efcb..2aa5821c65a 100644 --- a/lib/stylesheet/manager.rb +++ b/lib/stylesheet/manager.rb @@ -9,7 +9,9 @@ end class Stylesheet::Manager BASE_COMPILER_VERSION = 1 - CACHE_PATH ||= "tmp/stylesheet-cache" + CACHE_PATH = "tmp/stylesheet-cache" + private_constant :CACHE_PATH + MANIFEST_DIR ||= "#{Rails.root}/tmp/cache/assets/#{Rails.env}" THEME_REGEX ||= /_theme\z/ COLOR_SCHEME_STYLESHEET ||= "color_definitions" @@ -191,6 +193,12 @@ class Stylesheet::Manager File.join(path, "test_#{ENV["TEST_ENV_NUMBER"].presence || "0"}") end + if Rails.env.test? + def self.rm_cache_folder + FileUtils.rm_rf(cache_fullpath) + end + end + attr_reader :theme_ids def initialize(theme_id: nil) diff --git a/spec/lib/stylesheet/manager_spec.rb b/spec/lib/stylesheet/manager_spec.rb index 774ecaaf5b4..d1be670fb17 100644 --- a/spec/lib/stylesheet/manager_spec.rb +++ b/spec/lib/stylesheet/manager_spec.rb @@ -886,7 +886,7 @@ RSpec.describe Stylesheet::Manager do after do STDERR.unstub(:write) - FileUtils.rm_rf("tmp/stylesheet-cache") + Stylesheet::Manager.rm_cache_folder end it "correctly generates precompiled CSS" do diff --git a/spec/requests/stylesheets_controller_spec.rb b/spec/requests/stylesheets_controller_spec.rb index 4ef456fb396..91ee070a113 100644 --- a/spec/requests/stylesheets_controller_spec.rb +++ b/spec/requests/stylesheets_controller_spec.rb @@ -18,7 +18,7 @@ RSpec.describe StylesheetsController do expect(cached.digest).to eq digest # tmp folder destruction and cached - `rm -rf #{Stylesheet::Manager.cache_fullpath}` + Stylesheet::Manager.rm_cache_folder get "/stylesheets/desktop_rtl_#{digest}.css" expect(response.status).to eq(200) @@ -35,7 +35,7 @@ RSpec.describe StylesheetsController do builder = Stylesheet::Manager::Builder.new(target: :desktop, theme: theme, manager: manager) builder.compile - `rm -rf #{Stylesheet::Manager.cache_fullpath}` + Stylesheet::Manager.rm_cache_folder get "/stylesheets/#{builder.stylesheet_filename.sub(".css", "")}.css" @@ -49,7 +49,7 @@ RSpec.describe StylesheetsController do Stylesheet::Manager::Builder.new(target: :desktop_theme, theme: theme, manager: manager) builder.compile - `rm -rf #{Stylesheet::Manager.cache_fullpath}` + Stylesheet::Manager.rm_cache_folder get "/stylesheets/#{builder.stylesheet_filename.sub(".css", "")}.css"