Skip to content

Commit

Permalink
Merge pull request #1016 from nanoc/gh-1015-route-without-leading-slash
Browse files Browse the repository at this point in the history
Raise error if item rep path does not start with slash
  • Loading branch information
denisdefreyne committed Dec 17, 2016
2 parents 6bf04d9 + 58a4a77 commit 9cb3877
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
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

0 comments on commit 9cb3877

Please sign in to comment.