Repository navigation
Expand file tree
/
Copy pathtest_gemfile_lock.py
More file actions
460 lines (401 loc) · 17 KB
/
Copy pathtest_gemfile_lock.py
File metadata and controls
460 lines (401 loc) · 17 KB
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
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# ScanCode is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/nexB/scancode-toolkit for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#
import json
import os
import shutil
from commoncode.testcase import FileBasedTesting
from packagedcode import gemfile_lock
from scancode_config import REGEN_TEST_FIXTURES
class TestGemfileLock(FileBasedTesting):
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
def check_results(self, results, expected_loc, regen=REGEN_TEST_FIXTURES):
"""
Helper to test a Gemfile object against an expected JSON file.
"""
expected_loc = self.get_test_loc(expected_loc)
if regen:
regened_exp_loc = self.get_temp_file()
wmode = 'w'
with open(regened_exp_loc, wmode) as ex:
json.dump(results, ex, indent=2, separators=(',', ': '))
expected_dir = os.path.dirname(expected_loc)
if not os.path.exists(expected_dir):
os.makedirs(expected_dir)
shutil.copy(regened_exp_loc, expected_loc)
with open(expected_loc) as ex:
expected = json.load(ex)
try:
assert results == expected
except AssertionError:
assert json.dumps(results, indent=2) == json.dumps(expected, indent=2)
def check_gemfile_lock(self, test_file, expected_loc, regen=REGEN_TEST_FIXTURES):
test_file = self.get_test_loc(test_file)
gfl = gemfile_lock.GemfileLockParser(test_file)
results = [g.to_dict() for g in gfl.all_gems.values()]
self.check_results(results, expected_loc, regen=regen)
def test_get_options(self):
test = '''GIT
remote: git://github.com/amatsuda/kaminari.git
revision: cb5539e0e81188ccadc0e703b32ae7b5dec13cb1
specs:
kaminari (0.15.0)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
ref: 4e286bf
ref: 81e5e4d6
ref: 92d6dac
remote: .
remote: ..
remote: ../
remote: ../../
remote: ../gems/encryptor
remote: /Users/hector/dev/gems/factory
remote: git://github.com/ahoward/mongoid-grid_fs.git
remote: git://github.com/amatsuda/kaminari.git
remote: http://rubygems.org/
remote: http://www.rubygems.org/
revision: 059012a2c2a9e0a5d6e67137752c3e918689c88a
foo: bar'''.splitlines()
expected = [
(None, None),
('remote', 'git://github.com/amatsuda/kaminari.git'),
('revision', 'cb5539e0e81188ccadc0e703b32ae7b5dec13cb1'),
(None, None),
(None, None),
(None, None),
(None, None),
('ref', '4e286bf'),
('ref', '81e5e4d6'),
('ref', '92d6dac'),
('remote', '.'),
('remote', '..'),
('remote', '../'),
('remote', '../../'),
('remote', '../gems/encryptor'),
('remote', '/Users/hector/dev/gems/factory'),
('remote', 'git://github.com/ahoward/mongoid-grid_fs.git'),
('remote', 'git://github.com/amatsuda/kaminari.git'),
('remote', 'http://rubygems.org/'),
('remote', 'http://www.rubygems.org/'),
('revision', '059012a2c2a9e0a5d6e67137752c3e918689c88a'),
(None, None)
]
results = [gemfile_lock.get_option(t) for t in test]
assert results == expected
def test_NAME_VERSION_re(self):
import re
nv = re.compile(gemfile_lock.NAME_VERSION).match
test = [
'brakeman (2.3.1)',
'erubis (~> 2.6)',
'fastercsv (~> 1.5)',
'haml (>= 3.0, < 5.0)',
'highline (~> 1.6.20)',
'multi_json (~> 1.2)',
'ruby2ruby (~> 2.0.5)',
'ruby_parser (~> 3.2.2)',
'sass (~> 3.0)',
'slim (>= 1.3.6, < 3.0)',
'terminal-table (~> 1.4)',
'json (1.8.0-java)',
'json',
'alpha (1.9.0-x86-mingw32)'
]
expected = [
('brakeman', '2.3.1'),
('erubis', '~> 2.6'),
('fastercsv', '~> 1.5'),
('haml', '>= 3.0, < 5.0'),
('highline', '~> 1.6.20'),
('multi_json', '~> 1.2'),
('ruby2ruby', '~> 2.0.5'),
('ruby_parser', '~> 3.2.2'),
('sass', '~> 3.0'),
('slim', '>= 1.3.6, < 3.0'),
('terminal-table', '~> 1.4'),
('json', '1.8.0'),
('json', None),
('alpha', '1.9.0')
]
results = [(nv(x).group('name'),
nv(x).group('version'),) for x in test]
assert results == expected
def test_DEPS_re(self):
test = '''DEPENDENCIES
activesupport (~> 3.0)
addressable
bcrypt-ruby!
activemodel (= 3.2.13.rc1)
activemodel (= 4.0.0.beta)!
activerecord (~> 3.1.12)
activerecord (= 2.0.0)
activerecord (>= 3.0.0)
activesupport (= 2.3.11)
msgpack (>= 0.4.4, < 0.6.0, != 0.5.3, != 0.5.2, != 0.5.1, != 0.5.0)
msgpack (>= 0.4.4, < 0.6.0, != 0.5.3, != 0.5.2, != 0.5.1, != 0.5.0)!
tilt (~> 1.1, != 1.3.0)!'''.splitlines()
expected = [
None,
('activesupport', '~> 3.0', False),
('addressable', None, False),
('bcrypt-ruby', None, True),
('activemodel', '= 3.2.13.rc1', False),
('activemodel', '= 4.0.0.beta', True),
('activerecord', '~> 3.1.12', False),
('activerecord', '= 2.0.0', False),
('activerecord', '>= 3.0.0', False),
('activesupport', '= 2.3.11', False),
('msgpack', '>= 0.4.4, < 0.6.0, != 0.5.3, != 0.5.2, != 0.5.1, != 0.5.0', False),
('msgpack', '>= 0.4.4, < 0.6.0, != 0.5.3, != 0.5.2, != 0.5.1, != 0.5.0', True),
('tilt', '~> 1.1, != 1.3.0', True),
]
results = []
for t in test:
dep = gemfile_lock.DEPS(t)
if dep:
version = dep.group('version')
name = dep.group('name')
pinned = True if dep.group('pinned') else False
results.append((name, version, pinned,))
else:
results.append(None)
assert results == expected
def test_SPEC_DEPS_re(self):
test = ''' specs:
actionmailer (4.0.0.rc1)
actionpack (4.0.0.beta1)
airbrake (3.1.16)
akami (1.2.1)
allowy (0.4.0)
arel (5.0.1.20140414130214)
bson (1.4.0-java)
active_attr (0.8.4)'''.splitlines()
expected = [
('actionmailer', '4.0.0.rc1'),
('actionpack', '4.0.0.beta1'),
('airbrake', '3.1.16'),
('akami', '1.2.1'),
('allowy', '0.4.0'),
('arel', '5.0.1.20140414130214'),
('bson', '1.4.0'),
('active_attr', '0.8.4')
]
nv = gemfile_lock.SPEC_DEPS
results = [(nv(x).group('name'), nv(x).group('version'),)
for x in test if nv(x)]
assert results == expected
def test_SPEC_SUB_DEPS_re(self):
test = ''' specs:
akami (1.2.1)
actionmailer (= 4.0.0)
actionmailer (= 4.0.0.rc1)
actionmailer (>= 3, < 5)
actionpack (~> 3.0)
active_utils (~> 2.0, >= 2.0.1)
activemodel (~> 3.0)
activemodel (~> 3.0.0)
beefcake (~> 1.2366.0)
msgpack (>= 0.4.4, < 0.6.0, != 0.5.3, != 0.5.2, != 0.5.1, != 0.5.0)
tilt (~> 1.1, != 1.3.0)'''.splitlines()
expected = [
('actionmailer', '= 4.0.0'),
('actionmailer', '= 4.0.0.rc1'),
('actionmailer', '>= 3, < 5'),
('actionpack', '~> 3.0'),
('active_utils', '~> 2.0, >= 2.0.1'),
('activemodel', '~> 3.0'),
('activemodel', '~> 3.0.0'),
('beefcake', '~> 1.2366.0'),
('msgpack', '>= 0.4.4, < 0.6.0, != 0.5.3, != 0.5.2, != 0.5.1, != 0.5.0'),
('tilt', '~> 1.1, != 1.3.0'),
]
nv = gemfile_lock.SPEC_SUB_DEPS
results = [(nv(x).group('name'), nv(x).group('version'),)
for x in test if nv(x)]
assert results == expected
def test_Gem_as_nv_tree(self):
Gem = gemfile_lock.Gem
a = Gem('a', '1')
b = Gem('b', '2')
c = Gem('c', '2')
d = Gem('d', '2')
e = Gem('e', '2')
f = Gem('f', '2')
a.dependencies['b'] = b
a.dependencies['c'] = c
b.dependencies['d'] = d
b.dependencies['e'] = e
b.dependencies['f'] = f
c.dependencies['e'] = e
c.dependencies['f'] = f
expected = {
('a', '1'): {
('b', '2'): {
('d', '2'): {},
('e', '2'): {},
('f', '2'): {}
},
('c', '2'): {
('e', '2'): {},
('f', '2'): {}
}
}
}
assert a.as_nv_tree() == expected
def test_Gem_flatten(self):
Gem = gemfile_lock.Gem
a = Gem('a', 'v1')
b = Gem('b', 'v2')
c = Gem('c', 'v3')
d = Gem('d', 'v4')
e = Gem('e', 'v5')
f = Gem('f', 'v6')
g = Gem('g', 'v7')
a.dependencies['b'] = b
a.dependencies['c'] = c
b.dependencies['d'] = d
b.dependencies['e'] = e
b.dependencies['f'] = f
c.dependencies['e'] = e
c.dependencies['f'] = f
c.dependencies['g'] = g
g.dependencies['b'] = b
expected = sorted([
(a, c),
(a, b),
(b, d),
(b, e),
(b, f),
(c, e),
(c, f),
(c, g),
(g, b),
])
results = sorted(a.flatten())
assert results == expected
def test_Gem_as_nv_tree_with_no_deps(self):
Gem = gemfile_lock.Gem
a = Gem('a', '1')
expected = {('a', '1'): {}}
results = a.as_nv_tree()
assert results == expected
def test_Gem_to_dict(self):
Gem = gemfile_lock.Gem
a = Gem('a', '1')
b = Gem('b', '2')
a.dependencies['b'] = b
expected = [
(u'name', 'a'),
(u'version', '1'),
(u'platform', None),
(u'pinned', False),
(u'remote', None),
(u'type', None),
(u'path', None),
(u'revision', None),
(u'ref', None),
(u'branch', None),
(u'submodules', None),
(u'tag', None),
(u'requirements', []),
(u'dependencies',
dict([(u'a@1', dict([(u'b@2', {})]))]))
]
results = a.to_dict()
assert list(results.items()) == expected
def test_GemfileLockParser_can_parse_a_flat_list_of_deps(self):
test_file = 'gemfile_lock/as_deps/Gemfile.lock'
expected_loc = 'gemfile_lock/as_deps/Gemfile.lock.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_simple_files(self):
test_file = 'gemfile_lock/complete/Gemfile.lock_simple'
expected_loc = 'gemfile_lock/complete/Gemfile.lock_simple.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_complex_files(self):
test_file = 'gemfile_lock/complete/Gemfile.lock_complex'
expected_loc = 'gemfile_lock/complete/Gemfile.lock_complex.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_dependency(self):
test_file = 'gemfile_lock/dependency/Gemfile.lock'
expected_loc = 'gemfile_lock/dependency/Gemfile.lock.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_platform(self):
test_file = 'gemfile_lock/platform/Gemfile.lock'
expected_loc = 'gemfile_lock/platform/Gemfile.lock.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_bundled(self):
test_file = 'gemfile_lock/bundled/Gemfile.lock'
expected_loc = 'gemfile_lock/bundled/Gemfile.lock.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
gfl = gemfile_lock.GemfileLockParser(self.get_test_loc(test_file))
assert gfl.bundled_with == "2.0.1"
def test_GemfileLockParser_can_parse_spec_single_level(self):
test_file = 'gemfile_lock/spec/Gemfile.lock1'
expected_loc = 'gemfile_lock/spec/Gemfile.lock1.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_spec_two_levels(self):
test_file = 'gemfile_lock/spec/Gemfile.lock2'
expected_loc = 'gemfile_lock/spec/Gemfile.lock2.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_spec_multi_levels(self):
test_file = 'gemfile_lock/spec/Gemfile.lock3'
expected_loc = 'gemfile_lock/spec/Gemfile.lock3.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_complex_multi_levels_deps(self):
test_file = 'gemfile_lock/spec/Gemfile.lock4'
expected_loc = 'gemfile_lock/spec/Gemfile.lock4.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_gem(self):
test_file = 'gemfile_lock/gem/Gemfile.lock1'
expected_loc = 'gemfile_lock/gem/Gemfile.lock1.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_gem_with_two_remotes(self):
test_file = 'gemfile_lock/gem/Gemfile.lock3'
expected_loc = 'gemfile_lock/gem/Gemfile.lock3.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_gem_with_path_before(self):
test_file = 'gemfile_lock/gem/Gemfile.lock2'
expected_loc = 'gemfile_lock/gem/Gemfile.lock2.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_source_with_one_path(self):
test_file = 'gemfile_lock/source/Gemfile.lock_path1'
expected_loc = 'gemfile_lock/source/Gemfile.lock_path1.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_with_two_paths(self):
test_file = 'gemfile_lock/source/Gemfile.lock_path2'
expected_loc = 'gemfile_lock/source/Gemfile.lock_path2.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_tag_and_remote_mixed_step1(self):
test_file = 'gemfile_lock/source/Gemfile.lock_mixed1'
expected_loc = 'gemfile_lock/source/Gemfile.lock_mixed1.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_source_with_remote(self):
test_file = 'gemfile_lock/source/Gemfile.lock_mixed2'
expected_loc = 'gemfile_lock/source/Gemfile.lock_mixed2.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_source_with_vcs_refs_mixed_step3(self):
test_file = 'gemfile_lock/source/Gemfile.lock_mixed3'
expected_loc = 'gemfile_lock/source/Gemfile.lock_mixed3.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_source_with_vsc_mixed_step4(self):
test_file = 'gemfile_lock/source/Gemfile.lock_mixed4'
expected_loc = 'gemfile_lock/source/Gemfile.lock_mixed4.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_source_with_mixed_step5(self):
test_file = 'gemfile_lock/source/Gemfile.lock_mixed5'
expected_loc = 'gemfile_lock/source/Gemfile.lock_mixed5.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_source_with_multi_paths_in_git(self):
test_file = 'gemfile_lock/git/Gemfile.lock'
expected_loc = 'gemfile_lock/git/Gemfile.lock.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)
def test_GemfileLockParser_can_parse_source_source_with_git_and_no_deps(self):
test_file = 'gemfile_lock/git/Gemfile.lock_single'
expected_loc = 'gemfile_lock/git/Gemfile.lock_single.expected.json'
self.check_gemfile_lock(test_file, expected_loc, regen=REGEN_TEST_FIXTURES)