Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return unless 'jekyll-archives' config is a Hash #139

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/jekyll-archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ class Archives < Jekyll::Generator
}.freeze

def initialize(config = {})
@config = Utils.deep_merge_hashes(DEFAULTS, config.fetch("jekyll-archives", {}))
archives_config = config.fetch("jekyll-archives", {})
if archives_config.is_a?(Hash)
@config = Utils.deep_merge_hashes(DEFAULTS, archives_config)
else
@config = nil
Jekyll.logger.warn "Archives:", "Expected a hash but got #{archives_config.inspect}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to force our users to care about data structures? What about something like

Suggested change
Jekyll.logger.warn "Archives:", "Expected a hash but got #{archives_config.inspect}"
Jekyll.logger.warn "Archives:", "The configuration appears to be invalid"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattr- I'm not sure about this suggestion..
When a third-party plugin (e.g. jekyll-algolia) erroneously sets config["jekyll-archives"] to nil explicitly, a simple "The configuration appears to be invalid" is going to throw the user into more confusion because:

  • First of all, their config file doesn't have a jekyll-archives key.
  • Secondly, they have no context as to why the configuration is invalid.
  • they have no backtrace to debug on their own.

Jekyll.logger.warn "", "Archives will not be generated for this site."
end
end

def generate(site)
return if @config.nil?

@site = site
@posts = site.posts
@archives = []
Expand Down
11 changes: 11 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,16 @@ def read_file(path)

File.read(read_path).strip
end

def capture_output(level = :debug)
buffer = StringIO.new
Jekyll.logger = Logger.new(buffer)
Jekyll.logger.log_level = level
yield
buffer.rewind
buffer.string.to_s
ensure
Jekyll.logger = Logger.new(StringIO.new, :error)
end
end
end
39 changes: 39 additions & 0 deletions test/test_jekyll_archives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,43 @@ class TestJekyllArchives < Minitest::Test
assert @day_archive.date.is_a? Date
end
end

context "the jekyll-archives plugin with a non-hash config" do
should "output a warning" do
output = capture_output do
site = fixture_site("jekyll-archives" => %w(apples oranges))
site.read
site.generate
end
assert_includes output, "Archives: Expected a hash but got [\"apples\", \"oranges\"]"
assert_includes output, "Archives will not be generated for this site."

output = capture_output do
site = fixture_site("jekyll-archives" => nil)
site.read
site.generate
end
assert_includes output, "Archives: Expected a hash but got nil"
assert_includes output, "Archives will not be generated for this site."
end

should "not generate archive pages" do
capture_output do
site = fixture_site("jekyll-archives" => nil)
site.read
site.generate
assert_nil(site.pages.find { |p| p.is_a?(Jekyll::Archives::Archive) })
end
end

should "be fine with a basic config" do
output = capture_output do
@site = fixture_site("title" => "Hello World")
@site.read
@site.generate
end
refute_includes output, "Archives: Expected a hash but got nil"
assert_nil(@site.pages.find { |p| p.is_a?(Jekyll::Archives::Archive) })
end
end
end