Skip to content

Commit

Permalink
Merge pull request #764 from nanoc/fix-pre-snapshot
Browse files Browse the repository at this point in the history
Fix final :pre snapshot not being generated
  • Loading branch information
denisdefreyne committed Dec 11, 2015
2 parents a49f0a8 + 84f5ead commit 4742740
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 47 deletions.
2 changes: 1 addition & 1 deletion lib/nanoc/base/entities/rule_memory.rb
Expand Up @@ -24,7 +24,7 @@ def add_layout(layout_identifier, params)
end

def add_snapshot(snapshot_name, final, path)
will_add_snapshot(snapshot_name)
will_add_snapshot(snapshot_name) if final
@actions << Nanoc::Int::RuleMemoryActions::Snapshot.new(snapshot_name, final, path)
end

Expand Down
15 changes: 5 additions & 10 deletions lib/nanoc/rule_dsl/recording_executor.rb
@@ -1,12 +1,6 @@
module Nanoc
module RuleDSL
class RecordingExecutor
class NonFinalSnapshotWithPathError < ::Nanoc::Error
def initialize
super('This call to #snapshot specifies `final: false`, but it also specifies a path, which is an impossible combination.')
end
end

class PathWithoutInitialSlashError < ::Nanoc::Error
def initialize(rep, basic_path)
super("The path returned for the #{rep.inspect} item representation, “#{basic_path}”, does not start with a slash. Please ensure that all routing rules return a path that starts with a slash.")
Expand All @@ -28,14 +22,15 @@ def filter(_rep, filter_name, filter_args = {})
end

def layout(_rep, layout_identifier, extra_filter_args = {})
unless @rule_memory.any_layouts?
@rule_memory.add_snapshot(:pre, true, nil)
end

@rule_memory.add_layout(layout_identifier, extra_filter_args)
end

def snapshot(rep, snapshot_name, final: true, path: nil)
actual_path = path || basic_path_from_rules_for(rep, snapshot_name)
if !final && actual_path
raise NonFinalSnapshotWithPathError
end
actual_path = final ? (path || basic_path_from_rules_for(rep, snapshot_name)) : nil
@rule_memory.add_snapshot(snapshot_name, final, actual_path)
end

Expand Down
16 changes: 13 additions & 3 deletions spec/nanoc/base/entities/rule_memory_spec.rb
Expand Up @@ -70,13 +70,23 @@
end
end

context 'snapshot already exist' do
context 'non-final snapshot already exist' do
before do
rule_memory.add_snapshot(:before_layout, false, '/bar.md')
end

example do
expect { rule_memory.add_snapshot(:before_layout, false, '/foo.md') }
it 'does not raise' do
rule_memory.add_snapshot(:before_layout, true, '/foo.md')
end
end

context 'final snapshot already exist' do
before do
rule_memory.add_snapshot(:before_layout, true, '/bar.md')
end

it 'raises' do
expect { rule_memory.add_snapshot(:before_layout, true, '/foo.md') }
.to raise_error(Nanoc::Int::Errors::CannotCreateMultipleSnapshotsWithSameName)
end
end
Expand Down
49 changes: 37 additions & 12 deletions spec/nanoc/rule_dsl/recording_executor_spec.rb
Expand Up @@ -29,19 +29,31 @@
it 'records layout call without arguments' do
executor.layout(rep, '/default.*')

expect(executor.rule_memory.size).to eql(1)
expect(executor.rule_memory[0]).to be_a(Nanoc::Int::RuleMemoryActions::Layout)
expect(executor.rule_memory[0].layout_identifier).to eql('/default.*')
expect(executor.rule_memory[0].params).to eql({})
expect(executor.rule_memory.size).to eql(2)

