summaryrefslogtreecommitdiff
path: root/spec/bundler/environment_preserver_spec.rb
blob: 530ca6f8356d7074ce7a30106113e497bd0f4ed0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true

RSpec.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["BUNDLER_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?("BUNDLER_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).not_to have_key "BUNDLER_ORIG_foo"
      end
    end

    context "when an original key is set" do
      let(:env) { { "foo" => "my-foo", "BUNDLER_ORIG_foo" => "orig-foo" } }

      it "should keep the original value in the BUNDLER_ORIG_ variable" do
        expect(subject["BUNDLER_ORIG_foo"]).to eq("orig-foo")
      end

      it "should keep the variable" do
        expect(subject["foo"]).to eq("my-foo")
      end
    end
  end

  describe "#restore" do
    subject { preserver.restore }

    context "when an original key is set" do
      let(:env) { { "foo" => "my-foo", "BUNDLER_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?("BUNDLER_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", "BUNDLER_ORIG_foo" => "" } }

      it "should keep the current value" do
        expect(subject["foo"]).to eq("my-foo")
      end
    end
  end
end