From f1b8018737d6542aebeddd1f0a4e310dffb05c97 Mon Sep 17 00:00:00 2001 From: Xiao Fan <2258776+NAR8789@users.noreply.github.com> Date: Tue, 8 May 2018 23:41:20 -0700 Subject: [PATCH 1/3] document existing behavior: prefer later-defined env vars --- spec/foreman/engine_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/foreman/engine_spec.rb b/spec/foreman/engine_spec.rb index 060b9638..6cb2ffb1 100644 --- a/spec/foreman/engine_spec.rb +++ b/spec/foreman/engine_spec.rb @@ -76,6 +76,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' From dba8a505f814f8d2d386af16f0392180e8d292a9 Mon Sep 17 00:00:00 2001 From: Xiao Fan <2258776+NAR8789@users.noreply.github.com> Date: Tue, 8 May 2018 23:41:58 -0700 Subject: [PATCH 2/3] inherit env from ENV, and prefer ENV to .env --- 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 + 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 67d697ab..7a074a06 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 2934458a..1b06165f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,6 +8,7 @@ require "pp" require "fakefs/safe" require "fakefs/spec_helpers" +require 'climate_control' $:.unshift File.expand_path("../../lib", __FILE__) From 2e6137a016ed6655e4881842d49ecd9a9cc2504c Mon Sep 17 00:00:00 2001 From: Xiao Fan <2258776+NAR8789@users.noreply.github.com> Date: Thu, 7 Dec 2023 14:30:42 -0800 Subject: [PATCH 3/3] explicitly declare climate_control dependency --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index 1316ac79..709950f7 100644 --- a/Gemfile +++ b/Gemfile @@ -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'