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
|
#!/usr/bin/env python
#
# __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 TestSCons
test = TestSCons.TestSCons()
test.write('SConstruct', """
env = Environment()
print(env['CC'])
print(" ".join(env['CCFLAGS']))
Default(env.Alias('dummy', None))
""")
test.run()
cc, ccflags = test.stdout().split('\n')[1:3]
test.write('SConstruct', """
# test validator. Change a key and add a new one to the environment
def validator(key, value, environ):
environ[key] = "v"
environ["valid_key"] = "v"
def old_converter (value):
return "old_converter"
def new_converter (value, env):
return "new_converter"
opts = Variables('custom.py')
opts.Add('RELEASE_BUILD',
'Set to 1 to build a release build',
0,
None,
int)
opts.Add('DEBUG_BUILD',
'Set to 1 to build a debug build',
1,
None,
int)
opts.Add('CC',
'The C compiler')
opts.Add('VALIDATE',
'An option for testing validation',
"notset",
validator,
None)
opts.Add('OLD_CONVERTER',
'An option for testing converters that take one parameter',
"foo",
None,
old_converter)
opts.Add('NEW_CONVERTER',
'An option for testing converters that take two parameters',
"foo",
None,
new_converter)
opts.Add('UNSPECIFIED',
'An option with no value')
def test_tool(env):
if env['RELEASE_BUILD']:
env.Append(CCFLAGS = '-O')
if env['DEBUG_BUILD']:
env.Append(CCFLAGS = '-g')
env = Environment(variables=opts, tools=['default', test_tool])
Help('Variables settable in custom.py or on the command line:\\n' + opts.GenerateHelpText(env))
print(env['RELEASE_BUILD'])
print(env['DEBUG_BUILD'])
print(env['CC'])
print(" ".join(env['CCFLAGS']))
print(env['VALIDATE'])
print(env['valid_key'])
# unspecified variables should not be set:
assert 'UNSPECIFIED' not in env
# undeclared variables should be ignored:
assert 'UNDECLARED' not in env
# calling Update() should not effect variables that
# are not declared on the variables object:
r = env['RELEASE_BUILD']
opts = Variables()
opts.Update(env)
assert env['RELEASE_BUILD'] == r
Default(env.Alias('dummy', None))
""")
def check(expect):
result = test.stdout().split('\n')
assert result[1:len(expect)+1] == expect, (result[1:len(expect)+1], expect)
test.run()
check(['0', '1', cc, (ccflags + ' -g').strip(), 'v', 'v'])
test.run(arguments='RELEASE_BUILD=1')
check(['1', '1', cc, (ccflags + ' -O -g').strip(), 'v', 'v'])
test.run(arguments='RELEASE_BUILD=1 DEBUG_BUILD=0')
check(['1', '0', cc, (ccflags + ' -O').strip(), 'v', 'v'])
test.run(arguments='CC=not_a_c_compiler')
check(['0', '1', 'not_a_c_compiler', (ccflags + ' -g').strip(), 'v', 'v'])
test.run(arguments='UNDECLARED=foo')
check(['0', '1', cc, (ccflags + ' -g').strip(), 'v', 'v'])
test.run(arguments='CCFLAGS=--taco')
check(['0', '1', cc, (ccflags + ' -g').strip(), 'v', 'v'])
test.write('custom.py', """
DEBUG_BUILD=0
RELEASE_BUILD=1
""")
test.run()
check(['1', '0', cc, (ccflags + ' -O').strip(), 'v', 'v'])
test.run(arguments='DEBUG_BUILD=1')
check(['1', '1', cc, (ccflags + ' -O -g').strip(), 'v', 'v'])
test.run(arguments='-h',
stdout = """\
scons: Reading SConscript files ...
1
0
%s
%s
v
v
scons: done reading SConscript files.
Variables settable in custom.py or on the command line:
RELEASE_BUILD: Set to 1 to build a release build
default: 0
actual: 1
DEBUG_BUILD: Set to 1 to build a debug build
default: 1
actual: 0
CC: The C compiler
default: None
actual: %s
VALIDATE: An option for testing validation
default: notset
actual: v
OLD_CONVERTER: An option for testing converters that take one parameter
default: foo
actual: old_converter
NEW_CONVERTER: An option for testing converters that take two parameters
default: foo
actual: new_converter
UNSPECIFIED: An option with no value
default: None
actual: None
Use scons -H for help about command-line options.
"""%(cc, ccflags and ccflags + ' -O' or '-O', cc))
# Test saving of variables and multi loading
#
test.write('SConstruct', """
opts = Variables(['custom.py', 'variables.saved'])
opts.Add('RELEASE_BUILD',
'Set to 1 to build a release build',
0,
None,
int)
opts.Add('DEBUG_BUILD',
'Set to 1 to build a debug build',
1,
None,
int)
opts.Add('UNSPECIFIED',
'An option with no value')
env = Environment(variables = opts)
print(env['RELEASE_BUILD'])
print(env['DEBUG_BUILD'])
opts.Save('variables.saved', env)
""")
# Check the save file by executing and comparing against
# the expected dictionary
def checkSave(file, expected):
gdict = {}
ldict = {}
with open(file, 'r') as f:
contents = f.read()
exec(contents, gdict, ldict)
assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
# First test with no command line variables
# This should just leave the custom.py settings
test.run()
check(['1','0'])
checkSave('variables.saved', { 'RELEASE_BUILD':1, 'DEBUG_BUILD':0})
# Override with command line arguments
test.run(arguments='DEBUG_BUILD=3')
check(['1','3'])
checkSave('variables.saved', {'RELEASE_BUILD':1, 'DEBUG_BUILD':3})
# Now make sure that saved variables are overridding the custom.py
test.run()
check(['1','3'])
checkSave('variables.saved', {'DEBUG_BUILD':3, 'RELEASE_BUILD':1})
# Load no variables from file(s)
# Used to test for correct output in save option file
test.write('SConstruct', """
opts = Variables()
opts.Add('RELEASE_BUILD',
'Set to 1 to build a release build',
'0',
None,
int)
opts.Add('DEBUG_BUILD',
'Set to 1 to build a debug build',
'1',
None,
int)
opts.Add('UNSPECIFIED',
'An option with no value')
opts.Add('LISTOPTION_TEST',
'testing list option persistence',
'none',
names = ['a','b','c',])
env = Environment(variables = opts)
print(env['RELEASE_BUILD'])
print(env['DEBUG_BUILD'])
print(env['LISTOPTION_TEST'])
opts.Save('variables.saved', env)
""")
# First check for empty output file when nothing is passed on command line
test.run()
check(['0','1'])
checkSave('variables.saved', {})
# Now specify one option the same as default and make sure it doesn't write out
test.run(arguments='DEBUG_BUILD=1')
check(['0','1'])
checkSave('variables.saved', {})
# Now specify same option non-default and make sure only it is written out
test.run(arguments='DEBUG_BUILD=0 LISTOPTION_TEST=a,b')
check(['0','0'])
checkSave('variables.saved',{'DEBUG_BUILD':0, 'LISTOPTION_TEST':'a,b'})
test.write('SConstruct', """
opts = Variables('custom.py')
opts.Add('RELEASE_BUILD',
'Set to 1 to build a release build',
0,
None,
int)
opts.Add('DEBUG_BUILD',
'Set to 1 to build a debug build',
1,
None,
int)
opts.Add('CC',
'The C compiler')
opts.Add('UNSPECIFIED',
'An option with no value')
env = Environment(variables=opts)
def compare(a, b):
return (a > b) - (a < b)
Help('Variables settable in custom.py or on the command line:\\n' + opts.GenerateHelpText(env,sort=compare))
""")
test.run(arguments='-h',
stdout = """\
scons: Reading SConscript files ...
scons: done reading SConscript files.
Variables settable in custom.py or on the command line:
CC: The C compiler
default: None
actual: %s
DEBUG_BUILD: Set to 1 to build a debug build
default: 1
actual: 0
RELEASE_BUILD: Set to 1 to build a release build
default: 0
actual: 1
UNSPECIFIED: An option with no value
default: None
actual: None
Use scons -H for help about command-line options.
"""%cc)
test.write('SConstruct', """
import SCons.Variables
env1 = Environment(variables = Variables())
env2 = Environment(variables = SCons.Variables.Variables())
""")
test.run()
test.pass_test()
# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4:
|