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
|
# frozen_string_literal: true
require 'rubygems/test_case'
require 'rubygems/request_set'
require 'rubygems/request_set/lockfile'
require 'rubygems/request_set/lockfile/tokenizer'
require 'rubygems/request_set/lockfile/parser'
class TestGemRequestSetLockfileParser < Gem::TestCase
def setup
super
@gem_deps_file = 'gem.deps.rb'
@lock_file = File.expand_path "#{@gem_deps_file}.lock"
@set = Gem::RequestSet.new
end
def test_get
tokenizer = Gem::RequestSet::Lockfile::Tokenizer.new "\n"
parser = tokenizer.make_parser nil, nil
assert_equal :newline, parser.get.first
end
def test_get_type_mismatch
filename = File.expand_path("#{@gem_deps_file}.lock")
tokenizer = Gem::RequestSet::Lockfile::Tokenizer.new "foo", filename, 1, 0
parser = tokenizer.make_parser nil, nil
e = assert_raise Gem::RequestSet::Lockfile::ParseError do
parser.get :section
end
expected =
'unexpected token [:text, "foo"], expected :section (at line 1 column 0)'
assert_equal expected, e.message
assert_equal 1, e.line
assert_equal 0, e.column
assert_equal filename, e.path
end
def test_get_type_multiple
filename = File.expand_path("#{@gem_deps_file}.lock")
tokenizer = Gem::RequestSet::Lockfile::Tokenizer.new "x", filename, 1
parser = tokenizer.make_parser nil, nil
assert parser.get [:text, :section]
end
def test_get_type_value_mismatch
filename = File.expand_path("#{@gem_deps_file}.lock")
tokenizer = Gem::RequestSet::Lockfile::Tokenizer.new "x", filename, 1
parser = tokenizer.make_parser nil, nil
e = assert_raise Gem::RequestSet::Lockfile::ParseError do
parser.get :text, 'y'
end
expected =
'unexpected token [:text, "x"], expected [:text, "y"] (at line 1 column 0)'
assert_equal expected, e.message
assert_equal 1, e.line
assert_equal 0, e.column
assert_equal File.expand_path("#{@gem_deps_file}.lock"), e.path
end
def test_parse
write_lockfile <<-LOCKFILE.strip
GEM
remote: #{@gem_repo}
specs:
a (2)
PLATFORMS
#{Gem::Platform::RUBY}
DEPENDENCIES
a
LOCKFILE
platforms = []
parse_lockfile @set, platforms
assert_equal [dep('a')], @set.dependencies
assert_equal [Gem::Platform::RUBY], platforms
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
assert lockfile_set, 'could not find a LockSet'
assert_equal %w[a-2], lockfile_set.specs.map {|tuple| tuple.full_name }
end
def test_parse_dependencies
write_lockfile <<-LOCKFILE
GEM
remote: #{@gem_repo}
specs:
a (2)
PLATFORMS
#{Gem::Platform::RUBY}
DEPENDENCIES
a (>= 1, <= 2)
LOCKFILE
platforms = []
parse_lockfile @set, platforms
assert_equal [dep('a', '>= 1', '<= 2')], @set.dependencies
assert_equal [Gem::Platform::RUBY], platforms
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
assert lockfile_set, 'could not find a LockSet'
assert_equal %w[a-2], lockfile_set.specs.map {|tuple| tuple.full_name }
end
def test_parse_DEPENDENCIES_git
write_lockfile <<-LOCKFILE
GIT
remote: git://git.example/josevalim/rails-footnotes.git
revision: 3a6ac1971e91d822f057650cc5916ebfcbd6ee37
specs:
rails-footnotes (3.7.9)
rails (>= 3.0.0)
GIT
remote: git://git.example/svenfuchs/i18n-active_record.git
revision: 55507cf59f8f2173d38e07e18df0e90d25b1f0f6
specs:
i18n-active_record (0.0.2)
i18n (>= 0.5.0)
GEM
remote: http://gems.example/
specs:
i18n (0.6.9)
rails (4.0.0)
PLATFORMS
ruby
DEPENDENCIES
i18n-active_record!
rails-footnotes!
LOCKFILE
parse_lockfile @set, []
expected = [
dep('i18n-active_record', '= 0.0.2'),
dep('rails-footnotes', '= 3.7.9'),
]
assert_equal expected, @set.dependencies
end
def test_parse_DEPENDENCIES_git_version
write_lockfile <<-LOCKFILE
GIT
remote: git://github.com/progrium/ruby-jwt.git
revision: 8d74770c6cd92ea234b428b5d0c1f18306a4f41c
specs:
jwt (1.1)
GEM
remote: http://gems.example/
specs:
PLATFORMS
ruby
DEPENDENCIES
jwt (= 1.1)!
LOCKFILE
parse_lockfile @set, []
expected = [
dep('jwt', '= 1.1'),
]
assert_equal expected, @set.dependencies
end
def test_parse_GEM
write_lockfile <<-LOCKFILE
GEM
specs:
a (2)
PLATFORMS
ruby
DEPENDENCIES
a
LOCKFILE
parse_lockfile @set, []
assert_equal [dep('a', '>= 0')], @set.dependencies
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
assert lockfile_set, 'found a LockSet'
assert_equal %w[a-2], lockfile_set.specs.map {|s| s.full_name }
end
def test_parse_GEM_remote_multiple
write_lockfile <<-LOCKFILE
GEM
remote: https://gems.example/
remote: https://other.example/
specs:
a (2)
PLATFORMS
ruby
DEPENDENCIES
a
LOCKFILE
parse_lockfile @set, []
assert_equal [dep('a', '>= 0')], @set.dependencies
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
assert lockfile_set, 'found a LockSet'
assert_equal %w[a-2], lockfile_set.specs.map {|s| s.full_name }
assert_equal %w[https://gems.example/ https://other.example/],
lockfile_set.specs.flat_map {|s| s.sources.map{|src| src.uri.to_s } }
end
def test_parse_GIT
@set.instance_variable_set :@install_dir, 'install_dir'
write_lockfile <<-LOCKFILE
GIT
remote: git://example/a.git
revision: master
specs:
a (2)
b (>= 3)
c
DEPENDENCIES
a!
LOCKFILE
parse_lockfile @set, []
assert_equal [dep('a', '= 2')], @set.dependencies
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
refute lockfile_set, 'fount a LockSet'
git_set = @set.sets.find do |set|
Gem::Resolver::GitSet === set
end
assert git_set, 'could not find a GitSet'
assert_equal %w[a-2], git_set.specs.values.map {|s| s.full_name }
assert_equal [dep('b', '>= 3'), dep('c')],
git_set.specs.values.first.dependencies
expected = {
'a' => %w[git://example/a.git master],
}
assert_equal expected, git_set.repositories
assert_equal 'install_dir', git_set.root_dir
end
def test_parse_GIT_branch
write_lockfile <<-LOCKFILE
GIT
remote: git://example/a.git
revision: 1234abc
branch: 0-9-12-stable
specs:
a (2)
b (>= 3)
DEPENDENCIES
a!
LOCKFILE
parse_lockfile @set, []
assert_equal [dep('a', '= 2')], @set.dependencies
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
refute lockfile_set, 'fount a LockSet'
git_set = @set.sets.find do |set|
Gem::Resolver::GitSet === set
end
assert git_set, 'could not find a GitSet'
expected = {
'a' => %w[git://example/a.git 1234abc],
}
assert_equal expected, git_set.repositories
end
def test_parse_GIT_ref
write_lockfile <<-LOCKFILE
GIT
remote: git://example/a.git
revision: 1234abc
ref: 1234abc
specs:
a (2)
b (>= 3)
DEPENDENCIES
a!
LOCKFILE
parse_lockfile @set, []
assert_equal [dep('a', '= 2')], @set.dependencies
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
refute lockfile_set, 'fount a LockSet'
git_set = @set.sets.find do |set|
Gem::Resolver::GitSet === set
end
assert git_set, 'could not find a GitSet'
expected = {
'a' => %w[git://example/a.git 1234abc],
}
assert_equal expected, git_set.repositories
end
def test_parse_GIT_tag
write_lockfile <<-LOCKFILE
GIT
remote: git://example/a.git
revision: 1234abc
tag: v0.9.12
specs:
a (2)
b (>= 3)
DEPENDENCIES
a!
LOCKFILE
parse_lockfile @set, []
assert_equal [dep('a', '= 2')], @set.dependencies
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
refute lockfile_set, 'fount a LockSet'
git_set = @set.sets.find do |set|
Gem::Resolver::GitSet === set
end
assert git_set, 'could not find a GitSet'
expected = {
'a' => %w[git://example/a.git 1234abc],
}
assert_equal expected, git_set.repositories
end
def test_parse_PATH
_, _, directory = vendor_gem
write_lockfile <<-LOCKFILE
PATH
remote: #{directory}
specs:
a (1)
b (2)
DEPENDENCIES
a!
LOCKFILE
parse_lockfile @set, []
assert_equal [dep('a', '= 1')], @set.dependencies
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
refute lockfile_set, 'found a LockSet'
vendor_set = @set.sets.find do |set|
Gem::Resolver::VendorSet === set
end
assert vendor_set, 'could not find a VendorSet'
assert_equal %w[a-1], vendor_set.specs.values.map {|s| s.full_name }
spec = vendor_set.load_spec 'a', nil, nil, nil
assert_equal [dep('b', '= 2')], spec.dependencies
end
def test_parse_dependency
write_lockfile ' 1)'
tokenizer = Gem::RequestSet::Lockfile::Tokenizer.from_file @lock_file
parser = tokenizer.make_parser nil, nil
parsed = parser.parse_dependency 'a', '='
assert_equal dep('a', '= 1'), parsed
write_lockfile ')'
tokenizer = Gem::RequestSet::Lockfile::Tokenizer.from_file @lock_file
parser = tokenizer.make_parser nil, nil
parsed = parser.parse_dependency 'a', '2'
assert_equal dep('a', '= 2'), parsed
end
def test_parse_gem_specs_dependency
write_lockfile <<-LOCKFILE
GEM
remote: #{@gem_repo}
specs:
a (2)
b (= 3)
c (~> 4)
d
e (~> 5.0, >= 5.0.1)
b (3-x86_64-linux)
PLATFORMS
#{Gem::Platform::RUBY}
DEPENDENCIES
a
LOCKFILE
platforms = []
parse_lockfile @set, platforms
assert_equal [dep('a')], @set.dependencies
assert_equal [Gem::Platform::RUBY], platforms
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
assert lockfile_set, 'could not find a LockSet'
assert_equal %w[a-2 b-3], lockfile_set.specs.map {|tuple| tuple.full_name }
expected = [
Gem::Platform::RUBY,
Gem::Platform.new('x86_64-linux'),
]
assert_equal expected, lockfile_set.specs.map {|tuple| tuple.platform }
spec = lockfile_set.specs.first
expected = [
dep('b', '= 3'),
dep('c', '~> 4'),
dep('d'),
dep('e', '~> 5.0', '>= 5.0.1'),
]
assert_equal expected, spec.dependencies
end
def test_parse_missing
assert_raise(Errno::ENOENT) do
parse_lockfile @set, []
end
lockfile_set = @set.sets.find do |set|
Gem::Resolver::LockSet === set
end
refute lockfile_set
end
def write_lockfile(lockfile)
File.open @lock_file, 'w' do |io|
io.write lockfile
end
end
def parse_lockfile(set, platforms)
tokenizer = Gem::RequestSet::Lockfile::Tokenizer.from_file @lock_file
parser = tokenizer.make_parser set, platforms
parser.parse
end
end
|