test/base/test_compiler_dsl.rb in nanoc-4.0.2 vs test/base/test_compiler_dsl.rb in nanoc-4.1.0a1
- old
+ new
@@ -10,11 +10,11 @@
def test_layout
# TODO: implement
end
def test_preprocess_twice
- rules_collection = Nanoc::Int::RulesCollection.new(nil)
+ rules_collection = Nanoc::Int::RulesCollection.new
compiler_dsl = Nanoc::Int::CompilerDSL.new(rules_collection, {})
# first time
io = capturing_stdio do
compiler_dsl.preprocess {}
@@ -28,72 +28,146 @@
end
assert_empty io[:stdout]
assert_match(/WARNING: A preprocess block is already defined./, io[:stderr])
end
+ def test_postprocess_twice
+ rules_collection = Nanoc::Int::RulesCollection.new
+ compiler_dsl = Nanoc::Int::CompilerDSL.new(rules_collection, {})
+
+ # first time
+ io = capturing_stdio do
+ compiler_dsl.postprocess {}
+ end
+ assert_empty io[:stdout]
+ assert_empty io[:stderr]
+
+ # second time
+ io = capturing_stdio do
+ compiler_dsl.postprocess {}
+ end
+ assert_empty io[:stdout]
+ assert_match(/WARNING: A postprocess block is already defined./, io[:stderr])
+ end
+
def test_per_rules_file_preprocessor
# Create site
- Nanoc::CLI.run %w( create_site per-rules-file-preprocessor )
- FileUtils.cd('per-rules-file-preprocessor') do
- # Create rep
- item = Nanoc::Int::Item.new('foo', { extension: 'bar' }, '/foo/')
+ Nanoc::CLI.run %w( create_site foo )
+ FileUtils.cd('foo') do
+ # Create a bonus rules file
+ File.write(
+ 'more_rules.rb',
+ "preprocess { @items['/index.*'][:preprocessed] = true }")
+ # Adjust normal rules file
+ File.write(
+ 'Rules',
+ "include_rules 'more_rules'\n\npreprocess {}\n\n" + File.read('Rules'))
+
+ # Create site and compiler
+ site = Nanoc::Int::SiteLoader.new.new_from_cwd
+ compiler = Nanoc::Int::CompilerLoader.new.load(site)
+
+ # Check that the two preprocess blocks have been added
+ assert_equal 2, compiler.rules_collection.preprocessors.size
+ refute_nil compiler.rules_collection.preprocessors.first
+ refute_nil compiler.rules_collection.preprocessors.to_a.last
+
+ # Apply preprocess blocks
+ Nanoc::Int::Preprocessor
+ .new(site: site, rules_collection: compiler.rules_collection)
+ .run
+ assert site.items['/index.*'].attributes[:preprocessed]
+ end
+ end
+
+ def test_per_rules_file_postprocessor
+ # Create site
+ Nanoc::CLI.run %w( create_site foo )
+ FileUtils.cd('foo') do
# Create a bonus rules file
- File.open('more_rules.rb', 'w') { |io| io.write "preprocess { @items['/foo/'][:preprocessed] = true }" }
+ File.write(
+ 'more_rules.rb',
+ 'postprocess {}')
- # Create other necessary stuff
+ # Adjust normal rules file
+ File.write(
+ 'Rules',
+ "include_rules 'more_rules'\n\npostprocess {}\n\n" + File.read('Rules'))
+
+ # Create site and compiler
site = Nanoc::Int::SiteLoader.new.new_from_cwd
- site.items << item
- dsl = site.compiler.rules_collection.dsl
- io = capturing_stdio do
- dsl.preprocess {}
+ compiler = Nanoc::Int::CompilerLoader.new.load(site)
+
+ # Check that the two postprocess blocks have been added
+ assert_equal 2, compiler.rules_collection.postprocessors.size
+ refute_nil compiler.rules_collection.postprocessors.first
+ refute_nil compiler.rules_collection.postprocessors.to_a.last
+ end
+ end
+
+ def test_postprocessor_modified_method
+ with_site do |site|
+ # Create rules
+ File.open('Rules', 'w') do |io|
+ io.write <<EOS
+compile '*' do
+end
+route '*' do
+end
+postprocess do
+ puts @items.select(&:modified).length
+end
+EOS
end
- assert_empty io[:stdout]
- assert_empty io[:stderr]
- # Include rules
- dsl.include_rules 'more_rules'
+ File.open('content/index.html', 'w') { |io| io.write('o hello') }
- # Check that the two preprocess blocks have been added
- assert_equal 2, site.compiler.rules_collection.preprocessors.size
- refute_nil site.compiler.rules_collection.preprocessors.first
- refute_nil site.compiler.rules_collection.preprocessors.to_a.last
+ io = capturing_stdio do
+ site = Nanoc::Int::SiteLoader.new.new_from_cwd
+ site.compile
+ end
- # Apply preprocess blocks
- site.compiler.preprocess
- assert item.attributes[:preprocessed]
+ assert_match(/1/, io[:stdout])
end
end
def test_include_rules
# Create site
- Nanoc::CLI.run %w( create_site with_bonus_rules )
- FileUtils.cd('with_bonus_rules') do
- # Create rep
- item = Nanoc::Int::Item.new('foo', { extension: 'bar' }, '/foo/')
- rep = Nanoc::Int::ItemRep.new(item, :default)
-
+ Nanoc::CLI.run %w( create_site foo )
+ FileUtils.cd('foo') do
# Create a bonus rules file
- File.open('more_rules.rb', 'w') { |io| io.write "passthrough '/foo/'" }
+ File.write(
+ 'more_rules.rb',
+ "passthrough '/index.*'")
- # Create other necessary stuff
+ # Adjust normal rules file
+ File.write(
+ 'Rules',
+ "include_rules 'more_rules'\n\n" \
+ "route '/**/*' do ; nil ; end\n\n" \
+ "compile '/**/*' do ; end\n")
+
+ # Create site and compiler
site = Nanoc::Int::SiteLoader.new.new_from_cwd
- site.items << item
- dsl = site.compiler.rules_collection.dsl
+ compiler = Nanoc::Int::CompilerLoader.new.load(site)
+ compiler.build_reps
- # Include rules
- dsl.include_rules 'more_rules'
-
- # Check that the rule made it into the collection
- refute_nil site.compiler.rules_collection.routing_rule_for(rep)
+ # Check
+ rep = compiler.reps[site.items['/index.*']][0]
+ routing_rules = site.compiler.rules_collection.routing_rules_for(rep)
+ routing_rule = routing_rules[:last]
+ refute_nil routing_rule
+ assert_equal Nanoc::Int::StringPattern, routing_rule.pattern.class
+ assert_equal '/index.*', routing_rule.pattern.to_s
end
end
def test_passthrough
with_site do
# Create rules
File.open('Rules', 'w') do |io|
- io.write <<EOS
+ io.write <<-EOS
passthrough "/robots/"
compile '*' do ; end
route '*' do ; item.identifier.chop + '-xyz' + item[:extension] ; end
EOS