From 4681ca870933ae41d292d43741340fb29f80f4b5 Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Sun, 9 Sep 2018 17:38:50 +0530 Subject: [PATCH] Configure cache_dir --- features/cache.feature | 28 ++++++++++++++++++++++++++++ lib/jekyll/cache.rb | 6 +++++- lib/jekyll/site.rb | 1 + test/test_site.rb | 10 +++++++--- 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 features/cache.feature diff --git a/features/cache.feature b/features/cache.feature new file mode 100644 index 00000000000..99aee2457e8 --- /dev/null +++ b/features/cache.feature @@ -0,0 +1,28 @@ +Feature: Cache + As a developer who likes to create plugins + I want to be able to cache certain aspects across multiple builds + And retrieve the cached aspects when needed + + Scenario: Default Cache directory + Given I have an "index.md" page that contains "{{ site.title }}" + And I have a configuration file with "title" set to "Hello World" + When I run jekyll build + Then I should get a zero exit status + And the .jekyll-cache directory should exist + And the .jekyll-cache/Jekyll/Cache/Jekyll--Cache directory should exist + And the _site directory should exist + And I should see "

Hello World

" in "_site/index.html" + + Scenario: Custom Cache directory + Given I have an "index.md" page that contains "{{ site.title }}" + And I have a configuration file with: + | key | value | + | title | Hello World | + | cache_dir | .foo-cache | + When I run jekyll build + Then I should get a zero exit status + And the .foo-cache directory should exist + And the .foo-cache/Jekyll/Cache/Jekyll--Cache directory should exist + But the .jekyll-cache directory should not exist + And the _site directory should exist + And I should see "

Hello World

" in "_site/index.html" diff --git a/lib/jekyll/cache.rb b/lib/jekyll/cache.rb index ae5138e018d..28e366a180c 100644 --- a/lib/jekyll/cache.rb +++ b/lib/jekyll/cache.rb @@ -14,11 +14,15 @@ class Cache # # Returns nothing. def initialize(name) - @@base_dir ||= File.expand_path(".jekyll-cache/Jekyll/Cache") @cache = @@caches[name] ||= {} @name = name.gsub(%r![^\w\s-]!, "-") end + # Set class-wide base_dir + def self.base_dir=(dir_path) + @@base_dir = dir_path + end + # Disable Marshaling cached items to disk def self.disable_disk_cache! @@disk_cache_enabled = false diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 530e2b233a0..0e24586c872 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -425,6 +425,7 @@ def site_cleaner # Disable Marshaling cache to disk in Safe Mode def configure_cache + Jekyll::Cache.base_dir = in_source_dir(config["cache_dir"], "Jekyll/Cache") Jekyll::Cache.disable_disk_cache! if safe end diff --git a/test/test_site.rb b/test/test_site.rb index d0322ebbdb3..ed34b465f66 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -76,13 +76,17 @@ def read_posts allow(File).to receive(:directory?).with(theme_dir("_sass")).and_return(true) allow(File).to receive(:directory?).with(theme_dir("_layouts")).and_return(true) allow(File).to receive(:directory?).with(theme_dir("_includes")).and_return(false) - allow(File).to receive(:directory?).with( - File.expand_path(".jekyll-cache/Jekyll/Cache/Jekyll--Cache") - ).and_return(true) site = fixture_site("theme" => "test-theme") assert_equal [source_dir("_includes")], site.includes_load_paths end + + should "configure cache_dir" do + fixture_site.process + assert File.directory?(source_dir(".jekyll-cache", "Jekyll", "Cache")) + assert File.directory?(source_dir(".jekyll-cache", "Jekyll", "Cache", "Jekyll--Cache")) + end end + context "creating sites" do setup do @site = Site.new(site_configuration)