diff --git a/lib/psenv/environment.rb b/lib/psenv/environment.rb index 33a5be7..ad2e01c 100644 --- a/lib/psenv/environment.rb +++ b/lib/psenv/environment.rb @@ -1,11 +1,11 @@ module Psenv class Environment < Hash def apply - each { |k, v| ENV.store(k, v) unless ENV.has_key?(k) } + each { |k, v| ENV.store(k.to_s, v) unless ENV.has_key?(k.to_s) } end def apply! - each { |k, v| ENV.store(k, v) } + each { |k, v| ENV.store(k.to_s, v) } end def self.create(*variables) diff --git a/spec/environment_spec.rb b/spec/environment_spec.rb new file mode 100644 index 0000000..2b29040 --- /dev/null +++ b/spec/environment_spec.rb @@ -0,0 +1,63 @@ +require "spec_helper" + +RSpec.describe Psenv::Environment do + let(:environment1) { { A: "1", B: "1" } } + let(:environment2) { { B: "2", C: "2" } } + + context ".create" do + subject { Psenv::Environment.create(environment1, environment2) } + + it "returns an environment object" do + expect(subject).to be_kind_of(Psenv::Environment) + end + + it "is also a hash" do + expect(subject).to be_kind_of(Hash) + end + + it "creates the correct environment" do + expected = { A: "1", B: "1", C: "2" } + expect(subject).to eq(expected) + end + end + + context "#apply" do + before(:each) do + ENV.store("A", "0") + environment = Psenv::Environment.create(environment1, environment2) + environment.apply + end + + it "does not overwrite existing environment" do + expect(ENV["A"]).to eq("0") + end + + it "applies unique variables as expected" do + expect(ENV["C"]).to eq("2") + end + + it "applies duplicates based on the first provided" do + expect(ENV["B"]).to eq("1") + end + end + + context "#apply!" do + before(:each) do + ENV.store("A", "0") + environment = Psenv::Environment.create(environment1, environment2) + environment.apply! + end + + it "does overwrite existing environment" do + expect(ENV["A"]).to eq("1") + end + + it "applies unique variables as expected" do + expect(ENV["C"]).to eq("2") + end + + it "applies duplicates based on the first provided" do + expect(ENV["B"]).to eq("1") + end + end +end