From 60eff9421af3a724c7942342cf4e26d9bdc2d7c0 Mon Sep 17 00:00:00 2001
From: Osama Sayegh <asooomaasoooma90@gmail.com>
Date: Fri, 31 Aug 2018 14:23:55 +0300
Subject: [PATCH] FIX: precompile `desktop_theme` and `mobile_theme`
 stylesheets

required for environments that pre stage docker images and keep old image running during the deploy
---
 lib/stylesheet/manager.rb                  |  3 +-
 spec/components/stylesheet/manager_spec.rb | 61 ++++++++++++++++++++++
 2 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rb
index 3b4b4400a76..7beded7f1b2 100644
--- a/lib/stylesheet/manager.rb
+++ b/lib/stylesheet/manager.rb
@@ -86,8 +86,9 @@ class Stylesheet::Manager
     themes = Theme.where('user_selectable OR id = ?', SiteSetting.default_theme_id).pluck(:id, :name)
     themes << nil
     themes.each do |id, name|
-      [:desktop, :mobile, :desktop_rtl, :mobile_rtl].each do |target|
+      [:desktop, :mobile, :desktop_rtl, :mobile_rtl, :desktop_theme, :mobile_theme, :admin].each do |target|
         theme_id = id || SiteSetting.default_theme_id
+        next if target =~ THEME_REGEX && theme_id == -1
         cache_key = "#{target}_#{theme_id}"
 
         STDERR.puts "precompile target: #{target} #{name}"
diff --git a/spec/components/stylesheet/manager_spec.rb b/spec/components/stylesheet/manager_spec.rb
index fb6f3fd5dbb..19426478fbb 100644
--- a/spec/components/stylesheet/manager_spec.rb
+++ b/spec/components/stylesheet/manager_spec.rb
@@ -157,4 +157,65 @@ describe Stylesheet::Manager do
       expect(digest3).to_not eq(digest1)
     end
   end
+
+  # this test takes too long, we don't run it by default
+  describe ".precompile_css", if: ENV["RUN_LONG_TESTS"] == "1" do
+    before do
+      class << STDERR
+        alias_method :orig_write, :write
+        def write(x)
+        end
+      end
+    end
+
+    after do
+      class << STDERR
+        def write(x)
+          orig_write(x)
+        end
+      end
+      FileUtils.rm_rf("tmp/stylesheet-cache")
+    end
+
+    it "correctly generates precompiled CSS" do
+      scheme1 = ColorScheme.create!(name: "scheme1")
+      scheme2 = ColorScheme.create!(name: "scheme2")
+      core_targets = [:desktop, :mobile, :desktop_rtl, :mobile_rtl, :admin]
+      theme_targets = [:desktop_theme, :mobile_theme]
+
+      Theme.update_all(user_selectable: false)
+      user_theme = Fabricate(:theme, user_selectable: true, color_scheme: scheme1)
+      default_theme = Fabricate(:theme, user_selectable: true, color_scheme: scheme2)
+      default_theme.set_default!
+
+      StylesheetCache.destroy_all
+
+      Stylesheet::Manager.precompile_css
+      results = StylesheetCache.pluck(:target)
+
+      expect(results.size).to eq(14) # 2 themes x 7 targets
+      core_targets.each do |tar|
+        expect(results.count { |target| target =~ /^#{tar}_(#{scheme1.id}|#{scheme2.id})$/ }).to eq(2)
+      end
+
+      theme_targets.each do |tar|
+        expect(results.count { |target| target =~ /^#{tar}_(#{user_theme.id}|#{default_theme.id})$/ }).to eq(2)
+      end
+
+      Theme.clear_default!
+      StylesheetCache.destroy_all
+
+      Stylesheet::Manager.precompile_css
+      results = StylesheetCache.pluck(:target)
+      expect(results.size).to eq(19) # (2 themes x 7 targets) + (1 no/default/core theme x 5 core targets)
+
+      core_targets.each do |tar|
+        expect(results.count { |target| target =~ /^(#{tar}_(#{scheme1.id}|#{scheme2.id})|#{tar})$/ }).to eq(3)
+      end
+
+      theme_targets.each do |tar|
+        expect(results.count { |target| target =~ /^#{tar}_(#{user_theme.id}|#{default_theme.id})$/ }).to eq(2)
+      end
+    end
+  end
 end