4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
40 return SCons.Util.scons_subst(x, self, gvars=self.dict)
41 def __setitem__(self, key, value):
42 self.dict[key] = value
43 def __getitem__(self, key):
45 def has_key(self, key):
46 return self.dict.has_key(key)
49 def check(key, value, env):
50 assert int(value) == 6 * 9, "key %s = %s" % (key, repr(value))
52 # Check saved option file by executing and comparing against
53 # the expected dictionary
54 def checkSave(file, expected):
57 execfile(file, gdict, ldict)
58 assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
60 class OptionsTestCase(unittest.TestCase):
63 """Test adding to an Options object"""
64 opts = SCons.Options.Options()
68 'THE answer to THE question',
71 lambda x: int(x) + 12)
76 assert o.default == None
77 assert o.validator == None
78 assert o.converter == None
81 assert o.key == 'ANSWER'
82 assert o.help == 'THE answer to THE question'
83 assert o.default == "42"
84 o.validator(o.key, o.converter(o.default), {})
86 def test_it(var, opts=opts):
90 except SCons.Errors.UserError:
92 assert exc_caught, "did not catch UserError for '%s'" % var
97 def test_AddOptions(self):
98 """Test adding a list of options to an Options object"""
99 opts = SCons.Options.Options()
101 opts.AddOptions(('VAR2',),
103 'THE answer to THE question',
106 lambda x: int(x) + 12))
109 assert o.key == 'VAR2', o.key
110 assert o.help == '', o.help
111 assert o.default == None, o.default
112 assert o.validator == None, o.validator
113 assert o.converter == None, o.converter
116 assert o.key == 'ANSWER2', o.key
117 assert o.help == 'THE answer to THE question', o.help
118 assert o.default == "42", o.default
119 o.validator(o.key, o.converter(o.default), {})
121 def test_Update(self):
122 """Test updating an Environment"""
124 # Test that a default value is validated correctly.
125 test = TestSCons.TestSCons()
126 file = test.workpath('custom.py')
127 opts = SCons.Options.Options(file)
130 'THE answer to THE question',
133 lambda x: int(x) + 12)
137 assert env['ANSWER'] == 54
141 assert env['ANSWER'] == 54
143 # Test that a bad value from the file is used and
144 # validation fails correctly.
145 test = TestSCons.TestSCons()
146 file = test.workpath('custom.py')
147 test.write('custom.py', 'ANSWER=54')
148 opts = SCons.Options.Options(file)
151 'THE answer to THE question',
154 lambda x: int(x) + 12)
160 except AssertionError:
162 assert exc_caught, "did not catch expected assertion"
168 except AssertionError:
170 assert exc_caught, "did not catch expected assertion"
172 # Test that a good value from the file is used and validated.
173 test = TestSCons.TestSCons()
174 file = test.workpath('custom.py')
175 test.write('custom.py', 'ANSWER=42')
176 opts = SCons.Options.Options(file)
179 'THE answer to THE question',
182 lambda x: int(x) + 12)
186 assert env['ANSWER'] == 54
190 assert env['ANSWER'] == 54
192 # Test that a bad value from an args dictionary passed to
193 # Update() is used and validation fails correctly.
194 test = TestSCons.TestSCons()
195 file = test.workpath('custom.py')
196 test.write('custom.py', 'ANSWER=10')
197 opts = SCons.Options.Options(file)
200 'THE answer to THE question',
203 lambda x: int(x) + 12)
208 opts.Update(env, {'ANSWER':'54'})
209 except AssertionError:
211 assert exc_caught, "did not catch expected assertion"
213 # Test that a good value from an args dictionary
214 # passed to Update() is used and validated.
215 test = TestSCons.TestSCons()
216 file = test.workpath('custom.py')
217 test.write('custom.py', 'ANSWER=10')
218 opts = SCons.Options.Options(file)
221 'THE answer to THE question',
224 lambda x: int(x) + 12)
227 opts.Update(env, {'ANSWER':'42'})
228 assert env['ANSWER'] == 54
230 # Test against a former bug. If we supply a converter,
231 # but no default, the value should *not* appear in the
232 # Environment if no value is specified in the options file
234 test = TestSCons.TestSCons()
235 file = test.workpath('custom.py')
236 opts = SCons.Options.Options(file)
239 help='THE answer to THE question',
244 assert not env.has_key('ANSWER')
246 # Test that a default value of None is all right.
247 test = TestSCons.TestSCons()
248 file = test.workpath('custom.py')
249 opts = SCons.Options.Options(file)
252 "This is the answer",
258 assert not env.has_key('ANSWER')
261 """Test updating an Environment with arguments overridden"""
263 # Test that a bad (command-line) argument is used
264 # and the validation fails correctly.
265 test = TestSCons.TestSCons()
266 file = test.workpath('custom.py')
267 test.write('custom.py', 'ANSWER=42')
268 opts = SCons.Options.Options(file, {'ANSWER':54})
271 'THE answer to THE question',
274 lambda x: int(x) + 12)
280 except AssertionError:
282 assert exc_caught, "did not catch expected assertion"
284 # Test that a good (command-line) argument is used and validated.
285 test = TestSCons.TestSCons()
286 file = test.workpath('custom.py')
287 test.write('custom.py', 'ANSWER=54')
288 opts = SCons.Options.Options(file, {'ANSWER':42})
291 'THE answer to THE question',
294 lambda x: int(x) + 12)
298 assert env['ANSWER'] == 54
300 # Test that a (command-line) argument is overridden by a dictionary
301 # supplied to Update() and the dictionary value is validated correctly.
302 test = TestSCons.TestSCons()
303 file = test.workpath('custom.py')
304 test.write('custom.py', 'ANSWER=54')
305 opts = SCons.Options.Options(file, {'ANSWER':54})
308 'THE answer to THE question',
311 lambda x: int(x) + 12)
314 opts.Update(env, {'ANSWER':42})
315 assert env['ANSWER'] == 54
318 """Testing saving Options"""
320 test = TestSCons.TestSCons()
321 cache_file = test.workpath('cached.options')
322 opts = SCons.Options.Options()
324 # test saving out empty file
330 opts.Add('OPT_VAL_2',
332 opts.Add('OPT_VAL_3',
336 opts.Update(env, {'OPT_VAL_3' : 2})
337 assert env['OPT_VAL'] == 21
338 assert env['OPT_VAL_2'] == 'foo'
339 assert env['OPT_VAL_3'] == 2
340 env['OPT_VAL_2'] = 'bar'
341 opts.Save(cache_file, env)
342 checkSave(cache_file, { 'OPT_VAL_2' : 'bar',
345 # Test against some old bugs
347 def __init__(self, x):
352 test = TestSCons.TestSCons()
353 cache_file = test.workpath('cached.options')
354 opts = SCons.Options.Options()
356 opts.Add('THIS_USED_TO_BREAK',
360 opts.Add('THIS_ALSO_BROKE',
364 opts.Add('THIS_SHOULD_WORK',
369 opts.Update(env, { 'THIS_USED_TO_BREAK' : "Single'Quotes'In'String",
370 'THIS_ALSO_BROKE' : "\\Escape\nSequences\t",
371 'THIS_SHOULD_WORK' : Foo('baz') })
372 opts.Save(cache_file, env)
373 checkSave(cache_file, { 'THIS_USED_TO_BREAK' : "Single'Quotes'In'String",
374 'THIS_ALSO_BROKE' : "\\Escape\nSequences\t",
375 'THIS_SHOULD_WORK' : 'baz' })
377 def test_GenerateHelpText(self):
378 """Test generating the default format help text"""
379 opts = SCons.Options.Options()
382 'THE answer to THE question',
385 lambda x: int(x) + 12)
391 lambda x: int(x) + 12)
397 lambda x: int(x) + 12)
403 ANSWER: THE answer to THE question
416 text = opts.GenerateHelpText(env)
417 assert text == expect, text
424 ANSWER: THE answer to THE question
432 text = opts.GenerateHelpText(env, sort=cmp)
433 assert text == expectAlpha, text
435 def test_FormatOptionHelpText(self):
436 """Test generating custom format help text"""
437 opts = SCons.Options.Options()
439 def my_format(env, opt, help, default, actual):
440 return '%s %s %s %s\n' % (opt, default, actual, help)
442 opts.FormatOptionHelpText = my_format
445 'THE answer to THE question',
448 lambda x: int(x) + 12)
454 lambda x: int(x) + 12)
460 lambda x: int(x) + 12)
466 ANSWER 42 54 THE answer to THE question
467 B 42 54 b - alpha test
468 A 42 54 a - alpha test
471 text = opts.GenerateHelpText(env)
472 assert text == expect, text
475 A 42 54 a - alpha test
476 ANSWER 42 54 THE answer to THE question
477 B 42 54 b - alpha test
479 text = opts.GenerateHelpText(env, sort=cmp)
480 assert text == expectAlpha, text
485 if __name__ == "__main__":
486 suite = unittest.makeSuite(OptionsTestCase, 'test_')
487 if not unittest.TextTestRunner().run(suite).wasSuccessful():