Skip to content

Commit

Permalink
Merge pull request #1084 from nanoc/snapshot-def-binariness
Browse files Browse the repository at this point in the history
Record snapshot definition binary-ness
  • Loading branch information
denisdefreyne committed Feb 16, 2017
2 parents f15e979 + 075c3f1 commit 14b8853
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 32 deletions.
10 changes: 8 additions & 2 deletions lib/nanoc/base/entities/snapshot_def.rb
Expand Up @@ -4,10 +4,16 @@ class SnapshotDef
include Nanoc::Int::ContractsSupport

attr_reader :name
attr_reader :binary

contract Symbol => C::Any
def initialize(name)
contract Symbol, C::KeywordArgs[binary: C::Optional[C::Bool]] => C::Any
def initialize(name, binary:)
@name = name
@binary = binary
end

def binary?
@binary
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/nanoc/base/views/item_rep_view.rb
Expand Up @@ -80,7 +80,9 @@ def raw_path(snapshot: :last)

# @api private
def binary?
@context.snapshot_repo.raw_compiled_content(rep: unwrap, snapshot: :last).binary?
snapshot_def = unwrap.snapshot_defs.find { |sd| sd.name == :last }
raise Nanoc::Int::Errors::NoSuchSnapshot.new(unwrap, :last) if snapshot_def.nil?
snapshot_def.binary?
end

def inspect
Expand Down
14 changes: 12 additions & 2 deletions lib/nanoc/rule_dsl/rule_memory_calculator.rb
Expand Up @@ -55,9 +55,19 @@ def [](obj)
end

def snapshots_defs_for(rep)
self[rep].snapshot_actions.map do |a|
Nanoc::Int::SnapshotDef.new(a.snapshot_name)
is_binary = rep.item.content.binary?
snapshot_defs = []

self[rep].each do |action|
case action
when Nanoc::Int::ProcessingActions::Snapshot
snapshot_defs << Nanoc::Int::SnapshotDef.new(action.snapshot_name, binary: is_binary)
when Nanoc::Int::ProcessingActions::Filter
is_binary = Nanoc::Filter.named!(action.filter_name).to_binary?
end
end

snapshot_defs
end

# @param [Nanoc::Int::ItemRep] rep The item representation to get the rule
Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc/spec.rb
Expand Up @@ -146,7 +146,7 @@ def memory_for(obj)
end

def snapshots_defs_for(_rep)
[Nanoc::Int::SnapshotDef.new(:last)]
[Nanoc::Int::SnapshotDef.new(:last, binary: false)]
end
end.new(self)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/nanoc/base/compiler_spec.rb
Expand Up @@ -62,7 +62,7 @@
reps << other_rep

reps.each do |rep|
rep.snapshot_defs << Nanoc::Int::SnapshotDef.new(:last)
rep.snapshot_defs << Nanoc::Int::SnapshotDef.new(:last, binary: false)
end

allow(outdatedness_checker).to receive(:outdated?).with(rep).and_return(true)
Expand Down
2 changes: 1 addition & 1 deletion spec/nanoc/base/entities/item_rep_spec.rb
Expand Up @@ -8,7 +8,7 @@
let(:snapshot_name) { raise 'override me' }

before do
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(:donkey)]
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(:donkey, binary: false)]
end

context 'snapshot does not exist' do
Expand Down
8 changes: 4 additions & 4 deletions spec/nanoc/base/repos/snapshot_repo_spec.rb
Expand Up @@ -103,7 +103,7 @@

context 'snapshot def exists' do
before do
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(snapshot_name)]
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(snapshot_name, binary: false)]
repo.set_all(rep, snapshot_name => content)
end

Expand All @@ -128,7 +128,7 @@

context 'snapshot def exists, but not content' do
before do
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(snapshot_name)]
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(snapshot_name, binary: false)]
repo.set_all(rep, {})
end

Expand All @@ -148,7 +148,7 @@
context 'snapshot exists' do
context 'snapshot is not final' do
before do
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(snapshot_name)]
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(snapshot_name, binary: false)]
end

context 'snapshot content does not exist' do
Expand Down Expand Up @@ -209,7 +209,7 @@

context 'snapshot is final' do
before do
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(snapshot_name)]
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(snapshot_name, binary: false)]
end

context 'snapshot content does not exist' do
Expand Down
6 changes: 3 additions & 3 deletions spec/nanoc/base/services/compiler/stages/compile_reps_spec.rb
Expand Up @@ -67,7 +67,7 @@
reps << other_rep

reps.each do |rep|
rep.snapshot_defs << Nanoc::Int::SnapshotDef.new(:last)
rep.snapshot_defs << Nanoc::Int::SnapshotDef.new(:last, binary: false)
end

allow(action_provider).to receive(:memory_for).with(rep).and_return(memory)
Expand All @@ -83,11 +83,11 @@
end

let(:snapshot_defs_for_rep) do
[Nanoc::Int::SnapshotDef.new(:last)]
[Nanoc::Int::SnapshotDef.new(:last, binary: false)]
end

let(:snapshot_defs_for_other_rep) do
[Nanoc::Int::SnapshotDef.new(:last)]
[Nanoc::Int::SnapshotDef.new(:last, binary: false)]
end

context 'rep not in outdatedness store' do
Expand Down
2 changes: 1 addition & 1 deletion spec/nanoc/base/services/executor_spec.rb
Expand Up @@ -414,7 +414,7 @@ def run(_content, params = {})
end

