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
|
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
import string
import sys
import unittest
import TestSCons
import SCons.Variables
import SCons.Subst
import SCons.Warnings
class Environment:
def __init__(self):
self.dict = {}
def subst(self, x):
return SCons.Subst.scons_subst(x, self, gvars=self.dict)
def __setitem__(self, key, value):
self.dict[key] = value
def __getitem__(self, key):
return self.dict[key]
def has_key(self, key):
return self.dict.has_key(key)
def check(key, value, env):
assert int(value) == 6 * 9, "key %s = %s" % (key, repr(value))
# Check saved option file by executing and comparing against
# the expected dictionary
def checkSave(file, expected):
gdict = {}
ldict = {}
execfile(file, gdict, ldict)
assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
class VariablesTestCase(unittest.TestCase):
def test_keys(self):
"""Test the Variables.keys() method"""
opts = SCons.Variables.Variables()
opts.Add('VAR1')
opts.Add('VAR2',
'THE answer to THE question',
"42",
check,
lambda x: int(x) + 12)
keys = opts.keys()
assert keys == ['VAR1', 'VAR2'], keys
def test_Add(self):
"""Test adding to a Variables object"""
opts = SCons.Variables.Variables()
opts.Add('VAR')
opts.Add('ANSWER',
'THE answer to THE question',
"42",
check,
lambda x: int(x) + 12)
o = opts.options[0]
assert o.key == 'VAR'
assert o.help == ''
assert o.default == None
assert o.validator == None
assert o.converter == None
o = opts.options[1]
assert o.key == 'ANSWER'
assert o.help == 'THE answer to THE question'
assert o.default == "42"
o.validator(o.key, o.converter(o.default), {})
def test_it(var, opts=opts):
exc_caught = None
try:
opts.Add(var)
except SCons.Errors.UserError:
exc_caught = 1
assert exc_caught, "did not catch UserError for '%s'" % var
test_it('foo/bar')
test_it('foo-bar')
test_it('foo.bar')
def test_AddVariables(self):
"""Test adding a list of options to a Variables object"""
opts = SCons.Variables.Variables()
opts.AddVariables(('VAR2',),
('ANSWER2',
'THE answer to THE question',
"42",
check,
lambda x: int(x) + 12))
o = opts.options[0]
assert o.key == 'VAR2', o.key
assert o.help == '', o.help
assert o.default == None, o.default
assert o.validator == None, o.validator
assert o.converter == None, o.converter
o = opts.options[1]
assert o.key == 'ANSWER2', o.key
assert o.help == 'THE answer to THE question', o.help
assert o.default == "42", o.default
o.validator(o.key, o.converter(o.default), {})
def test_Update(self):
"""Test updating an Environment"""
# Test that a default value is validated correctly.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
opts = SCons.Variables.Variables(file)
opts.Add('ANSWER',
'THE answer to THE question',
"42",
check,
lambda x: int(x) + 12)
env = Environment()
opts.Update(env)
assert env['ANSWER'] == 54
env = Environment()
opts.Update(env, {})
assert env['ANSWER'] == 54
# Test that a bad value from the file is used and
# validation fails correctly.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
test.write('custom.py', 'ANSWER=54')
opts = SCons.Variables.Variables(file)
opts.Add('ANSWER',
'THE answer to THE question',
"42",
check,
lambda x: int(x) + 12)
env = Environment()
exc_caught = None
try:
opts.Update(env)
except AssertionError:
exc_caught = 1
assert exc_caught, "did not catch expected assertion"
env = Environment()
exc_caught = None
try:
opts.Update(env, {})
except AssertionError:
exc_caught = 1
assert exc_caught, "did not catch expected assertion"
# Test that a good value from the file is used and validated.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
test.write('custom.py', 'ANSWER=42')
opts = SCons.Variables.Variables(file)
opts.Add('ANSWER',
'THE answer to THE question',
"10",
check,
lambda x: int(x) + 12)
env = Environment()
opts.Update(env)
assert env['ANSWER'] == 54
env = Environment()
opts.Update(env, {})
assert env['ANSWER'] == 54
# Test that a bad value from an args dictionary passed to
# Update() is used and validation fails correctly.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
test.write('custom.py', 'ANSWER=10')
opts = SCons.Variables.Variables(file)
opts.Add('ANSWER',
'THE answer to THE question',
"12",
check,
lambda x: int(x) + 12)
env = Environment()
exc_caught = None
try:
opts.Update(env, {'ANSWER':'54'})
except AssertionError:
exc_caught = 1
assert exc_caught, "did not catch expected assertion"
# Test that a good value from an args dictionary
# passed to Update() is used and validated.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
test.write('custom.py', 'ANSWER=10')
opts = SCons.Variables.Variables(file)
opts.Add('ANSWER',
'THE answer to THE question',
"12",
check,
lambda x: int(x) + 12)
env = Environment()
opts.Update(env, {'ANSWER':'42'})
assert env['ANSWER'] == 54
# Test against a former bug. If we supply a converter,
# but no default, the value should *not* appear in the
# Environment if no value is specified in the options file
# or args.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
opts = SCons.Variables.Variables(file)
opts.Add('ANSWER',
help='THE answer to THE question',
converter=str)
env = Environment()
opts.Update(env, {})
assert not env.has_key('ANSWER')
# Test that a default value of None is all right.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
opts = SCons.Variables.Variables(file)
opts.Add('ANSWER',
"This is the answer",
None,
check)
env = Environment()
opts.Update(env, {})
assert not env.has_key('ANSWER')
def test_args(self):
"""Test updating an Environment with arguments overridden"""
# Test that a bad (command-line) argument is used
# and the validation fails correctly.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
test.write('custom.py', 'ANSWER=42')
opts = SCons.Variables.Variables(file, {'ANSWER':54})
opts.Add('ANSWER',
'THE answer to THE question',
"42",
check,
lambda x: int(x) + 12)
env = Environment()
exc_caught = None
try:
opts.Update(env)
except AssertionError:
exc_caught = 1
assert exc_caught, "did not catch expected assertion"
# Test that a good (command-line) argument is used and validated.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
test.write('custom.py', 'ANSWER=54')
opts = SCons.Variables.Variables(file, {'ANSWER':42})
opts.Add('ANSWER',
'THE answer to THE question',
"54",
check,
lambda x: int(x) + 12)
env = Environment()
opts.Update(env)
assert env['ANSWER'] == 54
# Test that a (command-line) argument is overridden by a dictionary
# supplied to Update() and the dictionary value is validated correctly.
test = TestSCons.TestSCons()
file = test.workpath('custom.py')
test.write('custom.py', 'ANSWER=54')
opts = SCons.Variables.Variables(file, {'ANSWER':54})
opts.Add('ANSWER',
'THE answer to THE question',
"54",
check,
lambda x: int(x) + 12)
env = Environment()
opts.Update(env, {'ANSWER':42})
assert env['ANSWER'] == 54
def test_Save(self):
"""Testing saving Variables"""
test = TestSCons.TestSCons()
cache_file = test.workpath('cached.options')
opts = SCons.Variables.Variables()
def bool_converter(val):
if val in [1, 'y']: val = 1
if val in [0, 'n']: val = 0
return val
# test saving out empty file
opts.Add('OPT_VAL',
'An option to test',
21,
None,
None)
opts.Add('OPT_VAL_2',
default='foo')
opts.Add('OPT_VAL_3',
default=1)
opts.Add('OPT_BOOL_0',
default='n',
converter=bool_converter)
opts.Add('OPT_BOOL_1',
default='y',
converter=bool_converter)
opts.Add('OPT_BOOL_2',
default=0,
converter=bool_converter)
env = Environment()
opts.Update(env, {'OPT_VAL_3' : 2})
assert env['OPT_VAL'] == 21, env['OPT_VAL']
assert env['OPT_VAL_2'] == 'foo', env['OPT_VAL_2']
assert env['OPT_VAL_3'] == 2, env['OPT_VAL_3']
assert env['OPT_BOOL_0'] == 0, env['OPT_BOOL_0']
assert env['OPT_BOOL_1'] == 1, env['OPT_BOOL_1']
assert env['OPT_BOOL_2'] == '0', env['OPT_BOOL_2']
env['OPT_VAL_2'] = 'bar'
env['OPT_BOOL_0'] = 0
env['OPT_BOOL_1'] = 1
env['OPT_BOOL_2'] = 2
opts.Save(cache_file, env)
checkSave(cache_file, { 'OPT_VAL_2' : 'bar',
'OPT_VAL_3' : 2,
'OPT_BOOL_2' : 2})
# Test against some old bugs
class Foo:
def __init__(self, x):
self.x = x
def __str__(self):
return self.x
test = TestSCons.TestSCons()
cache_file = test.workpath('cached.options')
opts = SCons.Variables.Variables()
opts.Add('THIS_USED_TO_BREAK',
'An option to test',
"Default")
opts.Add('THIS_ALSO_BROKE',
'An option to test',
"Default2")
opts.Add('THIS_SHOULD_WORK',
'An option to test',
Foo('bar'))
env = Environment()
opts.Update(env, { 'THIS_USED_TO_BREAK' : "Single'Quotes'In'String",
'THIS_ALSO_BROKE' : "\\Escape\nSequences\t",
'THIS_SHOULD_WORK' : Foo('baz') })
opts.Save(cache_file, env)
checkSave(cache_file, { 'THIS_USED_TO_BREAK' : "Single'Quotes'In'String",
'THIS_ALSO_BROKE' : "\\Escape\nSequences\t",
'THIS_SHOULD_WORK' : 'baz' })
def test_GenerateHelpText(self):
"""Test generating the default format help text"""
opts = SCons.Variables.Variables()
opts.Add('ANSWER',
'THE answer to THE question',
"42",
check,
lambda x: int(x) + 12)
opts.Add('B',
'b - alpha test',
"42",
check,
lambda x: int(x) + 12)
opts.Add('A',
'a - alpha test',
"42",
check,
lambda x: int(x) + 12)
env = Environment()
opts.Update(env, {})
expect = """
ANSWER: THE answer to THE question
default: 42
actual: 54
B: b - alpha test
default: 42
actual: 54
A: a - alpha test
default: 42
actual: 54
"""
text = opts.GenerateHelpText(env)
assert text == expect, text
expectAlpha = """
A: a - alpha test
default: 42
actual: 54
ANSWER: THE answer to THE question
default: 42
actual: 54
B: b - alpha test
default: 42
actual: 54
"""
text = opts.GenerateHelpText(env, sort=cmp)
assert text == expectAlpha, text
def test_FormatVariableHelpText(self):
"""Test generating custom format help text"""
opts = SCons.Variables.Variables()
def my_format(env, opt, help, default, actual, aliases):
return '%s %s %s %s %s\n' % (opt, default, actual, help, aliases)
opts.FormatVariableHelpText = my_format
opts.Add('ANSWER',
'THE answer to THE question',
"42",
check,
lambda x: int(x) + 12)
opts.Add('B',
'b - alpha test',
"42",
check,
lambda x: int(x) + 12)
opts.Add('A',
'a - alpha test',
"42",
check,
lambda x: int(x) + 12)
env = Environment()
opts.Update(env, {})
expect = """\
ANSWER 42 54 THE answer to THE question ['ANSWER']
B 42 54 b - alpha test ['B']
A 42 54 a - alpha test ['A']
"""
text = opts.GenerateHelpText(env)
assert text == expect, text
expectAlpha = """\
A 42 54 a - alpha test ['A']
ANSWER 42 54 THE answer to THE question ['ANSWER']
B 42 54 b - alpha test ['B']
"""
text = opts.GenerateHelpText(env, sort=cmp)
assert text == expectAlpha, text
class UnknownVariablesTestCase(unittest.TestCase):
def test_unknown(self):
"""Test the UnknownVariables() method"""
opts = SCons.Variables.Variables()
opts.Add('ANSWER',
'THE answer to THE question',
"42")
args = {
'ANSWER' : 'answer',
'UNKNOWN' : 'unknown',
}
env = Environment()
opts.Update(env, args)
r = opts.UnknownVariables()
assert r == {'UNKNOWN' : 'unknown'}, r
assert env['ANSWER'] == 'answer', env['ANSWER']
if __name__ == "__main__":
suite = unittest.TestSuite()
tclasses = [ VariablesTestCase,
UnknownVariablesTestCase ]
for tclass in tclasses:
names = unittest.getTestCaseNames(tclass, 'test_')
suite.addTests(map(tclass, names))
if not unittest.TextTestRunner().run(suite).wasSuccessful():
sys.exit(1)
# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4:
|