class Nanoc::Int::CompilerDSLTest < Nanoc::TestCase def test_compile # TODO: implement end def test_route # TODO: implement end def test_layout # TODO: implement end def test_preprocess_twice rules_collection = Nanoc::Int::RulesCollection.new compiler_dsl = Nanoc::Int::CompilerDSL.new(rules_collection, {}) # first time io = capturing_stdio do compiler_dsl.preprocess {} end assert_empty io[:stdout] assert_empty io[:stderr] # second time io = capturing_stdio do compiler_dsl.preprocess {} 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 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.write( 'more_rules.rb', 'postprocess {}') # 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 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 <" end # Compile site = Nanoc::Int::SiteLoader.new.new_from_cwd site.compile # Check paths assert_equal ['output/foo'], Dir['output/*'] assert_equal ['output/foo/index.html'], Dir['output/foo/*'] end end def test_passthrough_with_full_identifiers with_site do File.open('nanoc.yaml', 'w') do |io| io << 'string_pattern_type: legacy' << "\n" io << 'data_sources:' << "\n" io << ' -' << "\n" io << ' type: filesystem' << "\n" io << ' identifier_type: full' << "\n" end # Create rules File.open('Rules', 'w') do |io| io << 'passthrough \'*\'' end # Create items assert Dir['content/*'].empty? File.open('content/robots.txt', 'w') do |io| io.write 'Hello I am robots' end # Compile site = Nanoc::Int::SiteLoader.new.new_from_cwd site.compile # Check paths assert_equal ['output/robots.txt'], Dir['output/*'] end end def test_ignore with_site do # Create rules File.open('Rules', 'w') do |io| io.write <" end # Compile site = Nanoc::Int::SiteLoader.new.new_from_cwd site.compile # Check paths assert_equal ['output/foo'], Dir['output/*'] assert_equal ['output/foo/index.html'], Dir['output/foo/*'] end end def test_create_pattern_with_string_with_no_config compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, {}) err = assert_raises(Nanoc::Int::Errors::GenericTrivial) do compiler_dsl.create_pattern('/foo/*') end assert_equal 'Invalid string_pattern_type: ', err.message end def test_create_pattern_with_string_with_glob_string_pattern_type compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, { string_pattern_type: 'glob' }) pattern = compiler_dsl.create_pattern('/foo/*') assert pattern.match?('/foo/aaaa') refute pattern.match?('/foo/aaaa/') refute pattern.match?('/foo/a/a/a/a') end def test_create_pattern_with_regex compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, { string_pattern_type: 'glob' }) pattern = compiler_dsl.create_pattern(%r{\A/foo/a*/}) assert pattern.match?('/foo/aaaa/') end def test_create_pattern_with_string_with_unknown_string_pattern_type compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, { string_pattern_type: 'donkey' }) err = assert_raises(Nanoc::Int::Errors::GenericTrivial) do compiler_dsl.create_pattern('/foo/*') end assert_equal 'Invalid string_pattern_type: donkey', err.message end def test_identifier_to_regex_without_wildcards # Create compiler DSL compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('foo') } expected = %r{^/foo/$} assert_equal(expected.to_s, actual.to_s) assert_equal(expected.source, actual.source) assert_equal(expected.kcode, actual.kcode) if expected.respond_to?(:kcode) assert_equal(expected.casefold?, actual.casefold?) assert_equal(expected.options, actual.options) end def test_identifier_to_regex_with_one_wildcard # Create compiler DSL compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('foo/*/bar') } expected = %r{^/foo/(.*?)/bar/$} assert_equal(expected.to_s, actual.to_s) assert_equal(expected.source, actual.source) assert_equal(expected.kcode, actual.kcode) if expected.respond_to?(:kcode) assert_equal(expected.casefold?, actual.casefold?) assert_equal(expected.options, actual.options) end def test_identifier_to_regex_with_two_wildcards # Create compiler DSL compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('foo/*/bar/*/qux') } expected = %r{^/foo/(.*?)/bar/(.*?)/qux/$} assert_equal(expected.to_s, actual.to_s) assert_equal(expected.source, actual.source) assert_equal(expected.kcode, actual.kcode) if expected.respond_to?(:kcode) assert_equal(expected.casefold?, actual.casefold?) assert_equal(expected.options, actual.options) end def test_identifier_to_regex_with_just_one_wildcard # Create compiler DSL compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('*') } expected = %r{^/(.*?)$} assert_equal(expected.to_s, actual.to_s) assert_equal(expected.source, actual.source) assert_equal(expected.kcode, actual.kcode) if expected.respond_to?(:kcode) assert_equal(expected.casefold?, actual.casefold?) assert_equal(expected.options, actual.options) end def test_identifier_to_regex_with_root # Create compiler DSL compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('/') } expected = %r{^/$} assert_equal(expected.to_s, actual.to_s) assert_equal(expected.source, actual.source) assert_equal(expected.kcode, actual.kcode) if expected.respond_to?(:kcode) assert_equal(expected.casefold?, actual.casefold?) assert_equal(expected.options, actual.options) end def test_identifier_to_regex_with_only_children # Create compiler DSL compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('/foo/*/') } expected = %r{^/foo/(.*?)/$} assert_equal(expected.to_s, actual.to_s) assert_equal(expected.source, actual.source) assert_equal(expected.kcode, actual.kcode) if expected.respond_to?(:kcode) assert_equal(expected.casefold?, actual.casefold?) assert_equal(expected.options, actual.options) end def test_identifier_to_regex_with_plus_wildcard # Create compiler DSL compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, {}) actual = compiler_dsl.instance_eval { identifier_to_regex('/foo/+') } expected = %r{^/foo/(.+?)/$} assert_equal(expected.to_s, actual.to_s) assert_equal(expected.source, actual.source) assert_equal(expected.kcode, actual.kcode) if expected.respond_to?(:kcode) assert_equal(expected.casefold?, actual.casefold?) assert_equal(expected.options, actual.options) assert('/foo/bar/' =~ actual) refute('/foo/' =~ actual) end def test_dsl_has_no_access_to_compiler compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, {}) assert_raises(NameError) do compiler_dsl.instance_eval { compiler } end end def test_config $venetian = 'unsnares' compiler_dsl = Nanoc::Int::CompilerDSL.new(nil, { venetian: 'snares' }) compiler_dsl.instance_eval { $venetian = @config[:venetian] } assert_equal 'snares', $venetian end end