before do
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(:pre)]
rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(:pre, binary: false)]

snapshot_repo.set(rep, :last, content)

Expand Down
38 changes: 36 additions & 2 deletions spec/nanoc/base/views/item_rep_view_spec.rb
Expand Up @@ -146,7 +146,7 @@
Nanoc::Int::ItemRep.new(item, :default).tap do |ir|
ir.compiled = true
ir.snapshot_defs = [
Nanoc::Int::SnapshotDef.new(:last),
Nanoc::Int::SnapshotDef.new(:last, binary: false),
]
end
end
Expand Down Expand Up @@ -213,7 +213,7 @@
Nanoc::Int::ItemRep.new(item, :default).tap do |ir|
ir.compiled = true
ir.snapshot_defs = [
Nanoc::Int::SnapshotDef.new(:last),
Nanoc::Int::SnapshotDef.new(:last, binary: false),
]
end
end
Expand Down Expand Up @@ -317,6 +317,40 @@
it { should eq('output/about/index.html') }
end

describe '#binary?' do
let(:item_rep) { Nanoc::Int::ItemRep.new(item, :jacques) }
let(:item) { Nanoc::Int::Item.new('asdf', {}, '/foo/') }
let(:view) { described_class.new(item_rep, view_context) }

subject { view.binary? }

context 'no :last snapshot' do
before do
item_rep.snapshot_defs = []
end

it 'raises' do
expect { subject }.to raise_error(Nanoc::Int::Errors::NoSuchSnapshot)
end
end

context ':last snapshot is textual' do
before do
item_rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(:last, binary: false)]
end

it { is_expected.not_to be }
end

context ':last snapshot is binary' do
before do
item_rep.snapshot_defs = [Nanoc::Int::SnapshotDef.new(:last, binary: true)]
end

it { is_expected.to be }
end
end

describe '#item' do
let(:item_rep) { Nanoc::Int::ItemRep.new(item, :jacques) }
let(:item) { Nanoc::Int::Item.new('asdf', {}, '/foo/') }
Expand Down
8 changes: 4 additions & 4 deletions spec/nanoc/base/views/item_view_spec.rb
Expand Up @@ -214,10 +214,10 @@
Nanoc::Int::ItemRep.new(item, :default).tap do |ir|
ir.compiled = true
ir.snapshot_defs = [
Nanoc::Int::SnapshotDef.new(:last),
Nanoc::Int::SnapshotDef.new(:pre),
Nanoc::Int::SnapshotDef.new(:post),
Nanoc::Int::SnapshotDef.new(:specific),
Nanoc::Int::SnapshotDef.new(:last, binary: false),
Nanoc::Int::SnapshotDef.new(:pre, binary: false),
Nanoc::Int::SnapshotDef.new(:post, binary: false),
Nanoc::Int::SnapshotDef.new(:specific, binary: false),
]
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/nanoc/regressions/gh_1082c_spec.rb
@@ -0,0 +1,19 @@
describe 'GH-1082', site: true, stdio: true do
before do
File.write('content/a.erb', '<%= @items["/b.*"].reps[:default].binary? %>')
File.write('content/b.erb', '<%= @items["/a.*"].reps[:default].binary? %>')

File.write('Rules', <<EOS)
compile '/*' do
filter :erb
write item.identifier.without_ext + '.txt'
end
EOS
end

it 'does not require any items to be compiled' do
Nanoc::CLI.run(%w(compile))
expect(File.read('output/a.txt')).to eql('false')
expect(File.read('output/b.txt')).to eql('false')
end
end
17 changes: 17 additions & 0 deletions spec/nanoc/regressions/gh_1082d_spec.rb
@@ -0,0 +1,17 @@
describe 'GH-1082', site: true, stdio: true do
before do
File.write('content/a.erb', '<%= @item.reps[:default].binary? %>')

File.write('Rules', <<EOS)
compile '/*' do
filter :erb
write item.identifier.without_ext + '.txt'
end
EOS
end

it 'does not require any items to be compiled' do
Nanoc::CLI.run(%w(compile))
expect(File.read('output/a.txt')).to eql('false')
end
end
32 changes: 22 additions & 10 deletions spec/nanoc/rule_dsl/rule_memory_calculator_spec.rb
Expand Up @@ -215,31 +215,43 @@
let(:view_context) { double(:view_context) }

before do
Class.new(Nanoc::Filter) do
identifier :rule_memory_calculator_spec_snapshot_def_test
type text: :binary

def run(content, params = {})
# …
end
end

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

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

example do
expect(subject[0]).to be_a(Nanoc::Int::SnapshotDef)
expect(subject[0].name).to eql(:raw)
it 'creates snapshot defs' do
expect(subject.size).to eql(4)
expect(subject).to all(be_a(Nanoc::Int::SnapshotDef))
end

expect(subject[1]).to be_a(Nanoc::Int::SnapshotDef)
it 'has the right names' do
expect(subject[0].name).to eql(:raw)
expect(subject[1].name).to eql(:pre)

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

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

expect(subject.size).to eql(4)
it 'has the right binary-ness' do
expect(subject[0]).not_to be_binary
expect(subject[1]).not_to be_binary
expect(subject[2]).to be_binary
expect(subject[3]).to be_binary
end
end
end

0 comments on commit 14b8853

Please sign in to comment.