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

Make content_for raise an exception in case of silently overwriting content #713

Merged
merged 4 commits into from
Nov 1, 2015
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion lib/nanoc/helpers/capturing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ def initialize

def []=(item, name, content)
@store[item.identifier] ||= {}
@store[item.identifier][name] = content
if @store[item.identifier][name]
return if @store[item.identifier][name] == content
raise KeyError, "Content_for was called twice with the same key: #{name}, this would overwrite content for the first call"
Copy link
Member

Choose a reason for hiding this comment

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

I don’t think KeyError is correct—that is an exception used when the value of a given key isn’t found. I think raising a string is OK.

else
@store[item.identifier][name] = content
end
end

def [](item, name)
Expand Down
30 changes: 28 additions & 2 deletions test/helpers/test_capturing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ def test_content_for_recursively
<% content_for :box do %>
basic
<% end %>
<% content_for :box do %>
<% content_for :outerbox do %>
before <%= content_for @item, :box %> after
<% end %>
<%= content_for @item, :box %>
<%= content_for @item, :outerbox %>
foot
EOS

Expand Down Expand Up @@ -101,6 +101,32 @@ def test_different_sites
assert_equal 'Content Two', content_for(@item, :b)
end

def test_content_for_duplicate_symbols
with_site do |_site|
# Prepare
File.open('lib/helpers.rb', 'w') do |io|
io.write 'include Nanoc::Helpers::Capturing'
end
File.open('content/includer.erb', 'w') do |io|
io.write '[<%= content_for(@items.find { |i| i.identifier == \'/includee/\' }, :blah) %>]'
Copy link
Member

Choose a reason for hiding this comment

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

Replace

@items.find { |i| i.identifier == \'/includee/\' }

with

@items["/includee/"]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I changed that, but note that all other tests in that file use the other style

end
File.open('Rules', 'w') do |io|
io.write "compile '*' do ; filter :erb ; end\n"
io.write "route '*' do ; item.identifier + 'index.html' ; end\n"
end

File.open('content/includee.erb', 'w') do |io|
io.write '{<% content_for :blah do %>First content<% end %><% content_for :blah do %>Second content<% end %>}'
end

# Using the same symbols twice now raises an error, to be changed to concatenating in a future version
assert_raises KeyError do
Nanoc::CLI.run(%w(compile))
end

end
end

def test_dependencies
with_site do |_site|
# Prepare
Expand Down