Skip to content

Commit

Permalink
Raise proper error when no compilation rule memory exists
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jul 24, 2016
1 parent 5b2e9a6 commit b255927
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 45 deletions.
12 changes: 10 additions & 2 deletions lib/nanoc/rule_dsl/rule_memory_calculator.rb
Expand Up @@ -18,6 +18,12 @@ def initialize(layout)
end
end

class NoRuleMemoryForItemRepException < ::Nanoc::Error
def initialize(item)
super("There is no compilation rule specified for #{item.inspect}")
end
end

# @api private
attr_accessor :rules_collection

Expand Down Expand Up @@ -63,14 +69,16 @@ def snapshots_defs_for(rep)
#
# @return [Nanoc::Int::RuleMemory]
def new_rule_memory_for_rep(rep)
# FIXME: What if #compilation_rule_for returns nil?

dependency_tracker = Nanoc::Int::DependencyTracker::Null.new
view_context = @site.compiler.create_view_context(dependency_tracker)

executor = Nanoc::RuleDSL::RecordingExecutor.new(rep, @rules_collection, @site)
rule = @rules_collection.compilation_rule_for(rep)

unless rule
raise NoRuleMemoryForItemRepException.new(rep)
end

executor.snapshot(rep, :raw)
executor.snapshot(rep, :pre, final: false)
rule.apply_to(rep, executor: executor, site: @site, view_context: view_context)
Expand Down
98 changes: 55 additions & 43 deletions spec/nanoc/rule_dsl/rule_memory_calculator_spec.rb
Expand Up @@ -21,58 +21,69 @@
let(:view_context) { double(:view_context) }

before do
rules_proc = proc do
filter :erb, speed: :over_9000
layout '/default.*'
filter :typohero
end
rule = Nanoc::RuleDSL::Rule.new(Nanoc::Int::Pattern.from('/list.*'), :csv, rules_proc)
rules_collection.add_item_compilation_rule(rule)

expect(compiler).to receive(:create_view_context).and_return(view_context)
end

example do
subject
context 'no rules exist' do
it 'raises error' do
error = Nanoc::RuleDSL::RuleMemoryCalculator::NoRuleMemoryForItemRepException
expect { subject }.to raise_error(error)
end
end

expect(subject.size).to eql(8)
context 'rules exist' do
before do
rules_proc = proc do
filter :erb, speed: :over_9000
layout '/default.*'
filter :typohero
end
rule = Nanoc::RuleDSL::Rule.new(Nanoc::Int::Pattern.from('/list.*'), :csv, rules_proc)
rules_collection.add_item_compilation_rule(rule)
end

example do
subject

expect(subject[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[0].snapshot_name).to eql(:raw)
expect(subject[0]).to be_final
expect(subject[0].path).to be_nil
expect(subject.size).to eql(8)

expect(subject[1]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[1].snapshot_name).to eql(:pre)
expect(subject[1]).not_to be_final
expect(subject[1].path).to be_nil
expect(subject[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[0].snapshot_name).to eql(:raw)
expect(subject[0]).to be_final
expect(subject[0].path).to be_nil

expect(subject[2]).to be_a(Nanoc::Int::RuleMemoryActions::Filter)
expect(subject[2].filter_name).to eql(:erb)
expect(subject[2].params).to eql({ speed: :over_9000 })
expect(subject[1]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[1].snapshot_name).to eql(:pre)
expect(subject[1]).not_to be_final
expect(subject[1].path).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[2]).to be_a(Nanoc::Int::RuleMemoryActions::Filter)
expect(subject[2].filter_name).to eql(:erb)
expect(subject[2].params).to eql({ speed: :over_9000 })

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[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[5]).to be_a(Nanoc::Int::RuleMemoryActions::Filter)
expect(subject[5].filter_name).to eql(:typohero)
expect(subject[5].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[6]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[6].snapshot_name).to eql(:post)
expect(subject[6]).to be_final
expect(subject[6].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[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
expect(subject[6]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
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
end

Expand All @@ -81,8 +92,8 @@

context 'no rules exist' do
it 'raises error' do
expect { subject }.to raise_error(
Nanoc::RuleDSL::RuleMemoryCalculator::NoRuleMemoryForLayoutException)
error = Nanoc::RuleDSL::RuleMemoryCalculator::NoRuleMemoryForLayoutException
expect { subject }.to raise_error(error)
end
end

Expand All @@ -105,7 +116,8 @@
let(:obj) { :donkey }

it 'errors' do
expect { subject }.to raise_error(Nanoc::RuleDSL::RuleMemoryCalculator::UnsupportedObjectTypeException)
error = Nanoc::RuleDSL::RuleMemoryCalculator::UnsupportedObjectTypeException
expect { subject }.to raise_error(error)
end
end
end
Expand Down

0 comments on commit b255927

Please sign in to comment.