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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
# frozen_string_literal: true
RSpec.describe "env helpers" do
def bundle_exec_ruby(args, options = {})
build_bundler_context options.dup
bundle "exec '#{Gem.ruby}' #{args}", options
end
def build_bundler_context(options = {})
bundle "config set path vendor/bundle", options.dup
gemfile "source 'https://gem.repo1'"
bundle "install", options
end
def run_bundler_script(env, script)
system(env, "ruby", "-I#{lib_dir}", "-rbundler", script.to_s)
end
describe "Bundler.original_env" do
it "should return the PATH present before bundle was activated" do
create_file("source.rb", <<-RUBY)
print Bundler.original_env["PATH"]
RUBY
path = `getconf PATH`.strip + "#{File::PATH_SEPARATOR}/foo"
with_path_as(path) do
bundle_exec_ruby(bundled_app("source.rb").to_s)
expect(last_command.stdboth).to eq(path)
end
end
it "should return the GEM_PATH present before bundle was activated" do
create_file("source.rb", <<-RUBY)
print Bundler.original_env['GEM_PATH']
RUBY
gem_path = ENV["GEM_PATH"] + "#{File::PATH_SEPARATOR}/foo"
with_gem_path_as(gem_path) do
bundle_exec_ruby(bundled_app("source.rb").to_s)
expect(last_command.stdboth).to eq(gem_path)
end
end
it "works with nested bundle exec invocations", :ruby_repo do
create_file("exe.rb", <<-'RUBY')
count = ARGV.first.to_i
exit if count < 0
STDERR.puts "#{count} #{ENV["PATH"].end_with?("#{File::PATH_SEPARATOR}/foo")}"
if count == 2
ENV["PATH"] = "#{ENV["PATH"]}#{File::PATH_SEPARATOR}/foo"
end
exec(Gem.ruby, __FILE__, (count - 1).to_s)
RUBY
path = `getconf PATH`.strip + File::PATH_SEPARATOR + File.dirname(Gem.ruby)
with_path_as(path) do
build_bundler_context
bundle_exec_ruby("#{bundled_app("exe.rb")} 2")
end
expect(err).to eq <<-EOS.strip
2 false
1 true
0 true
EOS
end
it "removes variables that bundler added", :ruby_repo do
# Simulate bundler has not yet been loaded
ENV.replace(ENV.to_hash.delete_if {|k, _v| k.start_with?(Bundler::EnvironmentPreserver::BUNDLER_PREFIX) })
original = ruby('puts ENV.to_a.map {|e| e.join("=") }.sort.join("\n")', artifice: "fail")
create_file("source.rb", <<-RUBY)
puts Bundler.original_env.to_a.map {|e| e.join("=") }.sort.join("\n")
RUBY
bundle_exec_ruby bundled_app("source.rb"), artifice: "fail"
expect(out).to eq original
end
end
shared_examples_for "an unbundling helper" do
it "should delete BUNDLE_PATH" do
create_file("source.rb", <<-RUBY)
print #{modified_env}.has_key?('BUNDLE_PATH')
RUBY
ENV["BUNDLE_PATH"] = "./foo"
bundle_exec_ruby bundled_app("source.rb")
expect(last_command.stdboth).to include "false"
end
it "should remove absolute path to 'bundler/setup' from RUBYOPT even if it was present in original env" do
create_file("source.rb", <<-RUBY)
print #{modified_env}['RUBYOPT']
RUBY
setup_require = "-r#{lib_dir}/bundler/setup"
ENV["BUNDLER_ORIG_RUBYOPT"] = "-W2 #{setup_require} #{ENV["RUBYOPT"]}"
bundle_exec_ruby bundled_app("source.rb")
expect(last_command.stdboth).not_to include(setup_require)
end
it "should remove relative path to 'bundler/setup' from RUBYOPT even if it was present in original env" do
create_file("source.rb", <<-RUBY)
print #{modified_env}['RUBYOPT']
RUBY
ENV["BUNDLER_ORIG_RUBYOPT"] = "-W2 -rbundler/setup #{ENV["RUBYOPT"]}"
bundle_exec_ruby bundled_app("source.rb")
expect(last_command.stdboth).not_to include("-rbundler/setup")
end
it "should delete BUNDLER_SETUP even if it was present in original env" do
create_file("source.rb", <<-RUBY)
print #{modified_env}.has_key?('BUNDLER_SETUP')
RUBY
ENV["BUNDLER_ORIG_BUNDLER_SETUP"] = system_gem_path("gems/bundler-#{Bundler::VERSION}/lib/bundler/setup").to_s
bundle_exec_ruby bundled_app("source.rb")
expect(last_command.stdboth).to include "false"
end
it "should restore RUBYLIB", :ruby_repo do
create_file("source.rb", <<-RUBY)
print #{modified_env}['RUBYLIB']
RUBY
ENV["RUBYLIB"] = lib_dir.to_s + File::PATH_SEPARATOR + "/foo"
ENV["BUNDLER_ORIG_RUBYLIB"] = lib_dir.to_s + File::PATH_SEPARATOR + "/foo-original"
bundle_exec_ruby bundled_app("source.rb")
expect(last_command.stdboth).to include("/foo-original")
end
it "should restore the original MANPATH" do
create_file("source.rb", <<-RUBY)
print #{modified_env}['MANPATH']
RUBY
ENV["MANPATH"] = "/foo"
ENV["BUNDLER_ORIG_MANPATH"] = "/foo-original"
bundle_exec_ruby bundled_app("source.rb")
expect(last_command.stdboth).to include("/foo-original")
end
end
describe "Bundler.unbundled_env" do
let(:modified_env) { "Bundler.unbundled_env" }
it_behaves_like "an unbundling helper"
end
describe "Bundler.clean_env", bundler: 2 do
let(:modified_env) { "Bundler.clean_env" }
it_behaves_like "an unbundling helper"
end
describe "Bundler.with_original_env" do
it "should set ENV to original_env in the block" do
expected = Bundler.original_env
actual = Bundler.with_original_env { ENV.to_hash }
expect(actual).to eq(expected)
end
it "should restore the environment after execution" do
Bundler.with_original_env do
ENV["FOO"] = "hello"
end
expect(ENV).not_to have_key("FOO")
end
end
describe "Bundler.with_clean_env", bundler: 2 do
it "should set ENV to unbundled_env in the block" do
expected = Bundler.unbundled_env
actual = Bundler.ui.silence do
Bundler.with_clean_env { ENV.to_hash }
end
expect(actual).to eq(expected)
end
it "should restore the environment after execution" do
Bundler.ui.silence do
Bundler.with_clean_env { ENV["FOO"] = "hello" }
end
expect(ENV).not_to have_key("FOO")
end
end
describe "Bundler.with_unbundled_env" do
it "should set ENV to unbundled_env in the block" do
expected = Bundler.unbundled_env
actual = Bundler.with_unbundled_env { ENV.to_hash }
expect(actual).to eq(expected)
end
it "should restore the environment after execution" do
Bundler.with_unbundled_env do
ENV["FOO"] = "hello"
end
expect(ENV).not_to have_key("FOO")
end
end
describe "Bundler.original_system" do
before do
create_file("source.rb", <<-'RUBY')
Bundler.original_system("ruby", "-e", "exit(42) if ENV['BUNDLE_FOO'] == 'bar'")
exit $?.exitstatus
RUBY
end
it "runs system inside with_original_env" do
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
expect($?.exitstatus).to eq(42)
end
end
describe "Bundler.clean_system", bundler: 2 do
before do
create_file("source.rb", <<-'RUBY')
Bundler.ui.silence { Bundler.clean_system("ruby", "-e", "exit(42) unless ENV['BUNDLE_FOO'] == 'bar'") }
exit $?.exitstatus
RUBY
end
it "runs system inside with_clean_env" do
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
expect($?.exitstatus).to eq(42)
end
end
describe "Bundler.unbundled_system" do
before do
create_file("source.rb", <<-'RUBY')
Bundler.unbundled_system("ruby", "-e", "exit(42) unless ENV['BUNDLE_FOO'] == 'bar'")
exit $?.exitstatus
RUBY
end
it "runs system inside with_unbundled_env" do
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
expect($?.exitstatus).to eq(42)
end
end
describe "Bundler.original_exec" do
before do
create_file("source.rb", <<-'RUBY')
Process.fork do
exit Bundler.original_exec(%(test "\$BUNDLE_FOO" = "bar"))
end
_, status = Process.wait2
exit(status.exitstatus)
RUBY
end
it "runs exec inside with_original_env" do
skip "Fork not implemented" if Gem.win_platform?
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
expect($?.exitstatus).to eq(0)
end
end
describe "Bundler.clean_exec", bundler: 2 do
before do
create_file("source.rb", <<-'RUBY')
Process.fork do
exit Bundler.ui.silence { Bundler.clean_exec(%(test "\$BUNDLE_FOO" = "bar")) }
end
_, status = Process.wait2
exit(status.exitstatus)
RUBY
end
it "runs exec inside with_clean_env" do
skip "Fork not implemented" if Gem.win_platform?
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
expect($?.exitstatus).to eq(1)
end
end
describe "Bundler.unbundled_exec" do
before do
create_file("source.rb", <<-'RUBY')
Process.fork do
exit Bundler.unbundled_exec(%(test "\$BUNDLE_FOO" = "bar"))
end
_, status = Process.wait2
exit(status.exitstatus)
RUBY
end
it "runs exec inside with_clean_env" do
skip "Fork not implemented" if Gem.win_platform?
run_bundler_script({ "BUNDLE_FOO" => "bar" }, bundled_app("source.rb"))
expect($?.exitstatus).to eq(1)
end
end
end
|