From ad7a5c0121f21b72e72d109966a1aabc8d31eb04 Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 19 Oct 2018 17:35:42 +0200 Subject: [PATCH] Skip processing posts that can not be read (#7302) Merge pull request 7302 --- lib/jekyll/readers/post_reader.rb | 38 +++++++++++------ .../_posts/2008-02-03-wrong-extension.yml | 8 ++++ test/test_post_reader.rb | 42 +++++++++++++++++++ test/test_site.rb | 2 +- 4 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 test/source/_posts/2008-02-03-wrong-extension.yml create mode 100644 test/test_post_reader.rb diff --git a/lib/jekyll/readers/post_reader.rb b/lib/jekyll/readers/post_reader.rb index a82f9d8e..94e095bd 100644 --- a/lib/jekyll/readers/post_reader.rb +++ b/lib/jekyll/readers/post_reader.rb @@ -34,19 +34,9 @@ module Jekyll # # Returns nothing. def read_publishable(dir, magic_dir, matcher) - read_content(dir, magic_dir, matcher).tap { |docs| docs.each(&:read) } - .select do |doc| - if doc.content.valid_encoding? - site.publisher.publish?(doc).tap do |will_publish| - if !will_publish && site.publisher.hidden_in_the_future?(doc) - Jekyll.logger.debug "Skipping:", "#{doc.relative_path} has a future date" - end - end - else - Jekyll.logger.debug "Skipping:", "#{doc.relative_path} is not valid UTF-8" - false - end - end + read_content(dir, magic_dir, matcher) + .tap { |docs| docs.each(&:read) } + .select { |doc| processable?(doc) } end # Read all the content files from //magic_dir @@ -68,5 +58,27 @@ module Jekyll :collection => @site.posts) end.reject(&:nil?) end + + private + + def processable?(doc) + if doc.content.nil? + Jekyll.logger.debug "Skipping:", "Content in #{doc.relative_path} is nil" + false + elsif !doc.content.valid_encoding? + Jekyll.logger.debug "Skipping:", "#{doc.relative_path} is not valid UTF-8" + false + else + publishable?(doc) + end + end + + def publishable?(doc) + site.publisher.publish?(doc).tap do |will_publish| + if !will_publish && site.publisher.hidden_in_the_future?(doc) + Jekyll.logger.debug "Skipping:", "#{doc.relative_path} has a future date" + end + end + end end end diff --git a/test/source/_posts/2008-02-03-wrong-extension.yml b/test/source/_posts/2008-02-03-wrong-extension.yml new file mode 100644 index 00000000..bb9f7f47 --- /dev/null +++ b/test/source/_posts/2008-02-03-wrong-extension.yml @@ -0,0 +1,8 @@ +--- +layout: default +title: This file extension is skipped +category: test_post_reader +--- + +`jekyll serve` used to crash if there's a post (document) with a wrong file extension. +This file is used in `./test/test_post_reader.rb` to verify that this doesn't happen anymore. diff --git a/test/test_post_reader.rb b/test/test_post_reader.rb new file mode 100644 index 00000000..51c806c9 --- /dev/null +++ b/test/test_post_reader.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +require "helper" + +class TestPostReader < JekyllUnitTest + context "#read_publishable" do + setup do + @site = Site.new(site_configuration) + @post_reader = PostReader.new(@site) + @dir = "" + @magic_dir = "_posts" + @matcher = Document::DATE_FILENAME_MATCHER + end + + should "skip unprocessable documents" do + all_file_names = all_documents.collect(&:basename) + processed_file_names = processed_documents.collect(&:basename) + + actual_skipped_file_names = all_file_names - processed_file_names + + expected_skipped_file_names = [ + "2008-02-02-not-published.markdown", + "2008-02-03-wrong-extension.yml", + ] + + skipped_file_names_difference = expected_skipped_file_names - actual_skipped_file_names + + assert expected_skipped_file_names.count.positive?, + "There should be at least one document expected to be skipped" + assert_empty skipped_file_names_difference, + "The skipped documents (expected/actual) should be congruent (= empty array)" + end + end + + def all_documents + @post_reader.read_content(@dir, @magic_dir, @matcher) + end + + def processed_documents + @post_reader.read_publishable(@dir, @magic_dir, @matcher) + end +end diff --git a/test/test_site.rb b/test/test_site.rb index d0322ebb..03e624ec 100644 --- a/test/test_site.rb +++ b/test/test_site.rb @@ -86,7 +86,7 @@ class TestSite < JekyllUnitTest context "creating sites" do setup do @site = Site.new(site_configuration) - @num_invalid_posts = 4 + @num_invalid_posts = 5 end teardown do