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 if item rep path does not start with slash #1016

Merged
merged 1 commit into from Dec 17, 2016
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
10 changes: 10 additions & 0 deletions lib/nanoc/base/services/item_rep_router.rb
Expand Up @@ -9,6 +9,12 @@ def initialize(output_path, rep_a, rep_b)
end
end

class RouteWithoutSlashError < ::Nanoc::Error
def initialize(output_path, rep)
super("The item representation #{rep.inspect} is routed to #{output_path}, which does not start with a slash, as required.")
end
end

def initialize(reps, action_provider, site)
@reps = reps
@action_provider = action_provider
Expand All @@ -29,6 +35,10 @@ def route_rep(rep, path, snapshot_name, paths_to_reps)
return if basic_path.nil?
basic_path = basic_path.encode('UTF-8')

unless basic_path.start_with?('/')
raise RouteWithoutSlashError.new(basic_path, rep)
end

# Check for duplicate paths
if paths_to_reps.key?(basic_path)
raise IdenticalRoutesError.new(basic_path, paths_to_reps[basic_path], rep)
Expand Down
8 changes: 8 additions & 0 deletions spec/nanoc/base/services/item_rep_router_spec.rb
Expand Up @@ -81,6 +81,14 @@
expect(paths_to_reps).to have_key('/foo/index.html')
end

context 'path does not start with a slash' do
let(:basic_path) { 'foo/index.html' }

it 'errors' do
expect { subject }.to raise_error(Nanoc::Int::ItemRepRouter::RouteWithoutSlashError)
end
end

context 'path is not UTF-8' do
let(:basic_path) { '/foo/index.html'.encode('ISO-8859-1') }

Expand Down
17 changes: 17 additions & 0 deletions spec/nanoc/regressions/gh_1015_spec.rb
@@ -0,0 +1,17 @@
describe 'GH-1015', site: true, stdio: true do
before do
File.write('content/foo.md', 'I am foo!')

File.write('Rules', <<EOS)
compile '/foo.*' do
filter :erb, stuff: self
write 'foo.html'
end
EOS
end

it 'errors' do
expect { Nanoc::CLI.run(%w(compile --verbose)) }.to raise_exception(Nanoc::Int::ItemRepRouter::RouteWithoutSlashError)
expect(File.file?('outputfoo.html')).not_to be
end
end