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

Raise error when identifiers are not unique #435

Merged
merged 2 commits into from May 9, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/nanoc/base/errors.rb
Expand Up @@ -221,6 +221,15 @@ def initialize(type, plural)

end

# Error that is raised when multiple items or layouts with the same identifier exist.
class DuplicateIdentifier < Generic

def initialize(identifier, type)
super("There are multiple #{type}s with the #{identifier} identifier.")
end

end

end

end
14 changes: 14 additions & 0 deletions lib/nanoc/base/source_data/site.rb
Expand Up @@ -245,6 +245,10 @@ def load
data_sources.each { |ds| ds.unuse }
setup_child_parent_links

# Ensure unique
ensure_identifier_uniqueness(@items, 'item')
ensure_identifier_uniqueness(@layouts, 'layout')

# Load compiler too
# FIXME this should not be necessary
compiler.load
Expand Down Expand Up @@ -346,6 +350,16 @@ def load_layouts
end
end

def ensure_identifier_uniqueness(objects, type)
seen = Set.new
objects.each do |obj|
if seen.include?(obj.identifier)
raise Nanoc::Errors::DuplicateIdentifier.new(obj.identifier, type)
end
seen << obj.identifier
end
end

# Builds the configuration hash based on the given argument. Also see
# {#initialize} for details.
def build_config(dir_or_config_hash)
Expand Down
26 changes: 26 additions & 0 deletions test/base/test_site.rb
Expand Up @@ -113,6 +113,32 @@ def test_setup_child_parent_links
end
end

def test_multiple_items_with_same_identifier
with_site do
File.open('content/sam.html', 'w') { |io| io.write('I am Sam!') }
FileUtils.mkdir_p('content/sam')
File.open('content/sam/index.html', 'w') { |io| io.write('I am Sam, too!') }

assert_raises(Nanoc::Errors::DuplicateIdentifier) do
site = Nanoc::Site.new('.')
site.load
end
end
end

def test_multiple_layouts_with_same_identifier
with_site do
File.open('layouts/sam.html', 'w') { |io| io.write('I am Sam!') }
FileUtils.mkdir_p('layouts/sam')
File.open('layouts/sam/index.html', 'w') { |io| io.write('I am Sam, too!') }

assert_raises(Nanoc::Errors::DuplicateIdentifier) do
site = Nanoc::Site.new('.')
site.load
end
end
end

end

describe 'Nanoc::Site#initialize' do
Expand Down