From c8d0dba1cb137215f2431c3e220bfa7f2bd4f77d Mon Sep 17 00:00:00 2001 From: David Dollar Date: Mon, 27 Jun 2011 13:17:26 -0400 Subject: [PATCH] tweaks to template roots, add testing --- lib/foreman/export/base.rb | 7 +++---- lib/foreman/export/upstart.rb | 8 ++++---- spec/foreman/export/upstart_spec.rb | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/foreman/export/base.rb b/lib/foreman/export/base.rb index 304a81b..e771196 100644 --- a/lib/foreman/export/base.rb +++ b/lib/foreman/export/base.rb @@ -4,7 +4,6 @@ require "foreman/utils" class Foreman::Export::Base attr_reader :engine - attr_accessor :template def initialize(engine) @engine = engine @@ -24,11 +23,11 @@ private ###################################################################### puts "[foreman export] %s" % message end - def export_template(path, file) - if template and File.exist?(file_path = File.join(template, file)) + def export_template(exporter, file, template_root) + if template_root && File.exist?(file_path = File.join(template_root, file)) File.read(file_path) else - File.read(File.expand_path("../../../../data/export/#{path}/#{file}", __FILE__)) + File.read(File.expand_path("../../../../data/export/#{exporter}/#{file}", __FILE__)) end end diff --git a/lib/foreman/export/upstart.rb b/lib/foreman/export/upstart.rb index 1db930f..6e2979e 100644 --- a/lib/foreman/export/upstart.rb +++ b/lib/foreman/export/upstart.rb @@ -11,7 +11,7 @@ class Foreman::Export::Upstart < Foreman::Export::Base app = options[:app] || File.basename(engine.directory) user = options[:user] || app log_root = options[:log] || "/var/log/#{app}" - self.template = options[:template] + template_root = options[:template] Dir["#{location}/#{app}*.conf"].each do |file| say "cleaning up: #{file}" @@ -20,14 +20,14 @@ class Foreman::Export::Upstart < Foreman::Export::Base concurrency = Foreman::Utils.parse_concurrency(options[:concurrency]) - master_template = export_template("upstart", "master.conf.erb") + master_template = export_template("upstart", "master.conf.erb", template_root) master_config = ERB.new(master_template).result(binding) write_file "#{location}/#{app}.conf", master_config - process_template = export_template("upstart", "process.conf.erb") + process_template = export_template("upstart", "process.conf.erb", template_root) engine.processes.values.each do |process| - process_master_template = export_template("upstart", "process_master.conf.erb") + process_master_template = export_template("upstart", "process_master.conf.erb", template_root) process_master_config = ERB.new(process_master_template).result(binding) write_file "#{location}/#{app}-#{process.name}.conf", process_master_config diff --git a/spec/foreman/export/upstart_spec.rb b/spec/foreman/export/upstart_spec.rb index a632982..db746c0 100644 --- a/spec/foreman/export/upstart_spec.rb +++ b/spec/foreman/export/upstart_spec.rb @@ -21,4 +21,19 @@ describe Foreman::Export::Upstart do File.read("/tmp/init/app-bravo.conf").should == example_export_file("upstart/app-bravo.conf") File.read("/tmp/init/app-bravo-1.conf").should == example_export_file("upstart/app-bravo-1.conf") end + + context "with alternate templates" do + let(:template_root) { "/tmp/alternate" } + + before do + FileUtils.mkdir_p template_root + File.open("#{template_root}/master.conf.erb", "w") { |f| f.puts "alternate_template" } + end + + it "can export with alternate template files" do + upstart.export("/tmp/init", :template => template_root) + + File.read("/tmp/init/app.conf").should == "alternate_template\n" + end + end end