From 27a017e173560ff68385b886b4636ed2bd320ad0 Mon Sep 17 00:00:00 2001 From: Xiao Fan Date: Tue, 8 May 2018 23:41:58 -0700 Subject: [PATCH] inherit env from ENV, and prefer ENV to .env --- Gemfile | 1 + Gemfile.lock | 2 ++ lib/foreman/cli.rb | 6 ++++++ lib/foreman/engine.rb | 16 +++++++++++++++- lib/foreman/env.rb | 7 ------- spec/foreman/cli_spec.rb | 6 ++++++ spec/foreman/engine_spec.rb | 7 +++++++ spec/spec_helper.rb | 1 + 8 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index d57c4edc..b43bfd32 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,7 @@ group :test do gem "simplecov", :require => false gem 'timecop' gem "codeclimate-test-reporter", :require => false + gem 'climate_control' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 446f591b..d0d592e4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -12,6 +12,7 @@ GEM mime-types xml-simple builder (3.2.3) + climate_control (0.2.0) codeclimate-test-reporter (1.0.7) simplecov diff-lcs (1.3) @@ -61,6 +62,7 @@ PLATFORMS DEPENDENCIES aws-s3 + climate_control codeclimate-test-reporter fakefs (~> 0.10.0) foreman! diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 49f0377d..3ed03298 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -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 @@ -80,6 +81,7 @@ def check def run(*args) load_environment! + inherit_environment! if File.file?(procfile) engine.load_procfile(procfile) @@ -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| diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index a1316593..5fd392f1 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -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 diff --git a/lib/foreman/env.rb b/lib/foreman/env.rb index 378d2f7b..c76e59ad 100644 --- a/lib/foreman/env.rb +++ b/lib/foreman/env.rb @@ -19,11 +19,4 @@ def initialize(filename) ax end end - - def entries - @entries.each do |key, value| - yield key, value - end - end - end diff --git a/spec/foreman/cli_spec.rb b/spec/foreman/cli_spec.rb index b8c3cfee..500930dc 100644 --- a/spec/foreman/cli_spec.rb +++ b/spec/foreman/cli_spec.rb @@ -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 diff --git a/spec/foreman/engine_spec.rb b/spec/foreman/engine_spec.rb index 6cb2ffb1..ef1754ba 100644 --- a/spec/foreman/engine_spec.rb +++ b/spec/foreman/engine_spec.rb @@ -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") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 712313aa..44f96106 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,6 +18,7 @@ require "pp" require "fakefs/safe" require "fakefs/spec_helpers" +require 'climate_control' $:.unshift File.expand_path("../../lib", __FILE__)