Skip to content

Commit

Permalink
Merge branch 'release-4.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jul 24, 2016
2 parents d573212 + 684f81b commit 096432c
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 48 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -2,6 +2,8 @@ source 'https://rubygems.org'

gemspec

gem 'json', '~> 2.0'

group :devel do
gem 'contracts', '~> 0.14'
gem 'coveralls', require: false
Expand Down
3 changes: 1 addition & 2 deletions lib/nanoc/base/compilation/compiler.rb
Expand Up @@ -191,8 +191,7 @@ def compile_reps
end

# Find item reps to compile and compile them
outdated_reps = @reps.select { |r| outdatedness_checker.outdated?(r) }
selector = Nanoc::Int::ItemRepSelector.new(outdated_reps)
selector = Nanoc::Int::ItemRepSelector.new(@reps)
selector.each do |rep|
@stack = []
compile_rep(rep)
Expand Down
5 changes: 4 additions & 1 deletion lib/nanoc/extra/checking/checks/internal_links.rb
Expand Up @@ -15,7 +15,10 @@ def run
# TODO: de-duplicate this (duplicated in external links check)
filenames = output_filenames.select { |f| File.extname(f) == '.html' }
hrefs_with_filenames = ::Nanoc::Extra::LinkCollector.new(filenames, :internal).filenames_per_href
hrefs_with_filenames.each_pair do |href, fns|
resource_uris_with_filenames = ::Nanoc::Extra::LinkCollector.new(filenames, :internal).filenames_per_resource_uri

uris = hrefs_with_filenames.merge(resource_uris_with_filenames)
uris.each_pair do |href, fns|
fns.each do |filename|
next if valid?(href, filename)

Expand Down
4 changes: 4 additions & 0 deletions lib/nanoc/rule_dsl/recording_executor.rb
Expand Up @@ -22,6 +22,10 @@ def filter(_rep, filter_name, filter_args = {})
end

def layout(_rep, layout_identifier, extra_filter_args = {})
unless layout_identifier.is_a?(String)
raise ArgumentError.new('The layout passed to #layout must be a string')
end

unless @rule_memory.any_layouts?
@rule_memory.add_snapshot(:pre, true, nil)
end
Expand Down
24 changes: 21 additions & 3 deletions lib/nanoc/rule_dsl/rule_memory_calculator.rb
Expand Up @@ -12,6 +12,18 @@ def initialize(obj)
end
end

class NoRuleMemoryForLayoutException < ::Nanoc::Error
def initialize(layout)
super("There is no layout rule specified for #{layout.inspect}")
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 @@ -57,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 All @@ -83,7 +97,11 @@ def new_rule_memory_for_rep(rep)
# @return [Nanoc::Int::RuleMemory]
def new_rule_memory_for_layout(layout)
res = @rules_collection.filter_for_layout(layout)
# FIXME: what if res is nil?

unless res
raise NoRuleMemoryForLayoutException.new(layout)
end

Nanoc::Int::RuleMemory.new(layout).tap do |rm|
rm.add_filter(res[0], res[1])
end
Expand Down
4 changes: 4 additions & 0 deletions spec/nanoc/rule_dsl/recording_executor_spec.rb
Expand Up @@ -55,6 +55,10 @@
expect(executor.rule_memory[1].layout_identifier).to eql('/default.*')
expect(executor.rule_memory[1].params).to eql({ final: false })
end

it 'fails when passed a symbol' do
expect { executor.layout(rep, :default, final: false) }.to raise_error(ArgumentError)
end
end

describe '#snapshot' do
Expand Down
99 changes: 57 additions & 42 deletions spec/nanoc/rule_dsl/rule_memory_calculator_spec.rb
Expand Up @@ -21,66 +21,80 @@
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.size).to eql(8)

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[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[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[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[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[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[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[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::Layout)
expect(subject[4].layout_identifier).to eql('/default.*')
expect(subject[4].params).to be_nil
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::Filter)
expect(subject[5].filter_name).to eql(:typohero)
expect(subject[5].params).to eql({})
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(:post)
expect(subject[6]).to be_final
expect(subject[6].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
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

context 'with layout' do
let(:obj) { Nanoc::Int::Layout.new('content', {}, '/default.erb') }

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

context 'rule exists' do
Expand All @@ -102,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
15 changes: 15 additions & 0 deletions test/base/test_compiler.rb
Expand Up @@ -208,6 +208,21 @@ def test_disallow_duplicate_routes
end
end

def test_compile_should_recompile_all_reps
Nanoc::CLI.run %w(create_site bar)

FileUtils.cd('bar') do
Nanoc::CLI.run %w(compile)

site = Nanoc::Int::SiteLoader.new.new_from_cwd
site.compile

# At this point, even the already compiled items in the previous pass
# should have their compiled content assigned, so this should work:
site.compiler.reps[site.items['/index.*']][0].compiled_content
end
end

def test_disallow_multiple_snapshots_with_the_same_name
# Create site
Nanoc::CLI.run %w(create_site bar)
Expand Down
15 changes: 15 additions & 0 deletions test/extra/checking/checks/test_internal_links.rb
Expand Up @@ -16,6 +16,21 @@ def test_run
end
end

def test_resource_uris
with_site do |site|
# Create files
FileUtils.mkdir_p('output')
File.open('output/bar.html', 'w') { |io| io.write('<link rel="stylesheet" href="/styledinges.css">') }

# Create check
check = Nanoc::Extra::Checking::Checks::InternalLinks.create(site)
check.run

# Test
assert check.issues.size == 1
end
end

def test_valid?
with_site do |site|
# Create files
Expand Down

0 comments on commit 096432c

Please sign in to comment.