diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 8cdaf57..85498dc 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -79,14 +79,15 @@ private ###################################################################### end def engine - root = File.expand_path(File.dirname(procfile)) - env = File.expand_path(File.join(root, ".env")) - @engine ||= Foreman::Engine.new(procfile, options.merge({:app_root => root, - :env => env})) + @engine ||= Foreman::Engine.new(procfile, options) end def procfile - options[:procfile] || "Procfile" + case + when options[:procfile] then options[:procfile] + when options[:app_root] then File.expand_path(File.join(options[:app_root], "Procfile")) + else "Procfile" + end end def error(message) diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index 7ed568f..34b96f9 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -22,10 +22,12 @@ class Foreman::Engine def initialize(procfile, options={}) @procfile = Foreman::Procfile.new(procfile) - @directory = options[:app_root] + @directory = options[:app_root] || File.expand_path(File.dirname(procfile)) @options = options.dup - @environment = read_environment_files(options[:env]) @output_mutex = Mutex.new + + @options[:env] ||= default_env + @environment = read_environment_files(@options[:env]) end def start @@ -216,4 +218,10 @@ private ###################################################################### environment end + + def default_env + env = File.join(directory, ".env") + File.exists?(env) ? env : "" + end + end diff --git a/spec/foreman/cli_spec.rb b/spec/foreman/cli_spec.rb index 405b8a2..4473330 100644 --- a/spec/foreman/cli_spec.rb +++ b/spec/foreman/cli_spec.rb @@ -34,6 +34,15 @@ describe "Foreman::CLI", :fakefs do subject.start("alpha") end end + + describe "with an alternate root" do + it "reads the Procfile from that root" do + write_procfile "/some/app/Procfile" + mock(Foreman::Procfile).new("/some/app/Procfile") + mock.instance_of(Foreman::Engine).start + foreman %{ start -d /some/app } + end + end end describe "export" do diff --git a/spec/foreman/engine_spec.rb b/spec/foreman/engine_spec.rb index a7e8516..b070d42 100644 --- a/spec/foreman/engine_spec.rb +++ b/spec/foreman/engine_spec.rb @@ -49,6 +49,14 @@ describe "Foreman::Engine", :fakefs do end end + describe "directories" do + it "has the directory default relative to the Procfile" do + write_procfile "/some/app/Procfile" + engine = Foreman::Engine.new("/some/app/Procfile") + engine.directory.should == "/some/app" + end + end + describe "environment" do before(:each) do write_procfile @@ -97,6 +105,15 @@ describe "Foreman::Engine", :fakefs do engine.environment.should == {"FOO"=>"qoo"} engine.start end + + it "should be loaded relative to the Procfile" do + FileUtils.mkdir_p "/some/app" + File.open("/some/app/.env", "w") { |f| f.puts("FOO=qoo") } + write_procfile "/some/app/Procfile" + engine = Foreman::Engine.new("/some/app/Procfile") + engine.environment.should == {"FOO"=>"qoo"} + engine.start + end end describe "utf8" do