Simplify subclassing by adding all arguments to the initializer. Also, clean up the method signatures for existing exporters.

This commit is contained in:
Chris Lowder
2012-01-24 15:10:18 +00:00
parent 4080b3f1f2
commit 1b701cddf3
10 changed files with 45 additions and 33 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ class Foreman::CLI < Thor
error "Unknown export format: #{format} (unable to load file 'foreman/export/#{ format.tr('-', '_') }')." error "Unknown export format: #{format} (unable to load file 'foreman/export/#{ format.tr('-', '_') }')."
end end
formatter.new(engine, options).export(location) formatter.new(location, engine, options).export
rescue Foreman::Export::Exception => ex rescue Foreman::Export::Exception => ex
error ex.message error ex.message
end end
+4 -2
View File
@@ -3,10 +3,12 @@ require "foreman/utils"
class Foreman::Export::Base class Foreman::Export::Base
attr_reader :engine, :app, :log, :port, :user, :template, :concurrency attr_reader :location, :engine, :options, :app, :log, :port, :user, :template, :concurrency
def initialize(engine, options={}) def initialize(location, engine, options={})
@location = location
@engine = engine @engine = engine
@options = options
@app = options[:app] @app = options[:app]
@log = options[:log] @log = options[:log]
@port = options[:port] @port = options[:port]
+1 -1
View File
@@ -3,7 +3,7 @@ require "foreman/export"
class Foreman::Export::Bluepill < Foreman::Export::Base class Foreman::Export::Bluepill < Foreman::Export::Base
def export(location, options={}) def export
error("Must specify a location") unless location error("Must specify a location") unless location
FileUtils.mkdir_p location FileUtils.mkdir_p location
+1 -1
View File
@@ -2,7 +2,7 @@ require "foreman/export"
class Foreman::Export::Inittab < Foreman::Export::Base class Foreman::Export::Inittab < Foreman::Export::Base
def export(fname=nil, options={}) def export
app = self.app || File.basename(engine.directory) app = self.app || File.basename(engine.directory)
user = self.user || app user = self.user || app
log_root = self.log || "/var/log/#{app}" log_root = self.log || "/var/log/#{app}"
+1 -1
View File
@@ -4,7 +4,7 @@ require "foreman/export"
class Foreman::Export::Runit < Foreman::Export::Base class Foreman::Export::Runit < Foreman::Export::Base
ENV_VARIABLE_REGEX = /([a-zA-Z_]+[a-zA-Z0-9_]*)=(\S+)/ ENV_VARIABLE_REGEX = /([a-zA-Z_]+[a-zA-Z0-9_]*)=(\S+)/
def export(location, options={}) def export
error("Must specify a location") unless location error("Must specify a location") unless location
app = self.app || File.basename(engine.directory) app = self.app || File.basename(engine.directory)
+1 -1
View File
@@ -3,7 +3,7 @@ require "foreman/export"
class Foreman::Export::Upstart < Foreman::Export::Base class Foreman::Export::Upstart < Foreman::Export::Base
def export(location, options={}) def export
error("Must specify a location") unless location error("Must specify a location") unless location
FileUtils.mkdir_p location FileUtils.mkdir_p location
+4 -4
View File
@@ -31,8 +31,8 @@ describe "Foreman::CLI", :fakefs do
write_procfile write_procfile
write_env("envfile") write_env("envfile")
mock_export = mock(Foreman::Export::Upstart) mock_export = mock(Foreman::Export::Upstart)
mock(Foreman::Export::Upstart).new(is_a(Foreman::Engine), { "env" => "envfile" }) { mock_export } mock(Foreman::Export::Upstart).new("/upstart", is_a(Foreman::Engine), { "env" => "envfile" }) { mock_export }
mock_export.export("/upstart") mock_export.export
foreman %{ export upstart /upstart --env envfile } foreman %{ export upstart /upstart --env envfile }
end end
end end
@@ -63,8 +63,8 @@ describe "Foreman::CLI", :fakefs do
it "runs successfully" do it "runs successfully" do
dont_allow(subject).error dont_allow(subject).error
mock_export = mock(Foreman::Export::Upstart) mock_export = mock(Foreman::Export::Upstart)
mock(Foreman::Export::Upstart).new(is_a(Foreman::Engine), {}) { mock_export } mock(Foreman::Export::Upstart).new("/tmp/foo", is_a(Foreman::Engine), {}) { mock_export }
mock_export.export("/tmp/foo") mock_export.export
subject.export("upstart", "/tmp/foo") subject.export("upstart", "/tmp/foo")
end end
end end
+11 -6
View File
@@ -5,20 +5,25 @@ require "tmpdir"
describe Foreman::Export::Bluepill, :fakefs do describe Foreman::Export::Bluepill, :fakefs do
let(:procfile) { FileUtils.mkdir_p("/tmp/app"); write_procfile("/tmp/app/Procfile") } let(:procfile) { FileUtils.mkdir_p("/tmp/app"); write_procfile("/tmp/app/Procfile") }
let(:engine) { Foreman::Engine.new(procfile) } let(:engine) { Foreman::Engine.new(procfile) }
let(:bluepill) { Foreman::Export::Bluepill.new(engine, :concurrency => "alpha=2") } let(:options) { Hash.new }
let(:bluepill) { Foreman::Export::Bluepill.new("/tmp/init", engine, options) }
before(:each) { load_export_templates_into_fakefs("bluepill") } before(:each) { load_export_templates_into_fakefs("bluepill") }
before(:each) { stub(bluepill).say } before(:each) { stub(bluepill).say }
it "exports to the filesystem" do it "exports to the filesystem" do
bluepill.export("/tmp/init") bluepill.export
normalize_space(File.read("/tmp/init/app.pill")).should == normalize_space(example_export_file("bluepill/app.pill")) normalize_space(File.read("/tmp/init/app.pill")).should == normalize_space(example_export_file("bluepill/app.pill"))
end end
it "exports to the filesystem with concurrency" do context "with concurrency" do
bluepill.export("/tmp/init", :concurrency => "alpha=2") let(:options) { Hash[:concurrency => "alpha=2"] }
normalize_space(File.read("/tmp/init/app.pill")).should == normalize_space(example_export_file("bluepill/app-concurrency.pill")) it "exports to the filesystem with concurrency" do
bluepill.export
normalize_space(File.read("/tmp/init/app.pill")).should == normalize_space(example_export_file("bluepill/app-concurrency.pill"))
end
end end
end end
+3 -3
View File
@@ -6,7 +6,7 @@ require "tmpdir"
describe Foreman::Export::Runit, :fakefs do describe Foreman::Export::Runit, :fakefs do
let(:procfile) { FileUtils.mkdir_p("/tmp/app"); write_procfile("/tmp/app/Procfile", 'bar=baz') } let(:procfile) { FileUtils.mkdir_p("/tmp/app"); write_procfile("/tmp/app/Procfile", 'bar=baz') }
let(:engine) { Foreman::Engine.new(procfile) } let(:engine) { Foreman::Engine.new(procfile) }
let(:runit) { Foreman::Export::Runit.new(engine, :concurrency => 'alpha=2') } let(:runit) { Foreman::Export::Runit.new('/tmp/init', engine, :concurrency => 'alpha=2,bravo=1') }
before(:each) { load_export_templates_into_fakefs("runit") } before(:each) { load_export_templates_into_fakefs("runit") }
before(:each) { stub(runit).say } before(:each) { stub(runit).say }
@@ -15,7 +15,7 @@ describe Foreman::Export::Runit, :fakefs do
it "exports to the filesystem" do it "exports to the filesystem" do
FileUtils.mkdir_p('/tmp/init') FileUtils.mkdir_p('/tmp/init')
runit.export('/tmp/init') runit.export
File.read("/tmp/init/app-alpha-1/run").should == example_export_file('runit/app-alpha-1-run') File.read("/tmp/init/app-alpha-1/run").should == example_export_file('runit/app-alpha-1-run')
File.read("/tmp/init/app-alpha-1/log/run").should == File.read("/tmp/init/app-alpha-1/log/run").should ==
@@ -36,6 +36,6 @@ describe Foreman::Export::Runit, :fakefs do
end end
it "creates a full path to the export directory" do it "creates a full path to the export directory" do
expect { runit.export('/tmp/init', :concurrency => "alpha=2,bravo=1") }.to_not raise_error(Errno::ENOENT) expect { runit.export }.to_not raise_error(Errno::ENOENT)
end end
end end
+18 -13
View File
@@ -5,14 +5,15 @@ require "tmpdir"
describe Foreman::Export::Upstart, :fakefs do describe Foreman::Export::Upstart, :fakefs do
let(:procfile) { FileUtils.mkdir_p("/tmp/app"); write_procfile("/tmp/app/Procfile") } let(:procfile) { FileUtils.mkdir_p("/tmp/app"); write_procfile("/tmp/app/Procfile") }
let(:engine) { Foreman::Engine.new(procfile) } let(:engine) { Foreman::Engine.new(procfile) }
let(:upstart) { Foreman::Export::Upstart.new(engine, :concurrency => "alpha=2") } let(:options) { Hash.new }
let(:upstart) { Foreman::Export::Upstart.new("/tmp/init", engine, options) }
before(:each) { load_export_templates_into_fakefs("upstart") } before(:each) { load_export_templates_into_fakefs("upstart") }
before(:each) { stub(upstart).say } before(:each) { stub(upstart).say }
it "exports to the filesystem" do it "exports to the filesystem" do
upstart.export("/tmp/init") upstart.export
File.read("/tmp/init/app.conf").should == example_export_file("upstart/app.conf") File.read("/tmp/init/app.conf").should == example_export_file("upstart/app.conf")
File.read("/tmp/init/app-alpha.conf").should == example_export_file("upstart/app-alpha.conf") File.read("/tmp/init/app-alpha.conf").should == example_export_file("upstart/app-alpha.conf")
@@ -21,19 +22,23 @@ describe Foreman::Export::Upstart, :fakefs do
File.read("/tmp/init/app-bravo-1.conf").should == example_export_file("upstart/app-bravo-1.conf") File.read("/tmp/init/app-bravo-1.conf").should == example_export_file("upstart/app-bravo-1.conf")
end end
it "exports to the filesystem with concurrency" do context "with concurrency" do
upstart.export("/tmp/init", :concurrency => "alpha=2") let(:options) { Hash[:concurrency => "alpha=2"] }
File.read("/tmp/init/app.conf").should == example_export_file("upstart/app.conf") it "exports to the filesystem with concurrency" do
File.read("/tmp/init/app-alpha.conf").should == example_export_file("upstart/app-alpha.conf") upstart.export
File.read("/tmp/init/app-alpha-1.conf").should == example_export_file("upstart/app-alpha-1.conf")
File.read("/tmp/init/app-alpha-2.conf").should == example_export_file("upstart/app-alpha-2.conf") File.read("/tmp/init/app.conf").should == example_export_file("upstart/app.conf")
File.exists?("/tmp/init/app-bravo-1.conf").should == false File.read("/tmp/init/app-alpha.conf").should == example_export_file("upstart/app-alpha.conf")
File.read("/tmp/init/app-alpha-1.conf").should == example_export_file("upstart/app-alpha-1.conf")
File.read("/tmp/init/app-alpha-2.conf").should == example_export_file("upstart/app-alpha-2.conf")
File.exists?("/tmp/init/app-bravo-1.conf").should == false
end
end end
context "with alternate templates" do context "with alternate templates" do
let(:template_root) { "/tmp/alternate" } let(:template_root) { "/tmp/alternate" }
let(:upstart) { Foreman::Export::Upstart.new(engine, :template => template_root) } let(:upstart) { Foreman::Export::Upstart.new("/tmp/init", engine, :template => template_root) }
before do before do
FileUtils.mkdir_p template_root FileUtils.mkdir_p template_root
@@ -41,7 +46,7 @@ describe Foreman::Export::Upstart, :fakefs do
end end
it "can export with alternate template files" do it "can export with alternate template files" do
upstart.export("/tmp/init") upstart.export
File.read("/tmp/init/app.conf").should == "alternate_template\n" File.read("/tmp/init/app.conf").should == "alternate_template\n"
end end
@@ -62,7 +67,7 @@ describe Foreman::Export::Upstart, :fakefs do
end end
it "can export with alternate template files" do it "can export with alternate template files" do
upstart.export("/tmp/init") upstart.export
File.read("/tmp/init/app.conf").should == "default_alternate_template\n" File.read("/tmp/init/app.conf").should == "default_alternate_template\n"
end end