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 Default(env.Alias('dummy', None))
39 cc, ccflags = string.split(test.stdout(), '\n')[1:3]
41 test.write('SConstruct', """
42 # test validator. Change a key and add a new one to the environment
43 def validator(key, value, environ):
45 environ["valid_key"] = "v"
47 opts = Options('custom.py')
48 opts.Add('RELEASE_BUILD',
49 'Set to 1 to build a release build',
54 opts.Add('DEBUG_BUILD',
55 'Set to 1 to build a debug build',
64 'An option for testing validation',
69 opts.Add('UNSPECIFIED',
70 'An option with no value')
73 if env['RELEASE_BUILD']:
74 env['CCFLAGS'] = env['CCFLAGS'] + ' -O'
75 if env['DEBUG_BUILD']:
76 env['CCFLAGS'] = env['CCFLAGS'] + ' -g'
79 env = Environment(options=opts, tools=['default', test_tool])
81 Help('Variables settable in custom.py or on the command line:\\n' + opts.GenerateHelpText(env))
83 print env['RELEASE_BUILD']
84 print env['DEBUG_BUILD']
88 print env['valid_key']
90 # unspecified options should not be set:
91 assert not env.has_key('UNSPECIFIED')
93 # undeclared options should be ignored:
94 assert not env.has_key('UNDECLARED')
96 # calling Update() should not effect options that
97 # are not declared on the options object:
98 r = env['RELEASE_BUILD']
101 assert env['RELEASE_BUILD'] == r
103 Default(env.Alias('dummy', None))
108 result = string.split(test.stdout(), '\n')
109 assert result[1:len(expect)+1] == expect, (result[1:len(expect)+1], expect)
112 check(['0', '1', cc, ccflags + ' -g', 'v', 'v'])
114 test.run(arguments='"RELEASE_BUILD=1"')
115 check(['1', '1', cc, ccflags + ' -O -g', 'v', 'v'])
117 test.run(arguments='"RELEASE_BUILD=1" "DEBUG_BUILD=0"')
118 check(['1', '0', cc, ccflags + ' -O', 'v', 'v'])
120 test.run(arguments='"CC=not_a_c_compiler"')
121 check(['0', '1', 'not_a_c_compiler', ccflags + ' -g', 'v', 'v'])
123 test.run(arguments='"UNDECLARED=foo"')
124 check(['0', '1', cc, ccflags + ' -g', 'v', 'v'])
126 test.run(arguments='"CCFLAGS=--taco"')
127 check(['0', '1', cc, ccflags + ' -g', 'v', 'v'])
129 test.write('custom.py', """
135 check(['1', '0', cc, ccflags + ' -O', 'v', 'v'])
137 test.run(arguments='"DEBUG_BUILD=1"')
138 check(['1', '1', cc, ccflags + ' -O -g', 'v', 'v'])
140 test.run(arguments='-h',
141 stdout = """scons: Reading SConscript files ...
142 scons: done reading SConscript files.
143 Variables settable in custom.py or on the command line:
145 RELEASE_BUILD: Set to 1 to build a release build
149 DEBUG_BUILD: Set to 1 to build a debug build
157 VALIDATE: An option for testing validation
161 UNSPECIFIED: An option with no value
165 Use scons -H for help about command-line options.
168 # Test saving of options and multi loading
170 test.write('SConstruct', """
171 opts = Options(['custom.py', 'options.saved'])
172 opts.Add('RELEASE_BUILD',
173 'Set to 1 to build a release build',
178 opts.Add('DEBUG_BUILD',
179 'Set to 1 to build a debug build',
184 opts.Add('UNSPECIFIED',
185 'An option with no value')
187 env = Environment(options = opts)
189 print env['RELEASE_BUILD']
190 print env['DEBUG_BUILD']
192 opts.Save('options.saved', env)
195 # Check the save file by executing and comparing against
196 # the expected dictionary
197 def checkSave(file, expected):
200 execfile(file, gdict, ldict)
201 assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
203 # First test with no command line options
204 # This should just leave the custom.py settings
207 checkSave('options.saved', { 'RELEASE_BUILD':1, 'DEBUG_BUILD':0})
209 # Override with command line arguments
210 test.run(arguments='"DEBUG_BUILD=3"')
212 checkSave('options.saved', {'RELEASE_BUILD':1, 'DEBUG_BUILD':3})
214 # Now make sure that saved options are overridding the custom.py
217 checkSave('options.saved', {'DEBUG_BUILD':3, 'RELEASE_BUILD':1})
219 # Load no options from file(s)
220 # Used to test for correct output in save option file
221 test.write('SConstruct', """
223 opts.Add('RELEASE_BUILD',
224 'Set to 1 to build a release build',
229 opts.Add('DEBUG_BUILD',
230 'Set to 1 to build a debug build',
235 opts.Add('UNSPECIFIED',
236 'An option with no value')
238 env = Environment(options = opts)
240 print env['RELEASE_BUILD']
241 print env['DEBUG_BUILD']
243 opts.Save('options.saved', env)
246 # First check for empty output file when nothing is passed on command line
249 checkSave('options.saved', {})
251 # Now specify one option the same as default and make sure it doesn't write out
252 test.run(arguments='"DEBUG_BUILD=1"')
254 checkSave('options.saved', {})
256 # Now specify same option non-default and make sure only it is written out
257 test.run(arguments='"DEBUG_BUILD=0"')
259 checkSave('options.saved',{'DEBUG_BUILD':0})
261 test.write('SConstruct', """
262 opts = Options('custom.py')
263 opts.Add('RELEASE_BUILD',
264 'Set to 1 to build a release build',
269 opts.Add('DEBUG_BUILD',
270 'Set to 1 to build a debug build',
278 opts.Add('UNSPECIFIED',
279 'An option with no value')
281 env = Environment(options=opts)
283 Help('Variables settable in custom.py or on the command line:\\n' + opts.GenerateHelpText(env,sort=cmp))
287 test.run(arguments='-h',
288 stdout = """scons: Reading SConscript files ...
289 scons: done reading SConscript files.
290 Variables settable in custom.py or on the command line:
296 DEBUG_BUILD: Set to 1 to build a debug build
300 RELEASE_BUILD: Set to 1 to build a release build
304 UNSPECIFIED: An option with no value
308 Use scons -H for help about command-line options.
311 test.write('SConstruct', """
313 env1 = Environment(options = Options())
314 env2 = Environment(options = SCons.Options.Options())