Skip to content

Commit

Permalink
Allow calling #write more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Feb 17, 2017
1 parent 5ea5cf5 commit bef6896
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/nanoc/rule_dsl/rule_context.rb
Expand Up @@ -74,7 +74,10 @@ def snapshot(snapshot_name, path: nil)
#
# @return [void]
def write(path)
snapshot(:last, path: path)
@_write_snapshot_counter ||= 0
snapshot_name = "_#{@_write_snapshot_counter}".to_sym
@_write_snapshot_counter += 1
snapshot(snapshot_name, path: path)
end
end
end
29 changes: 29 additions & 0 deletions spec/nanoc/regressions/gh_1037_spec.rb
@@ -0,0 +1,29 @@
describe 'GH-1037', site: true, stdio: true do
before do
File.write('content/giraffe.md', 'I am a giraffe!')
File.write('content/donkey.erb', '[<%= @items["/giraffe.*"].compiled_content(snapshot: :last) %>]')

File.write('Rules', <<EOS)
compile '/donkey.erb' do
filter :erb
write '/donkey.txt'
end
compile '/giraffe.*' do
write '/giraffe.txt'
write '/giraffe.md'
end
EOS
end

it 'writes two files' do
Nanoc::CLI.run(%w(compile))
expect(File.read('output/giraffe.txt')).to eql('I am a giraffe!')
expect(File.read('output/giraffe.md')).to eql('I am a giraffe!')
end

it 'has the right :last snapshot' do
Nanoc::CLI.run(%w(compile))
expect(File.read('output/donkey.txt')).to eql('[I am a giraffe!]')
end
end
23 changes: 18 additions & 5 deletions spec/nanoc/rule_dsl/rule_context_spec.rb
Expand Up @@ -162,13 +162,26 @@
end

describe '#write' do
subject { rule_context.write(path) }
context 'calling once' do
subject { rule_context.write('/foo.html') }

let(:path) { '/foo.html' }
it 'makes a request to the executor' do
expect(executor).to receive(:snapshot).with(:_0, path: '/foo.html')
subject
end
end

it 'makes a request to the executor' do
expect(executor).to receive(:snapshot).with(:last, path: '/foo.html')
subject
context 'calling twice' do
subject do
rule_context.write('/foo.html')
rule_context.write('/bar.html')
end

it 'makes two requests to the executor with unique snapshot names' do
expect(executor).to receive(:snapshot).with(:_0, path: '/foo.html')
expect(executor).to receive(:snapshot).with(:_1, path: '/bar.html')
subject
end
end
end
end

0 comments on commit bef6896

Please sign in to comment.