Skip processing posts that can not be read (#7302)

Merge pull request 7302
This commit is contained in:
Mario 2018-10-19 17:35:42 +02:00 committed by jekyllbot
parent 7a1254563a
commit ad7a5c0121
4 changed files with 76 additions and 14 deletions

View File

@ -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 <source>/<dir>/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

View File

@ -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.

42
test/test_post_reader.rb Normal file
View File

@ -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

View File

@ -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