Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gemspec
gem 'thor', '0.19.4', :require => false

group :test do
gem 'climate_control'
gem 'rake'
gem 'fakefs'
gem 'rspec', '~> 3.5'
Expand Down
6 changes: 6 additions & 0 deletions lib/foreman/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def is_thor_reserved_word?(word, type)
def start(process=nil)
check_procfile!
load_environment!
inherit_environment!
engine.load_procfile(procfile)
engine.options[:formation] = "#{process}=1" if process
engine.start
Expand Down Expand Up @@ -80,6 +81,7 @@ def check

def run(*args)
load_environment!
inherit_environment!

if File.file?(procfile)
engine.load_procfile(procfile)
Expand Down Expand Up @@ -134,6 +136,10 @@ def check_procfile!
error("#{procfile} does not exist.") unless File.file?(procfile)
end

def inherit_environment!
engine.inherit_env
end

def load_environment!
if options[:env]
options[:env].split(",").each do |file|
Expand Down
16 changes: 15 additions & 1 deletion lib/foreman/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,26 @@ def load_procfile(filename)
self
end

# Load ENV into the +env+ for this +Engine+
#
def inherit_env
merge_env(ENV)
end

# Load a .env file into the +env+ for this +Engine+
#
# @param [String] filename A .env file to load into the environment
#
def load_env(filename)
Foreman::Env.new(filename).entries do |name, value|
merge_env(Foreman::Env.new(filename).entries)
end

# Load a hash or entries list into the +env+ for this +Engine+
#
# @param [Hash] entries A Hash or an entries list to load into the environment
#
def merge_env(entries)
entries.each do |name, value|
@env[name] = value
end
end
Expand Down
7 changes: 0 additions & 7 deletions lib/foreman/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,4 @@ def initialize(filename)
ax
end
end

def entries
@entries.each do |key, value|
yield key, value
end
end

end
6 changes: 6 additions & 0 deletions spec/foreman/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
expect(forked_foreman("run -e #{resource_path(".env")} #{resource_path("bin/env FOO")}")).to eq("bar\n")
end

it "prefers ENV to .env" do
ClimateControl.modify FOO: 'qux' do
expect(forked_foreman("run -e #{resource_path(".env")} #{resource_path("bin/env FOO")}")).to eq("qux\n")
end
end

it "can run a command from the Procfile" do
expect(forked_foreman("run -f #{resource_path("Procfile")} test")).to eq("testing\n")
end
Expand Down
15 changes: 15 additions & 0 deletions spec/foreman/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ def shutdown
end

describe "environment" do
it "should read ENV" do
ClimateControl.modify FOO: 'baz' do
subject.inherit_env
expect(subject.env["FOO"]).to eq("baz")
end
end

it "should read env files" do
write_file("/tmp/env") { |f| f.puts("FOO=baz") }
subject.load_env("/tmp/env")
Expand All @@ -76,6 +83,14 @@ def shutdown
expect(subject.env["BAZ"]).to eq("qux")
end

it "should prefer later versions of values" do
write_file("/tmp/env1") { |f| f.puts("FOO=bar") }
write_file("/tmp/env2") { |f| f.puts("FOO=qux") }
subject.load_env "/tmp/env1"
subject.load_env "/tmp/env2"
expect(subject.env["FOO"]).to eq("qux")
end

it "should handle quoted values" do
write_file("/tmp/env") do |f|
f.puts 'FOO=bar'
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "pp"
require "fakefs/safe"
require "fakefs/spec_helpers"
require 'climate_control'

$:.unshift File.expand_path("../../lib", __FILE__)

Expand Down