5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
30 test = TestSCons.TestSCons()
32 test.write('SConstruct', """
36 print string.join(env['CCFLAGS'])
37 Default(env.Alias('dummy', None))
40 cc, ccflags = string.split(test.stdout(), '\n')[1:3]
42 test.write('SConstruct', """
45 # test validator. Change a key and add a new one to the environment
46 def validator(key, value, environ):
48 environ["valid_key"] = "v"
51 def old_converter (value):
52 return "old_converter"
54 def new_converter (value, env):
55 return "new_converter"
58 opts = Variables('custom.py')
59 opts.Add('RELEASE_BUILD',
60 'Set to 1 to build a release build',
65 opts.Add('DEBUG_BUILD',
66 'Set to 1 to build a debug build',
75 'An option for testing validation',
80 opts.Add('OLD_CONVERTER',
81 'An option for testing converters that take one parameter',
86 opts.Add('NEW_CONVERTER',
87 'An option for testing converters that take two parameters',
92 opts.Add('UNSPECIFIED',
93 'An option with no value')
96 if env['RELEASE_BUILD']:
97 env.Append(CCFLAGS = '-O')
98 if env['DEBUG_BUILD']:
99 env.Append(CCFLAGS = '-g')
102 env = Environment(variables=opts, tools=['default', test_tool])
104 Help('Variables settable in custom.py or on the command line:\\n' + opts.GenerateHelpText(env))
106 print env['RELEASE_BUILD']
107 print env['DEBUG_BUILD']
109 print string.join(env['CCFLAGS'])
110 print env['VALIDATE']
111 print env['valid_key']
113 # unspecified variables should not be set:
114 assert not env.has_key('UNSPECIFIED')
116 # undeclared variables should be ignored:
117 assert not env.has_key('UNDECLARED')
119 # calling Update() should not effect variables that
120 # are not declared on the variables object:
121 r = env['RELEASE_BUILD']
124 assert env['RELEASE_BUILD'] == r
126 Default(env.Alias('dummy', None))
131 result = string.split(test.stdout(), '\n')
132 assert result[1:len(expect)+1] == expect, (result[1:len(expect)+1], expect)
135 check(['0', '1', cc, string.strip(ccflags + ' -g'), 'v', 'v'])
137 test.run(arguments='RELEASE_BUILD=1')
138 check(['1', '1', cc, string.strip(ccflags + ' -O -g'), 'v', 'v'])
140 test.run(arguments='RELEASE_BUILD=1 DEBUG_BUILD=0')
141 check(['1', '0', cc, string.strip(ccflags + ' -O'), 'v', 'v'])
143 test.run(arguments='CC=not_a_c_compiler')
144 check(['0', '1', 'not_a_c_compiler', string.strip(ccflags + ' -g'), 'v', 'v'])
146 test.run(arguments='UNDECLARED=foo')
147 check(['0', '1', cc, string.strip(ccflags + ' -g'), 'v', 'v'])
149 test.run(arguments='CCFLAGS=--taco')
150 check(['0', '1', cc, string.strip(ccflags + ' -g'), 'v', 'v'])
152 test.write('custom.py', """
158 check(['1', '0', cc, string.strip(ccflags + ' -O'), 'v', 'v'])
160 test.run(arguments='DEBUG_BUILD=1')
161 check(['1', '1', cc, string.strip(ccflags + ' -O -g'), 'v', 'v'])
163 test.run(arguments='-h',
165 scons: Reading SConscript files ...
172 scons: done reading SConscript files.
173 Variables settable in custom.py or on the command line:
175 RELEASE_BUILD: Set to 1 to build a release build
179 DEBUG_BUILD: Set to 1 to build a debug build
187 VALIDATE: An option for testing validation
191 OLD_CONVERTER: An option for testing converters that take one parameter
193 actual: old_converter
195 NEW_CONVERTER: An option for testing converters that take two parameters
197 actual: new_converter
199 UNSPECIFIED: An option with no value
203 Use scons -H for help about command-line options.
204 """%(cc, ccflags and ccflags + ' -O' or '-O', cc))
206 # Test saving of variables and multi loading
208 test.write('SConstruct', """
209 opts = Variables(['custom.py', 'variables.saved'])
210 opts.Add('RELEASE_BUILD',
211 'Set to 1 to build a release build',
216 opts.Add('DEBUG_BUILD',
217 'Set to 1 to build a debug build',
222 opts.Add('UNSPECIFIED',
223 'An option with no value')
225 env = Environment(variables = opts)
227 print env['RELEASE_BUILD']
228 print env['DEBUG_BUILD']
230 opts.Save('variables.saved', env)
233 # Check the save file by executing and comparing against
234 # the expected dictionary
235 def checkSave(file, expected):
238 execfile(file, gdict, ldict)
239 assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
241 # First test with no command line variables
242 # This should just leave the custom.py settings
245 checkSave('variables.saved', { 'RELEASE_BUILD':1, 'DEBUG_BUILD':0})
247 # Override with command line arguments
248 test.run(arguments='DEBUG_BUILD=3')
250 checkSave('variables.saved', {'RELEASE_BUILD':1, 'DEBUG_BUILD':3})
252 # Now make sure that saved variables are overridding the custom.py
255 checkSave('variables.saved', {'DEBUG_BUILD':3, 'RELEASE_BUILD':1})
257 # Load no variables from file(s)
258 # Used to test for correct output in save option file
259 test.write('SConstruct', """
261 opts.Add('RELEASE_BUILD',
262 'Set to 1 to build a release build',
267 opts.Add('DEBUG_BUILD',
268 'Set to 1 to build a debug build',
273 opts.Add('UNSPECIFIED',
274 'An option with no value')
276 opts.Add('LISTOPTION_TEST',
277 'testing list option persistence',
279 names = ['a','b','c',])
281 env = Environment(variables = opts)
283 print env['RELEASE_BUILD']
284 print env['DEBUG_BUILD']
285 print env['LISTOPTION_TEST']
287 opts.Save('variables.saved', env)
290 # First check for empty output file when nothing is passed on command line
293 checkSave('variables.saved', {})
295 # Now specify one option the same as default and make sure it doesn't write out
296 test.run(arguments='DEBUG_BUILD=1')
298 checkSave('variables.saved', {})
300 # Now specify same option non-default and make sure only it is written out
301 test.run(arguments='DEBUG_BUILD=0 LISTOPTION_TEST=a,b')
303 checkSave('variables.saved',{'DEBUG_BUILD':0, 'LISTOPTION_TEST':'a,b'})
305 test.write('SConstruct', """
306 opts = Variables('custom.py')
307 opts.Add('RELEASE_BUILD',
308 'Set to 1 to build a release build',
313 opts.Add('DEBUG_BUILD',
314 'Set to 1 to build a debug build',
322 opts.Add('UNSPECIFIED',
323 'An option with no value')
325 env = Environment(variables=opts)
327 Help('Variables settable in custom.py or on the command line:\\n' + opts.GenerateHelpText(env,sort=cmp))
331 test.run(arguments='-h',
333 scons: Reading SConscript files ...
334 scons: done reading SConscript files.
335 Variables settable in custom.py or on the command line:
341 DEBUG_BUILD: Set to 1 to build a debug build
345 RELEASE_BUILD: Set to 1 to build a release build
349 UNSPECIFIED: An option with no value
353 Use scons -H for help about command-line options.
356 test.write('SConstruct', """
357 import SCons.Variables
358 env1 = Environment(variables = Variables())
359 env2 = Environment(variables = SCons.Variables.Variables())
368 # indent-tabs-mode:nil
370 # vim: set expandtab tabstop=4 shiftwidth=4: