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
|
require 'rubygems/test_case'
require 'rubygems/commands/pristine_command'
class TestGemCommandsPristineCommand < Gem::TestCase
def setup
super
common_installer_setup
@cmd = Gem::Commands::PristineCommand.new
end
def test_execute
a = util_spec 'a' do |s|
s.executables = %w[foo]
s.files = %w[bin/foo lib/a.rb]
end
write_file File.join(@tempdir, 'lib', 'a.rb') do |fp|
fp.puts "puts __FILE__"
end
write_file File.join(@tempdir, 'bin', 'foo') do |fp|
fp.puts "#!/usr/bin/ruby"
end
install_gem a
foo_path = File.join @gemhome, 'gems', a.full_name, 'bin', 'foo'
a_rb_path = File.join @gemhome, 'gems', a.full_name, 'lib', 'a.rb'
write_file foo_path do |io|
io.puts 'I changed it!'
end
write_file a_rb_path do |io|
io.puts 'I changed it!'
end
@cmd.options[:args] = %w[a]
use_ui @ui do
@cmd.execute
end
assert_equal "#!/usr/bin/ruby\n", File.read(foo_path), foo_path
assert_equal "puts __FILE__\n", File.read(a_rb_path), a_rb_path
out = @ui.output.split "\n"
assert_equal "Restoring gems to pristine condition...", out.shift
assert_equal "Restored #{a.full_name}", out.shift
assert_empty out, out.inspect
end
def test_execute_all
a = util_spec 'a' do |s| s.executables = %w[foo] end
write_file File.join(@tempdir, 'bin', 'foo') do |fp|
fp.puts "#!/usr/bin/ruby"
end
install_gem a
gem_bin = File.join @gemhome, 'gems', a.full_name, 'bin', 'foo'
gem_stub = File.join @gemhome, 'bin', 'foo'
FileUtils.rm gem_bin
FileUtils.rm gem_stub
@cmd.handle_options %w[--all]
use_ui @ui do
@cmd.execute
end
assert File.exist?(gem_bin)
assert File.exist?(gem_stub)
out = @ui.output.split "\n"
assert_equal "Restoring gems to pristine condition...", out.shift
assert_equal "Restored #{a.full_name}", out.shift
assert_empty out, out.inspect
end
def test_execute_env_shebang
a = util_spec 'a' do |s|
s.executables = %w[foo]
s.files = %w[bin/foo]
end
write_file File.join(@tempdir, 'bin', 'foo') do |fp|
fp.puts "#!/usr/bin/ruby"
end
install_gem a
gem_exec = File.join @gemhome, 'bin', 'foo'
FileUtils.rm gem_exec
@cmd.handle_options %w[--all --env-shebang]
use_ui @ui do
@cmd.execute
end
assert_path_exists gem_exec
ruby_exec = sprintf Gem.default_exec_format, 'ruby'
if win_platform?
assert_match %r%\A#!\s*#{ruby_exec}%, File.read(gem_exec)
else
assert_match %r%\A#!\s*/usr/bin/env #{ruby_exec}%, File.read(gem_exec)
end
end
def test_execute_extensions_explicit
a = util_spec 'a' do |s| s.extensions << 'ext/a/extconf.rb' end
ext_path = File.join @tempdir, 'ext', 'a', 'extconf.rb'
write_file ext_path do |io|
io.write <<-'RUBY'
File.open "Makefile", "w" do |f|
f.puts "clean:\n\techo cleaned\n"
f.puts "all:\n\techo built\n"
f.puts "install:\n\techo installed\n"
end
RUBY
end
b = util_spec 'b'
install_gem a
install_gem b
@cmd.options[:extensions] = true
@cmd.options[:extensions_set] = true
@cmd.options[:args] = []
use_ui @ui do
@cmd.execute
end
out = @ui.output.split "\n"
assert_equal 'Restoring gems to pristine condition...', out.shift
assert_equal 'Building native extensions. This could take a while...',
out.shift
assert_equal "Restored #{a.full_name}", out.shift
assert_empty out, out.inspect
end
def test_execute_no_extension
a = util_spec 'a' do |s| s.extensions << 'ext/a/extconf.rb' end
ext_path = File.join @tempdir, 'ext', 'a', 'extconf.rb'
write_file ext_path do |io|
io.write "# extconf.rb\nrequire 'mkmf'; create_makefile 'a'"
end
util_build_gem a
install_gem a
@cmd.options[:args] = %w[a]
@cmd.options[:extensions] = false
use_ui @ui do
@cmd.execute
end
out = @ui.output.split "\n"
assert_equal 'Restoring gems to pristine condition...', out.shift
assert_equal "Skipped #{a.full_name}, it needs to compile an extension",
out.shift
assert_empty out, out.inspect
end
def test_execute_with_extension_with_build_args
a = util_spec 'a' do |s| s.extensions << 'ext/a/extconf.rb' end
ext_path = File.join @tempdir, 'ext', 'a', 'extconf.rb'
write_file ext_path do |io|
io.write <<-'RUBY'
File.open "Makefile", "w" do |f|
f.puts "clean:\n\techo cleaned\n"
f.puts "all:\n\techo built\n"
f.puts "install:\n\techo installed\n"
end
RUBY
end
build_args = %w!--with-awesome=true --sweet!
install_gem a, :build_args => build_args
@cmd.options[:args] = %w[a]
use_ui @ui do
@cmd.execute
end
out = @ui.output.split "\n"
assert_equal 'Restoring gems to pristine condition...', out.shift
assert_equal "Building native extensions with: '--with-awesome=true --sweet'", out.shift
assert_equal "This could take a while...", out.shift
assert_equal "Restored #{a.full_name}", out.shift
assert_empty out, out.inspect
end
def test_execute_many
a = util_spec 'a'
b = util_spec 'b'
install_gem a
install_gem b
@cmd.options[:args] = %w[a b]
use_ui @ui do
@cmd.execute
end
out = @ui.output.split "\n"
assert_equal "Restoring gems to pristine condition...", out.shift
assert_equal "Restored #{a.full_name}", out.shift
assert_equal "Restored #{b.full_name}", out.shift
assert_empty out, out.inspect
end
def test_skip
a = util_spec 'a'
b = util_spec 'b'
install_gem a
install_gem b
@cmd.options[:args] = %w[a b]
@cmd.options[:skip] = 'a'
use_ui @ui do
@cmd.execute
end
out = @ui.output.split "\n"
assert_equal "Restoring gems to pristine condition...", out.shift
assert_equal "Skipped #{a.full_name}, it was given through options", out.shift
assert_equal "Restored #{b.full_name}", out.shift
assert_empty out, out.inspect
end
def test_execute_many_multi_repo
a = util_spec 'a'
install_gem a
Gem.clear_paths
gemhome2 = File.join @tempdir, 'gemhome2'
Gem.use_paths gemhome2, [gemhome2, @gemhome]
b = util_spec 'b'
install_gem b
@cmd.options[:args] = %w[a b]
use_ui @ui do
@cmd.execute
end
out = @ui.output.split "\n"
assert_equal "Restoring gems to pristine condition...", out.shift
assert_equal "Restored #{a.full_name}", out.shift
assert_equal "Restored #{b.full_name}", out.shift
assert_empty out, out.inspect
assert_path_exists File.join(@gemhome, "gems", 'a-2')
refute_path_exists File.join(gemhome2, "gems", 'a-2')
assert_path_exists File.join(gemhome2, "gems", 'b-2')
refute_path_exists File.join(@gemhome, "gems", 'b-2')
end
def test_execute_missing_cache_gem
specs = spec_fetcher do |fetcher|
fetcher.gem 'a', 1
fetcher.gem 'a', 2
fetcher.gem 'a', 3
fetcher.gem 'a', '3.a'
end
FileUtils.rm specs['a-2'].cache_file
@cmd.options[:args] = %w[a]
use_ui @ui do
@cmd.execute
end
out = @ui.output.split "\n"
[
"Restoring gems to pristine condition...",
"Restored a-1",
"Cached gem for a-2 not found, attempting to fetch...",
"Restored a-2",
"Restored a-3.a",
"Restored a-3",
].each do |line|
assert_equal line, out.shift
end
assert_empty out, out.inspect
end
def test_execute_missing_cache_gem_when_multi_repo
specs = spec_fetcher do |fetcher|
fetcher.gem 'a', 1
fetcher.gem 'b', 1
end
FileUtils.rm_rf File.join(@gemhome, 'gems', 'a-1')
FileUtils.rm_rf File.join(@gemhome, 'gems', 'b-1')
install_gem specs["a-1"]
FileUtils.rm File.join(@gemhome, 'cache', 'a-1.gem')
Gem.clear_paths
gemhome2 = File.join(@tempdir, 'gemhome2')
Gem.use_paths gemhome2, [gemhome2, @gemhome]
install_gem specs["b-1"]
FileUtils.rm File.join(gemhome2, 'cache', 'b-1.gem')
@cmd.options[:args] = %w[a b]
use_ui @ui do
@cmd.execute
end
out = @ui.output.split "\n"
[
"Restoring gems to pristine condition...",
"Cached gem for a-1 not found, attempting to fetch...",
"Restored a-1",
"Cached gem for b-1 not found, attempting to fetch...",
"Restored b-1",
].each do |line|
assert_equal line, out.shift
end
assert_empty out, out.inspect
assert_empty @ui.error
assert_path_exists File.join(@gemhome, "cache", 'a-1.gem')
refute_path_exists File.join(gemhome2, "cache", 'a-2.gem')
assert_path_exists File.join(@gemhome, "gems", 'a-1')
refute_path_exists File.join(gemhome2, "gems", 'a-1')
assert_path_exists File.join(gemhome2, "cache", 'b-1.gem')
refute_path_exists File.join(@gemhome, "cache", 'b-2.gem')
assert_path_exists File.join(gemhome2, "gems", 'b-1')
refute_path_exists File.join(@gemhome, "gems", 'b-1')
end
def test_execute_no_gem
@cmd.options[:args] = %w[]
e = assert_raises Gem::CommandLineError do
use_ui @ui do
@cmd.execute
end
end
assert_match %r|at least one gem name|, e.message
end
def test_execute_only_executables
a = util_spec 'a' do |s|
s.executables = %w[foo]
s.files = %w[bin/foo lib/a.rb]
end
write_file File.join(@tempdir, 'lib', 'a.rb') do |fp|
fp.puts "puts __FILE__"
end
write_file File.join(@tempdir, 'bin', 'foo') do |fp|
fp.puts "#!/usr/bin/ruby"
end
install_gem a
gem_lib = File.join @gemhome, 'gems', a.full_name, 'lib', 'a.rb'
gem_exec = File.join @gemhome, 'bin', 'foo'
FileUtils.rm gem_exec
FileUtils.rm gem_lib
@cmd.handle_options %w[--all --only-executables]
use_ui @ui do
@cmd.execute
end
assert File.exist? gem_exec
refute File.exist? gem_lib
end
def test_execute_unknown_gem_at_remote_source
install_specs util_spec 'a'
@cmd.options[:args] = %w[a]
use_ui @ui do
@cmd.execute
end
assert_equal([
"Restoring gems to pristine condition...",
"Cached gem for a-2 not found, attempting to fetch...",
"Skipped a-2, it was not found from cache and remote sources"
], @ui.output.split("\n"))
assert_empty @ui.error
end
def test_execute_default_gem
default_gem_spec = new_default_spec("default", "2.0.0.0",
nil, "default/gem.rb")
install_default_specs(default_gem_spec)
@cmd.options[:args] = %w[default]
use_ui @ui do
@cmd.execute
end
assert_equal([
"Restoring gems to pristine condition...",
"Skipped default-2.0.0.0, it is a default gem",
],
@ui.output.split("\n"))
assert_empty(@ui.error)
end
def test_execute_bundled_gem_on_old_rubies
util_set_RUBY_VERSION '1.9.3', 551
spec = util_spec 'bigdecimal', '1.1.0' do |s|
s.summary = "This bigdecimal is bundled with Ruby"
end
install_specs spec
@cmd.options[:args] = %w[bigdecimal]
use_ui @ui do
@cmd.execute
end
assert_equal([
"Restoring gems to pristine condition...",
"Skipped bigdecimal-1.1.0, it is bundled with old Ruby"
], @ui.output.split("\n"))
assert_empty @ui.error
ensure
util_restore_RUBY_VERSION
end
def test_handle_options
@cmd.handle_options %w[]
refute @cmd.options[:all]
assert @cmd.options[:extensions]
refute @cmd.options[:extensions_set]
assert_equal Gem::Requirement.default, @cmd.options[:version]
end
def test_handle_options_extensions
@cmd.handle_options %w[--extensions]
assert @cmd.options[:extensions]
assert @cmd.options[:extensions_set]
end
end
|