summaryrefslogtreecommitdiff
path: root/spec/runtime/with_clean_env_spec.rb
blob: eed024a39823d9fc883e437edf182ba583b0e9d1 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require "spec_helper"

describe "Bundler.with_env helpers" do
  shared_examples_for "Bundler.with_*_env" do
    it "should reset and restore the environment" do
      gem_path = ENV["GEM_PATH"]

      Bundler.with_clean_env do
        expect(`echo $GEM_PATH`.strip).not_to eq(gem_path)
      end

      expect(ENV["GEM_PATH"]).to eq(gem_path)
    end
  end

  around do |example|
    env = Bundler::ORIGINAL_ENV.dup
    Bundler::ORIGINAL_ENV["BUNDLE_PATH"] = "./Gemfile"
    example.run
    Bundler::ORIGINAL_ENV.replace env
  end

  describe "Bundler.with_clean_env" do
    it_should_behave_like "Bundler.with_*_env"

    it "should keep the original GEM_PATH even in sub processes" do
      gemfile ""
      bundle "install --path vendor/bundle"

      code = "Bundler.with_clean_env do;" \
             "  print ENV['GEM_PATH'] != '';" \
             "end"

      result = bundle "exec ruby -e #{code.inspect}"
      expect(result).to eq("true")
    end

    it "should not pass any bundler environment variables" do
      Bundler.with_clean_env do
        expect(`echo $BUNDLE_PATH`.strip).not_to eq("./Gemfile")
      end
    end

    it "should not pass RUBYOPT changes" do
      Bundler::ORIGINAL_ENV["RUBYOPT"] = " -rbundler/setup"

      Bundler.with_clean_env do
        expect(`echo $RUBYOPT`.strip).not_to include "-rbundler/setup"
      end

      expect(Bundler::ORIGINAL_ENV["RUBYOPT"]).to eq(" -rbundler/setup")
    end

    it "cleans RUBYLIB" do
      lib_path = File.expand_path("../../../lib", __FILE__)
      Bundler::ORIGINAL_ENV["RUBYLIB"] = lib_path

      Bundler.with_clean_env do
        expect(`echo $RUBYLIB`.strip).not_to include(lib_path)
      end
    end

    it "should not change ORIGINAL_ENV" do
      expect(Bundler::ORIGINAL_ENV["BUNDLE_PATH"]).to eq("./Gemfile")
    end
  end

  describe "Bundler.with_original_env" do
    it_should_behave_like "Bundler.with_*_env"

    it "should pass bundler environment variables set before Bundler was run" do
      Bundler.with_original_env do
        expect(`echo $BUNDLE_PATH`.strip).to eq("./Gemfile")
      end
    end
  end

  describe "Bundler.clean_system" do
    it "runs system inside with_clean_env" do
      Bundler.clean_system(%{echo 'if [ "$BUNDLE_PATH" = "" ]; then exit 42; else exit 1; fi' | /bin/sh})
      expect($?.exitstatus).to eq(42)
    end
  end

  describe "Bundler.clean_exec" do
    it "runs exec inside with_clean_env" do
      pid = Kernel.fork do
        Bundler.clean_exec(%{echo 'if [ "$BUNDLE_PATH" = "" ]; then exit 42; else exit 1; fi' | /bin/sh})
      end
      Process.wait(pid)
      expect($?.exitstatus).to eq(42)
    end
  end
end