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"
50 opts = Options('custom.py')
51 opts.Add('RELEASE_BUILD',
52 'Set to 1 to build a release build',
57 opts.Add('DEBUG_BUILD',
58 'Set to 1 to build a debug build',
67 'An option for testing validation',
72 opts.Add('UNSPECIFIED',
73 'An option with no value')
76 if env['RELEASE_BUILD']:
77 env.Append(CCFLAGS = '-O')
78 if env['DEBUG_BUILD']:
79 env.Append(CCFLAGS = '-g')
82 env = Environment(options=opts, tools=['default', test_tool])
84 Help('Variables settable in custom.py or on the command line:\\n' + opts.GenerateHelpText(env))
86 print env['RELEASE_BUILD']
87 print env['DEBUG_BUILD']
89 print string.join(env['CCFLAGS'])
91 print env['valid_key']
93 # unspecified options should not be set:
94 assert not env.has_key('UNSPECIFIED')
96 # undeclared options should be ignored:
97 assert not env.has_key('UNDECLARED')
99 # calling Update() should not effect options that
100 # are not declared on the options object:
101 r = env['RELEASE_BUILD']
104 assert env['RELEASE_BUILD'] == r
106 Default(env.Alias('dummy', None))
111 result = string.split(test.stdout(), '\n')
112 assert result[1:len(expect)+1] == expect, (result[1:len(expect)+1], expect)
115 check(['0', '1', cc, string.strip(ccflags + ' -g'), 'v', 'v'])
117 test.run(arguments='RELEASE_BUILD=1')
118 check(['1', '1', cc, string.strip(ccflags + ' -O -g'), 'v', 'v'])
120 test.run(arguments='RELEASE_BUILD=1 DEBUG_BUILD=0')
121 check(['1', '0', cc, string.strip(ccflags + ' -O'), 'v', 'v'])
123 test.run(arguments='CC=not_a_c_compiler')
124 check(['0', '1', 'not_a_c_compiler', string.strip(ccflags + ' -g'), 'v', 'v'])
126 test.run(arguments='UNDECLARED=foo')
127 check(['0', '1', cc, string.strip(ccflags + ' -g'), 'v', 'v'])
129 test.run(arguments='CCFLAGS=--taco')
130 check(['0', '1', cc, string.strip(ccflags + ' -g'), 'v', 'v'])
132 test.write('custom.py', """
138 check(['1', '0', cc, string.strip(ccflags + ' -O'), 'v', 'v'])
140 test.run(arguments='DEBUG_BUILD=1')
141 check(['1', '1', cc, string.strip(ccflags + ' -O -g'), 'v', 'v'])
143 test.run(arguments='-h',
145 scons: Reading SConscript files ...
152 scons: done reading SConscript files.
153 Variables settable in custom.py or on the command line:
155 RELEASE_BUILD: Set to 1 to build a release build
159 DEBUG_BUILD: Set to 1 to build a debug build
167 VALIDATE: An option for testing validation
171 UNSPECIFIED: An option with no value
175 Use scons -H for help about command-line options.
176 """%(cc, ccflags and ccflags + ' -O' or '-O', cc))
178 # Test saving of options and multi loading
180 test.write('SConstruct', """
181 opts = Options(['custom.py', 'options.saved'])
182 opts.Add('RELEASE_BUILD',
183 'Set to 1 to build a release build',
188 opts.Add('DEBUG_BUILD',
189 'Set to 1 to build a debug build',
194 opts.Add('UNSPECIFIED',
195 'An option with no value')
197 env = Environment(options = opts)
199 print env['RELEASE_BUILD']
200 print env['DEBUG_BUILD']
202 opts.Save('options.saved', env)
205 # Check the save file by executing and comparing against
206 # the expected dictionary
207 def checkSave(file, expected):
210 execfile(file, gdict, ldict)
211 assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
213 # First test with no command line options
214 # This should just leave the custom.py settings
217 checkSave('options.saved', { 'RELEASE_BUILD':1, 'DEBUG_BUILD':0})
219 # Override with command line arguments
220 test.run(arguments='DEBUG_BUILD=3')
222 checkSave('options.saved', {'RELEASE_BUILD':1, 'DEBUG_BUILD':3})
224 # Now make sure that saved options are overridding the custom.py
227 checkSave('options.saved', {'DEBUG_BUILD':3, 'RELEASE_BUILD':1})
229 # Load no options from file(s)
230 # Used to test for correct output in save option file
231 test.write('SConstruct', """
233 opts.Add('RELEASE_BUILD',
234 'Set to 1 to build a release build',
239 opts.Add('DEBUG_BUILD',
240 'Set to 1 to build a debug build',
245 opts.Add('UNSPECIFIED',
246 'An option with no value')
248 opts.Add('LISTOPTION_TEST',
249 'testing list option persistence',
251 names = ['a','b','c',])
253 env = Environment(options = opts)
255 print env['RELEASE_BUILD']
256 print env['DEBUG_BUILD']
257 print env['LISTOPTION_TEST']
259 opts.Save('options.saved', env)
262 # First check for empty output file when nothing is passed on command line
265 checkSave('options.saved', {})
267 # Now specify one option the same as default and make sure it doesn't write out
268 test.run(arguments='DEBUG_BUILD=1')
270 checkSave('options.saved', {})
272 # Now specify same option non-default and make sure only it is written out
273 test.run(arguments='DEBUG_BUILD=0 LISTOPTION_TEST=a,b')
275 checkSave('options.saved',{'DEBUG_BUILD':0, 'LISTOPTION_TEST':'a,b'})
277 test.write('SConstruct', """
278 opts = Options('custom.py')
279 opts.Add('RELEASE_BUILD',
280 'Set to 1 to build a release build',
285 opts.Add('DEBUG_BUILD',
286 'Set to 1 to build a debug build',
294 opts.Add('UNSPECIFIED',
295 'An option with no value')
297 env = Environment(options=opts)
299 Help('Variables settable in custom.py or on the command line:\\n' + opts.GenerateHelpText(env,sort=cmp))
303 test.run(arguments='-h',
305 scons: Reading SConscript files ...
306 scons: done reading SConscript files.
307 Variables settable in custom.py or on the command line:
313 DEBUG_BUILD: Set to 1 to build a debug build
317 RELEASE_BUILD: Set to 1 to build a release build
321 UNSPECIFIED: An option with no value
325 Use scons -H for help about command-line options.
328 test.write('SConstruct', """
330 env1 = Environment(options = Options())
331 env2 = Environment(options = SCons.Options.Options())