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
|
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest
import merge_isolate
# Create shortcuts.
from merge_isolate import KEY_TRACKED, KEY_UNTRACKED
class MergeGyp(unittest.TestCase):
def test_unknown_key(self):
try:
merge_isolate.verify_variables({'foo': [],})
self.fail()
except AssertionError:
pass
def test_unknown_var(self):
try:
merge_isolate.verify_condition({'variables': {'foo': [],}})
self.fail()
except AssertionError:
pass
def test_union(self):
value1 = {
'a': set(['A']),
'b': ['B', 'C'],
'c': 'C',
}
value2 = {
'a': set(['B', 'C']),
'b': [],
'd': set(),
}
expected = {
'a': set(['A', 'B', 'C']),
'b': ['B', 'C'],
'c': 'C',
'd': set(),
}
self.assertEquals(expected, merge_isolate.union(value1, value2))
def test_eval_content(self):
try:
# Intrinsics are not available.
merge_isolate.eval_content('map(str, [1, 2])')
self.fail()
except NameError:
pass
def test_load_gyp_empty(self):
self.assertEquals({}, merge_isolate.load_gyp({}).flatten())
def test_load_gyp(self):
value = {
'variables': {
KEY_TRACKED: ['a'],
KEY_UNTRACKED: ['b'],
},
'conditions': [
['OS=="atari"', {
'variables': {
KEY_TRACKED: ['c', 'x'],
KEY_UNTRACKED: ['d'],
'command': ['echo', 'Hello World'],
'read_only': True,
},
}, { # else
'variables': {
KEY_TRACKED: ['e', 'x'],
KEY_UNTRACKED: ['f'],
'command': ['echo', 'You should get an Atari'],
},
}],
['OS=="amiga"', {
'variables': {
KEY_TRACKED: ['g'],
'read_only': False,
},
}],
['OS=="dendy"', {
}],
['OS=="coleco"', {
}, { # else
'variables': {
KEY_UNTRACKED: ['h'],
'read_only': None,
},
}],
],
}
expected = {
'amiga': {
'command': ['echo', 'You should get an Atari'],
KEY_TRACKED: ['a', 'e', 'g', 'x'],
KEY_UNTRACKED: ['b', 'f', 'h'],
'read_only': False,
},
'atari': {
'command': ['echo', 'Hello World'],
KEY_TRACKED: ['a', 'c', 'x'],
KEY_UNTRACKED: ['b', 'd', 'h'],
'read_only': True,
},
'coleco': {
'command': ['echo', 'You should get an Atari'],
KEY_TRACKED: ['a', 'e', 'x'],
KEY_UNTRACKED: ['b', 'f'],
},
'dendy': {
'command': ['echo', 'You should get an Atari'],
KEY_TRACKED: ['a', 'e', 'x'],
KEY_UNTRACKED: ['b', 'f', 'h'],
},
}
self.assertEquals(expected, merge_isolate.load_gyp(value).flatten())
def test_load_gyp_duplicate_command(self):
value = {
'variables': {
'command': ['rm', '-rf', '/'],
},
'conditions': [
['OS=="atari"', {
'variables': {
'command': ['echo', 'Hello World'],
},
}],
],
}
try:
merge_isolate.load_gyp(value)
self.fail()
except AssertionError:
pass
def test_load_gyp_no_condition(self):
value = {
'variables': {
KEY_TRACKED: ['a'],
KEY_UNTRACKED: ['b'],
},
}
expected = {
KEY_TRACKED: ['a'],
KEY_UNTRACKED: ['b'],
}
actual = merge_isolate.load_gyp(value)
# Flattening the whole config will discard 'None'.
self.assertEquals({}, actual.flatten())
self.assertEquals([None], actual.per_os.keys())
# But the 'None' value is still available as a backup.
self.assertEquals(expected, actual.per_os[None].flatten())
def test_invert_map(self):
value = {
'amiga': {
'command': ['echo', 'You should get an Atari'],
KEY_TRACKED: ['a', 'e', 'g', 'x'],
KEY_UNTRACKED: ['b', 'f', 'h'],
'read_only': False,
},
'atari': {
'command': ['echo', 'Hello World'],
KEY_TRACKED: ['a', 'c', 'x'],
KEY_UNTRACKED: ['b', 'd', 'h'],
'read_only': True,
},
'coleco': {
'command': ['echo', 'You should get an Atari'],
KEY_TRACKED: ['a', 'e', 'x'],
KEY_UNTRACKED: ['b', 'f'],
},
'dendy': {
'command': ['echo', 'You should get an Atari'],
KEY_TRACKED: ['a', 'e', 'x'],
KEY_UNTRACKED: ['b', 'f', 'h'],
},
}
expected_values = {
'command': {
('echo', 'Hello World'): set(['atari']),
('echo', 'You should get an Atari'): set(['amiga', 'coleco', 'dendy']),
},
KEY_TRACKED: {
'a': set(['amiga', 'atari', 'coleco', 'dendy']),
'c': set(['atari']),
'e': set(['amiga', 'coleco', 'dendy']),
'g': set(['amiga']),
'x': set(['amiga', 'atari', 'coleco', 'dendy']),
},
KEY_UNTRACKED: {
'b': set(['amiga', 'atari', 'coleco', 'dendy']),
'd': set(['atari']),
'f': set(['amiga', 'coleco', 'dendy']),
'h': set(['amiga', 'atari', 'dendy']),
},
'read_only': {
None: set(['coleco', 'dendy']),
False: set(['amiga']),
True: set(['atari']),
},
}
expected_oses = set(['amiga', 'atari', 'coleco', 'dendy'])
actual_values, actual_oses = merge_isolate.invert_map(value)
self.assertEquals(expected_values, actual_values)
self.assertEquals(expected_oses, actual_oses)
def test_reduce_inputs(self):
values = {
'command': {
('echo', 'Hello World'): set(['atari']),
('echo', 'You should get an Atari'): set(['amiga', 'coleco', 'dendy']),
},
KEY_TRACKED: {
'a': set(['amiga', 'atari', 'coleco', 'dendy']),
'c': set(['atari']),
'e': set(['amiga', 'coleco', 'dendy']),
'g': set(['amiga']),
'x': set(['amiga', 'atari', 'coleco', 'dendy']),
},
KEY_UNTRACKED: {
'b': set(['amiga', 'atari', 'coleco', 'dendy']),
'd': set(['atari']),
'f': set(['amiga', 'coleco', 'dendy']),
'h': set(['amiga', 'atari', 'dendy']),
},
'read_only': {
None: set(['coleco', 'dendy']),
False: set(['amiga']),
True: set(['atari']),
},
}
oses = set(['amiga', 'atari', 'coleco', 'dendy'])
expected_values = {
'command': {
('echo', 'Hello World'): set(['atari']),
('echo', 'You should get an Atari'): set(['!atari']),
},
KEY_TRACKED: {
'a': set([None]),
'c': set(['atari']),
'e': set(['!atari']),
'g': set(['amiga']),
'x': set([None]),
},
KEY_UNTRACKED: {
'b': set([None]),
'd': set(['atari']),
'f': set(['!atari']),
'h': set(['!coleco']),
},
'read_only': {
None: set(['coleco', 'dendy']),
False: set(['amiga']),
True: set(['atari']),
},
}
actual_values, actual_oses = merge_isolate.reduce_inputs(values, oses)
self.assertEquals(expected_values, actual_values)
self.assertEquals(oses, actual_oses)
def test_convert_map_to_gyp(self):
values = {
'command': {
('echo', 'Hello World'): set(['atari']),
('echo', 'You should get an Atari'): set(['!atari']),
},
KEY_TRACKED: {
'a': set([None]),
'c': set(['atari']),
'e': set(['!atari']),
'g': set(['amiga']),
'x': set([None]),
},
KEY_UNTRACKED: {
'b': set([None]),
'd': set(['atari']),
'f': set(['!atari']),
'h': set(['!coleco']),
},
'read_only': {
None: set(['coleco', 'dendy']),
False: set(['amiga']),
True: set(['atari']),
},
}
oses = set(['amiga', 'atari', 'coleco', 'dendy'])
expected = {
'variables': {
KEY_TRACKED: ['a', 'x'],
KEY_UNTRACKED: ['b'],
},
'conditions': [
['OS=="amiga"', {
'variables': {
KEY_TRACKED: ['g'],
'read_only': False,
},
}],
['OS=="atari"', {
'variables': {
'command': ['echo', 'Hello World'],
KEY_TRACKED: ['c'],
KEY_UNTRACKED: ['d'],
'read_only': True,
},
}, {
'variables': {
'command': ['echo', 'You should get an Atari'],
KEY_TRACKED: ['e'],
KEY_UNTRACKED: ['f'],
},
}],
['OS=="coleco"', {
}, {
'variables': {
KEY_UNTRACKED: ['h'],
},
}],
],
}
self.assertEquals(expected, merge_isolate.convert_map_to_gyp(values, oses))
def test_merge_two_empty(self):
# Flat stay flat. Pylint is confused about union() return type.
# pylint: disable=E1103
actual = merge_isolate.union(
merge_isolate.union(
merge_isolate.Configs([]),
merge_isolate.load_gyp({})),
merge_isolate.load_gyp({})).flatten()
self.assertEquals({}, actual)
def test_merge_empty(self):
actual = merge_isolate.convert_map_to_gyp(*merge_isolate.reduce_inputs(
*merge_isolate.invert_map({})))
self.assertEquals({}, actual)
def test_load_two_conditions(self):
linux = {
'conditions': [
['OS=="linux"', {
'variables': {
'isolate_dependency_tracked': [
'file_linux',
'file_common',
],
},
}],
],
}
mac = {
'conditions': [
['OS=="mac"', {
'variables': {
'isolate_dependency_tracked': [
'file_mac',
'file_common',
],
},
}],
],
}
expected = {
'linux': {
'isolate_dependency_tracked': ['file_common', 'file_linux'],
},
'mac': {
'isolate_dependency_tracked': ['file_common', 'file_mac'],
},
}
# Pylint is confused about union() return type.
# pylint: disable=E1103
configs = merge_isolate.union(
merge_isolate.union(
merge_isolate.Configs([]),
merge_isolate.load_gyp(linux)),
merge_isolate.load_gyp(mac)).flatten()
self.assertEquals(expected, configs)
def test_load_three_conditions(self):
linux = {
'conditions': [
['OS=="linux"', {
'variables': {
'isolate_dependency_tracked': [
'file_linux',
'file_common',
],
},
}],
],
}
mac = {
'conditions': [
['OS=="mac"', {
'variables': {
'isolate_dependency_tracked': [
'file_mac',
'file_common',
],
},
}],
],
}
win = {
'conditions': [
['OS=="win"', {
'variables': {
'isolate_dependency_tracked': [
'file_win',
'file_common',
],
},
}],
],
}
expected = {
'linux': {
'isolate_dependency_tracked': ['file_common', 'file_linux'],
},
'mac': {
'isolate_dependency_tracked': ['file_common', 'file_mac'],
},
'win': {
'isolate_dependency_tracked': ['file_common', 'file_win'],
},
}
# Pylint is confused about union() return type.
# pylint: disable=E1103
configs = merge_isolate.union(
merge_isolate.union(
merge_isolate.union(
merge_isolate.Configs([]),
merge_isolate.load_gyp(linux)),
merge_isolate.load_gyp(mac)),
merge_isolate.load_gyp(win)).flatten()
self.assertEquals(expected, configs)
def test_merge_three_conditions(self):
values = {
'linux': {
'isolate_dependency_tracked': ['file_common', 'file_linux'],
},
'mac': {
'isolate_dependency_tracked': ['file_common', 'file_mac'],
},
'win': {
'isolate_dependency_tracked': ['file_common', 'file_win'],
},
}
expected = {
'variables': {
'isolate_dependency_tracked': [
'file_common',
],
},
'conditions': [
['OS=="linux"', {
'variables': {
'isolate_dependency_tracked': [
'file_linux',
],
},
}],
['OS=="mac"', {
'variables': {
'isolate_dependency_tracked': [
'file_mac',
],
},
}],
['OS=="win"', {
'variables': {
'isolate_dependency_tracked': [
'file_win',
],
},
}],
],
}
actual = merge_isolate.convert_map_to_gyp(*merge_isolate.reduce_inputs(
*merge_isolate.invert_map(values)))
self.assertEquals(expected, actual)
if __name__ == '__main__':
unittest.main()
|