This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #4298 - njam:preserve-path, r=indirect
Refactor path/environment preserver, make it always preserve the PATH Closes #4251 @segiddins @indirect @RochesterinNYC wdyt?
- Loading branch information
Showing
12 changed files
with
176 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# frozen_string_literal: true | ||
module Bundler | ||
class EnvironmentPreserver | ||
# @param env [ENV] | ||
# @param keys [Array<String>] | ||
def initialize(env, keys) | ||
@original = env.to_hash | ||
@keys = keys | ||
@prefix = "BUNDLE_ORIG_" | ||
end | ||
|
||
# @return [Hash] | ||
def backup | ||
env = restore.clone | ||
@keys.each do |key| | ||
value = env[key] | ||
env[@prefix + key] = value unless value.nil? || value.empty? | ||
end | ||
env | ||
end | ||
|
||
# @return [Hash] | ||
def restore | ||
env = @original.clone | ||
@keys.each do |key| | ||
value_original = env[@prefix + key] | ||
unless value_original.nil? || value_original.empty? | ||
env[key] = value_original | ||
env.delete(@prefix + key) | ||
end | ||
end | ||
env | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
require "spec_helper" | ||
|
||
describe Bundler::EnvironmentPreserver do | ||
let(:preserver) { described_class.new(env, ["foo"]) } | ||
|
||
describe "#backup" do | ||
let(:env) { { "foo" => "my-foo", "bar" => "my-bar" } } | ||
subject { preserver.backup } | ||
|
||
it "should create backup entries" do | ||
expect(subject["BUNDLE_ORIG_foo"]).to eq("my-foo") | ||
end | ||
|
||
it "should keep the original entry" do | ||
expect(subject["foo"]).to eq("my-foo") | ||
end | ||
|
||
it "should not create backup entries for unspecified keys" do | ||
expect(subject.key?("BUNDLE_ORIG_bar")).to eq(false) | ||
end | ||
|
||
it "should not affect the original env" do | ||
subject | ||
expect(env.keys.sort).to eq(%w(bar foo)) | ||
end | ||
|
||
context "when a key is empty" do | ||
let(:env) { { "foo" => "" } } | ||
|
||
it "should not create backup entries" do | ||
expect(subject.key?("BUNDLE_ORIG_foo")).to eq(false) | ||
end | ||
end | ||
end | ||
|
||
describe "#restore" do | ||
subject { preserver.restore } | ||
|
||
context "when an original key is set" do | ||
let(:env) { { "foo" => "my-foo", "BUNDLE_ORIG_foo" => "orig-foo" } } | ||
|
||
it "should restore the original value" do | ||
expect(subject["foo"]).to eq("orig-foo") | ||
end | ||
|
||
it "should delete the backup value" do | ||
expect(subject.key?("BUNDLE_ORIG_foo")).to eq(false) | ||
end | ||
end | ||
|
||
context "when no original key is set" do | ||
let(:env) { { "foo" => "my-foo" } } | ||
|
||
it "should keep the current value" do | ||
expect(subject["foo"]).to eq("my-foo") | ||
end | ||
end | ||
|
||
context "when the original key is empty" do | ||
let(:env) { { "foo" => "my-foo", "BUNDLE_ORIG_foo" => "" } } | ||
|
||
it "should keep the current value" do | ||
expect(subject["foo"]).to eq("my-foo") | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.