expect(executor.rule_memory[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(executor.rule_memory[0].snapshot_name).to eql(:pre)
expect(executor.rule_memory[0]).to be_final
expect(executor.rule_memory[0].path).to be_nil

expect(executor.rule_memory[1]).to be_a(Nanoc::Int::RuleMemoryActions::Layout)
expect(executor.rule_memory[1].layout_identifier).to eql('/default.*')
expect(executor.rule_memory[1].params).to eql({})
end

it 'records layout call with arguments' do
executor.layout(rep, '/default.*', final: false)

expect(executor.rule_memory.size).to eql(1)
expect(executor.rule_memory[0]).to be_a(Nanoc::Int::RuleMemoryActions::Layout)
expect(executor.rule_memory[0].layout_identifier).to eql('/default.*')
expect(executor.rule_memory[0].params).to eql({ final: false })
expect(executor.rule_memory.size).to eql(2)

expect(executor.rule_memory[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(executor.rule_memory[0].snapshot_name).to eql(:pre)
expect(executor.rule_memory[0]).to be_final
expect(executor.rule_memory[0].path).to be_nil

expect(executor.rule_memory[1]).to be_a(Nanoc::Int::RuleMemoryActions::Layout)
expect(executor.rule_memory[1].layout_identifier).to eql('/default.*')
expect(executor.rule_memory[1].params).to eql({ final: false })
end
end

Expand Down Expand Up @@ -167,6 +179,7 @@

it 'records' do
subject

expect(executor.rule_memory.size).to eql(1)
expect(executor.rule_memory[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(executor.rule_memory[0].snapshot_name).to eql(:foo)
Expand All @@ -177,8 +190,14 @@
context 'explicit path given' do
let(:path) { '/routed-foo.html' }

it 'errors' do
expect { subject }.to raise_error(Nanoc::RuleDSL::RecordingExecutor::NonFinalSnapshotWithPathError)
it 'records without path' do
subject

expect(executor.rule_memory.size).to eql(1)
expect(executor.rule_memory[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(executor.rule_memory[0].snapshot_name).to eql(:foo)
expect(executor.rule_memory[0].path).to be_nil
expect(executor.rule_memory[0]).not_to be_final
end
end

Expand All @@ -202,8 +221,14 @@
allow(site).to receive(:config).and_return(double(:config))
end

it 'errors' do
expect { subject }.to raise_error(Nanoc::RuleDSL::RecordingExecutor::NonFinalSnapshotWithPathError)
it 'records without path' do
subject

expect(executor.rule_memory.size).to eql(1)
expect(executor.rule_memory[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(executor.rule_memory[0].snapshot_name).to eql(:foo)
expect(executor.rule_memory[0].path).to be_nil
expect(executor.rule_memory[0]).not_to be_final
end
end
end
Expand Down
39 changes: 24 additions & 15 deletions spec/nanoc/rule_dsl/rule_memory_calculator_spec.rb
Expand Up @@ -31,7 +31,7 @@
example do
subject

expect(subject.size).to eql(7)
expect(subject.size).to eql(8)

expect(subject[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[0].snapshot_name).to eql(:raw)
Expand All @@ -47,23 +47,28 @@
expect(subject[2].filter_name).to eql(:erb)
expect(subject[2].params).to eql({ speed: :over_9000 })

expect(subject[3]).to be_a(Nanoc::Int::RuleMemoryActions::Layout)
expect(subject[3].layout_identifier).to eql('/default.*')
expect(subject[3].params).to be_nil
expect(subject[3]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[3].snapshot_name).to eql(:pre)
expect(subject[3]).to be_final
expect(subject[3].path).to be_nil

expect(subject[4]).to be_a(Nanoc::Int::RuleMemoryActions::Filter)
expect(subject[4].filter_name).to eql(:typohero)
expect(subject[4].params).to eql({})
expect(subject[4]).to be_a(Nanoc::Int::RuleMemoryActions::Layout)
expect(subject[4].layout_identifier).to eql('/default.*')
expect(subject[4].params).to be_nil

expect(subject[5]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[5].snapshot_name).to eql(:post)
expect(subject[5]).to be_final
expect(subject[5].path).to be_nil
expect(subject[5]).to be_a(Nanoc::Int::RuleMemoryActions::Filter)
expect(subject[5].filter_name).to eql(:typohero)
expect(subject[5].params).to eql({})

expect(subject[6]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[6].snapshot_name).to eql(:last)
expect(subject[6].snapshot_name).to eql(:post)
expect(subject[6]).to be_final
expect(subject[6].path).to be_nil

expect(subject[7]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[7].snapshot_name).to eql(:last)
expect(subject[7]).to be_final
expect(subject[7].path).to be_nil
end
end

Expand Down Expand Up @@ -120,7 +125,7 @@
end

example do
expect(subject.size).to eql(4)
expect(subject.size).to eql(5)

expect(subject[0]).to be_a(Nanoc::Int::SnapshotDef)
expect(subject[0].name).to eql(:raw)
Expand All @@ -131,12 +136,16 @@
expect(subject[1]).not_to be_final

expect(subject[2]).to be_a(Nanoc::Int::SnapshotDef)
expect(subject[2].name).to eql(:post)
expect(subject[2].name).to eql(:pre)
expect(subject[2]).to be_final

expect(subject[3]).to be_a(Nanoc::Int::SnapshotDef)
expect(subject[3].name).to eql(:last)
expect(subject[3].name).to eql(:post)
expect(subject[3]).to be_final

expect(subject[4]).to be_a(Nanoc::Int::SnapshotDef)
expect(subject[4].name).to eql(:last)
expect(subject[4]).to be_final
end
end
end
10 changes: 4 additions & 6 deletions test/base/test_compiler.rb
Expand Up @@ -39,8 +39,6 @@ def test_compile_rep_should_write_proper_snapshots_real
File.write('content/moo.txt', '<%= 1 %> <%%= 2 %> <%%%= 3 %>')
File.write('layouts/default.erb', 'head <%= yield %> foot')

# FIXME: :pre is broken (it’s always non-final)

File.open('Rules', 'w') do |io|
io.write "compile '/**/*' do\n"
io.write " filter :erb\n"
Expand All @@ -53,10 +51,10 @@ def test_compile_rep_should_write_proper_snapshots_real
io.write " '/moo-raw.txt'\n"
io.write "end\n"
io.write "\n"
# io.write "route '/**/*', snapshot: :pre do\n"
# io.write " '/moo-pre.txt'\n"
# io.write "end\n"
# io.write "\n"
io.write "route '/**/*', snapshot: :pre do\n"
io.write " '/moo-pre.txt'\n"
io.write "end\n"
io.write "\n"
io.write "route '/**/*', snapshot: :post do\n"
io.write " '/moo-post.txt'\n"
io.write "end\n"
Expand Down

0 comments on commit 4742740

Please sign in to comment.