From 63b3739062bd18b0ee45e22a6a0620a725b85cef Mon Sep 17 00:00:00 2001 From: Ashwin Maroli Date: Wed, 20 Feb 2019 21:17:20 +0530 Subject: [PATCH] Add `Site#in_cache_dir` helper method (#7160) Merge pull request 7160 --- lib/jekyll/site.rb | 16 +++++++++++++++- test/test_site.rb | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 233ff413..7904a393 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -2,7 +2,7 @@ module Jekyll class Site - attr_reader :source, :dest, :config + attr_reader :source, :dest, :cache_dir, :config attr_accessor :layouts, :pages, :static_files, :drafts, :exclude, :include, :lsi, :highlighter, :permalink_style, :time, :future, :unpublished, :safe, :plugins, :limit_posts, @@ -22,6 +22,8 @@ module Jekyll self.config = config + @cache_dir = in_source_dir(config["cache_dir"]) + @reader = Reader.new(self) @regenerator = Regenerator.new(self) @liquid_renderer = LiquidRenderer.new(self) @@ -401,6 +403,18 @@ module Jekyll end end + # Public: Prefix a given path with the cache directory. + # + # paths - (optional) path elements to a file or directory within the + # cache directory + # + # Returns a path which is prefixed with the cache directory. + def in_cache_dir(*paths) + paths.reduce(cache_dir) do |base, path| + Jekyll.sanitized_path(base, path) + end + end + # Public: The full path to the directory that houses all the collections registered # with the current site. # diff --git a/test/test_site.rb b/test/test_site.rb index f18c9a96..15cebaf3 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -85,6 +85,11 @@ class TestSite < JekyllUnitTest assert File.directory?(source_dir(".jekyll-cache", "Jekyll", "Cache")) assert File.directory?(source_dir(".jekyll-cache", "Jekyll", "Cache", "Jekyll--Cache")) end + + should "use .jekyll-cache directory at source as cache_dir by default" do + site = Site.new(default_configuration) + assert_equal File.join(site.source, ".jekyll-cache"), site.cache_dir + end end context "creating sites" do @@ -678,5 +683,23 @@ class TestSite < JekyllUnitTest refute_equal mtime1, mtime2 # must be regenerated end end + + context "#in_cache_dir method" do + setup do + @site = Site.new( + site_configuration( + "cache_dir" => "../../custom-cache-dir" + ) + ) + end + + should "create sanitized paths within the cache directory" do + assert_equal File.join(@site.source, "custom-cache-dir"), @site.cache_dir + assert_equal( + File.join(@site.source, "custom-cache-dir", "foo.md.metadata"), + @site.in_cache_dir("../../foo.md.metadata") + ) + end + end end end