URL: warn if key doesn't exist in url drop #5524

Merged
merged 4 commits into from Nov 9, 2016

Projects

None yet

4 participants

@pnn
Member
pnn commented Nov 1, 2016

fixes #5332

the tests only cover not throwing a NoMethodError because i can't figure out how to capture the logged message to check if it logged the correct message (minitest's capture_io only works with kernel puts, weirdly enough)

pnn added some commits Nov 1, 2016
@pnn pnn add failing test 1bb2259
@pnn pnn rewrite test to only not throw error a2ffde8
@pnn pnn implement warning about missing keys in url drops
2d35364
@DirtyF DirtyF added the tests label Nov 2, 2016
@DirtyF
Member
DirtyF commented Nov 2, 2016
lib/jekyll/url.rb
- replacement = @placeholders.public_send(match.sub(":".freeze, "".freeze))
+ begin
+ replacement = @placeholders.public_send(match.sub(":".freeze, "".freeze))
+ rescue NoMethodError
@parkr
parkr Nov 2, 2016 Member

Instead of a begin .. rescue, what about checking for the key first, like

template.gsub(%r!:([a-z_]+)!) do |match|
  key = match.sub(":".freeze, "".freeze)
  raise NoMethodError, "No URL template key #{key} exists!" unless @placeholders.key?(key)
  if @placeholders[key].nil?
    "".freeze
  else
    self.class.escape_path(@placeholders[key])
  end
end.gsub(%r!//!, "/".freeze)
@pnn
pnn Nov 2, 2016 Member

hmm, that makes more sense, yeah

@pnn
pnn Nov 2, 2016 edited Member

hmm, by using this approach, it looks like the URLDrop (at least the one i use in my tests) throws an unrelated error because it can't find fallback_data when using key? or #[]

@parkr
parkr Nov 2, 2016 Member

Ah, ok. No problem. Define def fallback_data in UrlDrop as an empty hash.

class UrlDrop
  private
  def fallback_data
    {}
  end
end
@pnn pnn check for the urldrop key first
e92dd20
@pnn
Member
pnn commented Nov 2, 2016

updated according to @parkr's comments

@parkr
parkr approved these changes Nov 2, 2016 View changes

LGTM!

@pnn
Member
pnn commented Nov 6, 2016

@jekyll/core is this ok to merge? its been sitting for a while

@parkr
Member
parkr commented Nov 9, 2016

@jekyllbot: merge +minor

@jekyllbot jekyllbot merged commit f10c914 into jekyll:master Nov 9, 2016

1 of 2 checks passed

continuous-integration/appveyor/pr AppVeyor build failed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@pnn pnn deleted the unknown repository branch Nov 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment