require 'rubygems' require 'fileutils' require 'radius' require 'log4r' require 'find' require 'tmpdir' require_relative '../lib/depengine/processor/tags' require_relative '../lib/depengine/processor/template' require_relative '../lib/depengine/log/log' require_relative '../lib/depengine/dsl/template' require_relative '../lib/depengine/helper/validations' $log = Log::DeploymentLogger.new $log.writer.level = Log4r::ERROR describe 'the template mechanism' do context 'given a string' do it 'should render a template' do result = Processor::Template.parse('>Template< >Content<', {}) expect(result).to include('>Template< >Content<') end end context 'given a single file as source' do it 'should render a template' do result = Processor::Template.parse('spec/templates/single.tpl', {}) expect(result).to include('>Template< >Content<') end it 'should create Tags from the content hash and provide it to the template' do content = { 'parameter' => 'value' } result = Processor::Template.parse('spec/templates/single_hash.tpl', content) expect(result).to include('>Template< >value<') end it 'should honor the excludes' do tmp_dir = Dir.mktmpdir begin worker = Processor::Template.new worker.basepath = File.join(File.dirname(__FILE__), 'templates') worker.parse_template('base/sub1/single.tpl', {}, tmp_dir, excludes: ['single']) expect(File.file?(File.join(tmp_dir, 'base/sub1/single'))).to be false ensure FileUtils.remove_entry tmp_dir end end it 'should create a directory structure in analogy to the source tree' do tmp_dir = Dir.mktmpdir begin worker = Processor::Template.new worker.basepath = File.join(File.dirname(__FILE__), 'templates') worker.parse_template('base/sub1/single.tpl', {}, tmp_dir) expect(File.open(File.join(tmp_dir, 'base/sub1/single'), 'r').read).to include('>Template< >Content<') ensure FileUtils.remove_entry tmp_dir end end end context 'given a directory' do it 'should create a directory structure in analogy to the source tree' do tmp_dir = Dir.mktmpdir begin worker = Processor::Template.new worker.basepath = File.join(File.dirname(__FILE__), 'templates') worker.parse_template('base/sub2', {}, tmp_dir) expect(File.open(File.join(tmp_dir, 'base/sub2/multi2'), 'r').read).to include('>Template< >Content<') ensure FileUtils.remove_entry tmp_dir end end it 'should not use any excluded file' do tmp_dir = Dir.mktmpdir begin worker = Processor::Template.new worker.basepath = File.join(File.dirname(__FILE__), 'templates') worker.parse_template('base/sub2', {}, tmp_dir, excludes: ['multi2']) expect(File.file?(File.join(tmp_dir, 'base/sub2/multi2'))).to be false expect(File.file?(File.join(tmp_dir, 'base/sub2/multi1'))).to be true ensure FileUtils.remove_entry tmp_dir end end end end describe "the template DSL method 'parse_template'" do it 'calls the processor' do tmp_dir = Dir.mktmpdir begin hack_object = Class.new.class.class_eval('include Deployment::Methods::Template') hack_object.parse_template('base/sub1/single.tpl', tmp_dir, {}, basepath: File.join(File.dirname(__FILE__), 'templates')) ensure FileUtils.remove_entry tmp_dir end end end