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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
# frozen_string_literal: true
require "open3"
module Spec
module Helpers
def reset!
Dir.glob("#{tmp}/{gems/*,*}", File::FNM_DOTMATCH).each do |dir|
next if %w[base remote1 gems rubygems . ..].include?(File.basename(dir))
if ENV["BUNDLER_SUDO_TESTS"]
`sudo rm -rf "#{dir}"`
else
FileUtils.rm_rf(dir)
end
end
FileUtils.mkdir_p(home)
FileUtils.mkdir_p(tmpdir)
Bundler.reset!
Bundler.ui = nil
Bundler.ui # force it to initialize
end
def self.bang(method)
define_method("#{method}!") do |*args, &blk|
send(method, *args, &blk).tap do
unless last_command.success?
raise RuntimeError,
"Invoking #{method}!(#{args.map(&:inspect).join(", ")}) failed:\n#{last_command.stdboth}",
caller.drop_while {|bt| bt.start_with?(__FILE__) }
end
end
end
end
def the_bundle(*args)
TheBundle.new(*args)
end
def last_command
@command_executions.last || raise("There is no last command")
end
def out
last_command.stdout
end
def err
last_command.stderr
end
MAJOR_DEPRECATION = /^\[DEPRECATED\]\s*/.freeze
def err_without_deprecations
err.gsub(/#{MAJOR_DEPRECATION}.+[\n]?/, "")
end
def deprecations
err.split("\n").select {|l| l =~ MAJOR_DEPRECATION }.join("\n").split(MAJOR_DEPRECATION)
end
def exitstatus
last_command.exitstatus
end
def in_app_root(&blk)
Dir.chdir(bundled_app, &blk)
end
def in_app_root2(&blk)
Dir.chdir(bundled_app2, &blk)
end
def in_app_root_custom(root, &blk)
Dir.chdir(root, &blk)
end
def run(cmd, *args)
opts = args.last.is_a?(Hash) ? args.pop : {}
groups = args.map(&:inspect).join(", ")
setup = "require 'bundler' ; Bundler.setup(#{groups})\n"
ruby(setup + cmd, opts)
end
bang :run
def load_error_run(ruby, name, *args)
cmd = <<-RUBY
begin
#{ruby}
rescue LoadError => e
$stderr.puts "ZOMG LOAD ERROR" if e.message.include?("-- #{name}")
end
RUBY
opts = args.last.is_a?(Hash) ? args.pop : {}
args += [opts]
run(cmd, *args)
end
def spec
spec_dir.to_s
end
def bundle(cmd, options = {})
with_sudo = options.delete(:sudo)
sudo = with_sudo == :preserve_env ? "sudo -E" : "sudo" if with_sudo
bundle_bin = options.delete("bundle_bin") || bindir.join("bundle")
if system_bundler = options.delete(:system_bundler)
bundle_bin = system_gem_path.join("bin/bundler")
end
env = options.delete(:env) || {}
env["PATH"].gsub!("#{Path.root}/exe", "") if env["PATH"] && system_bundler
requires = options.delete(:requires) || []
requires << "support/hax"
artifice = options.delete(:artifice) do
if RSpec.current_example.metadata[:realworld]
"vcr"
else
"fail"
end
end
if artifice
requires << File.expand_path("../artifice/#{artifice}", __FILE__)
end
requires_str = requires.map {|r| "-r#{r}" }.join(" ")
load_path = []
load_path << lib unless system_bundler
load_path << spec
load_path_str = "-I#{load_path.join(File::PATH_SEPARATOR)}"
args = options.map do |k, v|
case v
when nil
next
when true
" --#{k}"
when false
" --no-#{k}"
else
" --#{k} #{v}"
end
end.join
cmd = "#{sudo} #{Gem.ruby} #{load_path_str} #{requires_str} #{bundle_bin} #{cmd}#{args}"
sys_exec(cmd, env) {|i, o, thr| yield i, o, thr if block_given? }
end
bang :bundle
def forgotten_command_line_options(options)
remembered = Bundler::VERSION.split(".", 2).first == "2"
options = options.map do |k, v|
v = '""' if v && v.to_s.empty?
[k, v]
end
return Hash[options] if remembered
options.each do |k, v|
if v.nil?
bundle! "config unset #{k}"
else
bundle! "config set --local #{k} #{v}"
end
end
{}
end
def bundler(cmd, options = {})
options["bundle_bin"] = bindir.join("bundler")
bundle(cmd, options)
end
def ruby(ruby, options = {})
env = (options.delete(:env) || {}).map {|k, v| "#{k}='#{v}' " }.join
ruby = ruby.gsub(/["`\$]/) {|m| "\\#{m}" }
lib_option = options[:no_lib] ? "" : " -I#{lib}"
sys_exec(%(#{env}#{Gem.ruby}#{lib_option} -e "#{ruby}"))
end
bang :ruby
def load_error_ruby(ruby, name, opts = {})
ruby(<<-R)
begin
#{ruby}
rescue LoadError => e
$stderr.puts "ZOMG LOAD ERROR"# if e.message.include?("-- #{name}")
end
R
end
def gembin(cmd)
old = ENV["RUBYOPT"]
ENV["RUBYOPT"] = "#{ENV["RUBYOPT"]} -I#{lib}"
cmd = bundled_app("bin/#{cmd}") unless cmd.to_s.include?("/")
sys_exec(cmd.to_s)
ensure
ENV["RUBYOPT"] = old
end
def gem_command(command, args = "")
sys_exec("#{Path.gem_bin} #{command} #{args}")
end
bang :gem_command
def rake
"#{Gem.ruby} -S #{ENV["GEM_PATH"]}/bin/rake"
end
def sys_exec(cmd, env = {})
command_execution = CommandExecution.new(cmd.to_s, Dir.pwd)
env = env.map {|k, v| [k.to_s, v.to_s] }.to_h # convert env keys and values to string
Open3.popen3(env, cmd.to_s) do |stdin, stdout, stderr, wait_thr|
yield stdin, stdout, wait_thr if block_given?
stdin.close
command_execution.stdout = Thread.new { stdout.read }.value.strip
command_execution.stderr = Thread.new { stderr.read }.value.strip
command_execution.exitstatus = wait_thr && wait_thr.value.exitstatus
end
(@command_executions ||= []) << command_execution
command_execution.stdout
end
bang :sys_exec
def config(config = nil, path = bundled_app(".bundle/config"))
return YAML.load_file(path) unless config
FileUtils.mkdir_p(File.dirname(path))
File.open(path, "w") do |f|
f.puts config.to_yaml
end
config
end
def global_config(config = nil)
config(config, home(".bundle/config"))
end
def create_file(*args)
path = bundled_app(args.shift)
path = args.shift if args.first.is_a?(Pathname)
str = args.shift || ""
path.dirname.mkpath
File.open(path.to_s, "w") do |f|
f.puts strip_whitespace(str)
end
end
def gemfile(*args)
contents = args.shift
if contents.nil?
File.open("Gemfile", "r", &:read)
else
create_file("Gemfile", contents, *args)
end
end
def lockfile(*args)
contents = args.shift
if contents.nil?
File.open("Gemfile.lock", "r", &:read)
else
create_file("Gemfile.lock", contents, *args)
end
end
def strip_whitespace(str)
# Trim the leading spaces
spaces = str[/\A\s+/, 0] || ""
str.gsub(/^#{spaces}/, "")
end
def install_gemfile(*args)
gemfile(*args)
opts = args.last.is_a?(Hash) ? args.last : {}
opts[:retry] ||= 0
bundle :install, opts
end
bang :install_gemfile
def lock_gemfile(*args)
gemfile(*args)
opts = args.last.is_a?(Hash) ? args.last : {}
opts[:retry] ||= 0
bundle :lock, opts
end
def install_gems(*gems)
options = gems.last.is_a?(Hash) ? gems.pop : {}
gem_repo = options.fetch(:gem_repo) { gem_repo1 }
gems.each do |g|
path = if g == :bundler
if ruby_core?
spec = Gem::Specification.load(gemspec.to_s)
spec.bindir = "libexec"
File.open(root.join("bundler.gemspec").to_s, "w") {|f| f.write spec.to_ruby }
Dir.chdir(root) { gem_command! :build, root.join("bundler.gemspec") }
FileUtils.rm(root.join("bundler.gemspec"))
else
Dir.chdir(root) { gem_command! :build, gemspec }
end
bundler_path = root.join("bundler-#{Bundler::VERSION}.gem")
elsif g.to_s =~ %r{\A(?:[A-Z]:)?/.*\.gem\z}
g
else
"#{gem_repo}/gems/#{g}.gem"
end
install_gem(path)
bundler_path && bundler_path.rmtree
end
end
def install_gem(path)
raise "OMG `#{path}` does not exist!" unless File.exist?(path)
gem_command! :install, "--no-document --ignore-dependencies '#{path}'"
end
def with_gem_path_as(path)
backup = ENV.to_hash
ENV["GEM_HOME"] = path.to_s
ENV["GEM_PATH"] = path.to_s
ENV["BUNDLER_ORIG_GEM_PATH"] = nil
yield
ensure
ENV.replace(backup)
end
def with_path_as(path)
backup = ENV.to_hash
ENV["PATH"] = path.to_s
ENV["BUNDLER_ORIG_PATH"] = nil
yield
ensure
ENV.replace(backup)
end
def with_path_added(path)
with_path_as(path.to_s + ":" + ENV["PATH"]) do
yield
end
end
def break_git!
FileUtils.mkdir_p(tmp("broken_path"))
File.open(tmp("broken_path/git"), "w", 0o755) do |f|
f.puts "#!/usr/bin/env ruby\nSTDERR.puts 'This is not the git you are looking for'\nexit 1"
end
ENV["PATH"] = "#{tmp("broken_path")}:#{ENV["PATH"]}"
end
def with_fake_man
FileUtils.mkdir_p(tmp("fake_man"))
File.open(tmp("fake_man/man"), "w", 0o755) do |f|
f.puts "#!/usr/bin/env ruby\nputs ARGV.inspect\n"
end
with_path_added(tmp("fake_man")) { yield }
end
def system_gems(*gems)
opts = gems.last.is_a?(Hash) ? gems.last : {}
path = opts.fetch(:path, system_gem_path)
if path == :bundle_path
path = ruby!(<<-RUBY)
require "bundler"
begin
puts Bundler.bundle_path
rescue Bundler::GemfileNotFound
ENV["BUNDLE_GEMFILE"] = "Gemfile"
retry
end
RUBY
end
gems = gems.flatten
unless opts[:keep_path]
FileUtils.rm_rf(path)
FileUtils.mkdir_p(path)
end
Gem.clear_paths
env_backup = ENV.to_hash
ENV["GEM_HOME"] = path.to_s
ENV["GEM_PATH"] = path.to_s
ENV["BUNDLER_ORIG_GEM_PATH"] = nil
install_gems(*gems)
return unless block_given?
begin
yield
ensure
ENV.replace(env_backup)
end
end
def realworld_system_gems(*gems)
gems = gems.flatten
FileUtils.rm_rf(system_gem_path)
FileUtils.mkdir_p(system_gem_path)
Gem.clear_paths
gem_home = ENV["GEM_HOME"]
gem_path = ENV["GEM_PATH"]
path = ENV["PATH"]
ENV["GEM_HOME"] = system_gem_path.to_s
ENV["GEM_PATH"] = system_gem_path.to_s
gems.each do |gem|
gem_command! :install, "--no-document #{gem}"
end
return unless block_given?
begin
yield
ensure
ENV["GEM_HOME"] = gem_home
ENV["GEM_PATH"] = gem_path
ENV["PATH"] = path
end
end
def cache_gems(*gems)
gems = gems.flatten
FileUtils.rm_rf("#{bundled_app}/vendor/cache")
FileUtils.mkdir_p("#{bundled_app}/vendor/cache")
gems.each do |g|
path = "#{gem_repo1}/gems/#{g}.gem"
raise "OMG `#{path}` does not exist!" unless File.exist?(path)
FileUtils.cp(path, "#{bundled_app}/vendor/cache")
end
end
def simulate_new_machine
system_gems []
FileUtils.rm_rf system_gem_path
FileUtils.rm_rf bundled_app(".bundle")
end
def simulate_platform(platform)
old = ENV["BUNDLER_SPEC_PLATFORM"]
ENV["BUNDLER_SPEC_PLATFORM"] = platform.to_s
yield if block_given?
ensure
ENV["BUNDLER_SPEC_PLATFORM"] = old if block_given?
end
def simulate_ruby_version(version)
return if version == RUBY_VERSION
old = ENV["BUNDLER_SPEC_RUBY_VERSION"]
ENV["BUNDLER_SPEC_RUBY_VERSION"] = version
yield if block_given?
ensure
ENV["BUNDLER_SPEC_RUBY_VERSION"] = old if block_given?
end
def simulate_ruby_engine(engine, version = "1.6.0")
return if engine == local_ruby_engine
old = ENV["BUNDLER_SPEC_RUBY_ENGINE"]
ENV["BUNDLER_SPEC_RUBY_ENGINE"] = engine
old_version = ENV["BUNDLER_SPEC_RUBY_ENGINE_VERSION"]
ENV["BUNDLER_SPEC_RUBY_ENGINE_VERSION"] = version
yield if block_given?
ensure
ENV["BUNDLER_SPEC_RUBY_ENGINE"] = old if block_given?
ENV["BUNDLER_SPEC_RUBY_ENGINE_VERSION"] = old_version if block_given?
end
def simulate_bundler_version(version)
old = ENV["BUNDLER_SPEC_VERSION"]
ENV["BUNDLER_SPEC_VERSION"] = version.to_s
yield if block_given?
ensure
ENV["BUNDLER_SPEC_VERSION"] = old if block_given?
end
def simulate_rubygems_version(version)
old = ENV["BUNDLER_SPEC_RUBYGEMS_VERSION"]
ENV["BUNDLER_SPEC_RUBYGEMS_VERSION"] = version.to_s
yield if block_given?
ensure
ENV["BUNDLER_SPEC_RUBYGEMS_VERSION"] = old if block_given?
end
def simulate_windows(platform = mswin)
old = ENV["BUNDLER_SPEC_WINDOWS"]
ENV["BUNDLER_SPEC_WINDOWS"] = "true"
simulate_platform platform do
yield
end
ensure
ENV["BUNDLER_SPEC_WINDOWS"] = old
end
def revision_for(path)
Dir.chdir(path) { `git rev-parse HEAD`.strip }
end
def capture_output
capture(:stdout)
end
def with_read_only(pattern)
chmod = lambda do |dirmode, filemode|
lambda do |f|
mode = File.directory?(f) ? dirmode : filemode
File.chmod(mode, f)
end
end
Dir[pattern].each(&chmod[0o555, 0o444])
yield
ensure
Dir[pattern].each(&chmod[0o755, 0o644])
end
# Simulate replacing TODOs with real values
def prepare_gemspec(pathname)
process_file(pathname) do |line|
case line
when /spec\.metadata\["(?:allowed_push_host|homepage_uri|source_code_uri|changelog_uri)"\]/, /spec\.homepage/
line.gsub(/\=.*$/, "= 'http://example.org'")
when /spec\.summary/
line.gsub(/\=.*$/, "= %q{A short summary of my new gem.}")
when /spec\.description/
line.gsub(/\=.*$/, "= %q{A longer description of my new gem.}")
else
line
end
end
end
def process_file(pathname)
changed_lines = pathname.readlines.map do |line|
yield line
end
File.open(pathname, "w") {|file| file.puts(changed_lines.join) }
end
def with_env_vars(env_hash, &block)
current_values = {}
env_hash.each do |k, v|
current_values[k] = ENV[k]
ENV[k] = v
end
block.call if block_given?
env_hash.each do |k, _|
ENV[k] = current_values[k]
end
end
def require_rack
# need to hack, so we can require rack
old_gem_home = ENV["GEM_HOME"]
ENV["GEM_HOME"] = Spec::Path.base_system_gems.to_s
require "rack"
ENV["GEM_HOME"] = old_gem_home
end
def wait_for_server(host, port, seconds = 15)
tries = 0
sleep 0.5
TCPSocket.new(host, port)
rescue StandardError => e
raise(e) if tries > (seconds * 2)
tries += 1
retry
end
def find_unused_port
port = 21_453
begin
port += 1 while TCPSocket.new("127.0.0.1", port)
rescue StandardError
false
end
port
end
def bundler_fileutils
if RUBY_VERSION >= "2.4"
::Bundler::FileUtils
else
::FileUtils
end
end
end
end
|