Eliminate unnecessary WIN32/Win32/win32 references in tests, too.
[scons.git] / test / Options.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
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:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
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.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 import TestSCons
28 import string
29
30 test = TestSCons.TestSCons()
31
32 test.write('SConstruct', """
33 import string
34 env = Environment()
35 print env['CC']
36 print string.join(env['CCFLAGS'])
37 Default(env.Alias('dummy', None))
38 """)
39 test.run()
40 cc, ccflags = string.split(test.stdout(), '\n')[1:3]
41
42 test.write('SConstruct', """
43 import string
44
45 # test validator.  Change a key and add a new one to the environment
46 def validator(key, value, environ):
47     environ[key] = "v"
48     environ["valid_key"] = "v"
49
50 opts = Options('custom.py')
51 opts.Add('RELEASE_BUILD',
52          'Set to 1 to build a release build',
53          0,
54          None,
55          int)
56
57 opts.Add('DEBUG_BUILD',
58          'Set to 1 to build a debug build',
59          1,
60          None,
61          int)
62
63 opts.Add('CC',
64          'The C compiler')
65
66 opts.Add('VALIDATE',
67          'An option for testing validation',
68          "notset",
69          validator,
70          None)
71
72 opts.Add('UNSPECIFIED',
73          'An option with no value')
74
75 def test_tool(env):
76     if env['RELEASE_BUILD']:
77         env.Append(CCFLAGS = '-O')
78     if env['DEBUG_BUILD']:
79         env.Append(CCFLAGS = '-g')
80
81
82 env = Environment(options=opts, tools=['default', test_tool])
83
84 Help('Variables settable in custom.py or on the command line:\\n' + opts.GenerateHelpText(env))
85
86 print env['RELEASE_BUILD']
87 print env['DEBUG_BUILD']
88 print env['CC']
89 print string.join(env['CCFLAGS'])
90 print env['VALIDATE']
91 print env['valid_key']
92
93 # unspecified options should not be set:
94 assert not env.has_key('UNSPECIFIED')
95
96 # undeclared options should be ignored:
97 assert not env.has_key('UNDECLARED')
98
99 # calling Update() should not effect options that
100 # are not declared on the options object:
101 r = env['RELEASE_BUILD']
102 opts = Options()
103 opts.Update(env)
104 assert env['RELEASE_BUILD'] == r
105
106 Default(env.Alias('dummy', None))
107
108 """)
109
110 def check(expect):
111     result = string.split(test.stdout(), '\n')
112     assert result[1:len(expect)+1] == expect, (result[1:len(expect)+1], expect)
113
114 test.run()
115 check(['0', '1', cc, string.strip(ccflags + ' -g'), 'v', 'v'])
116
117 test.run(arguments='RELEASE_BUILD=1')
118 check(['1', '1', cc, string.strip(ccflags + ' -O -g'), 'v', 'v'])
119
120 test.run(arguments='RELEASE_BUILD=1 DEBUG_BUILD=0')
121 check(['1', '0', cc, string.strip(ccflags + ' -O'), 'v', 'v'])
122
123 test.run(arguments='CC=not_a_c_compiler')
124 check(['0', '1', 'not_a_c_compiler', string.strip(ccflags + ' -g'), 'v', 'v'])
125
126 test.run(arguments='UNDECLARED=foo')
127 check(['0', '1', cc, string.strip(ccflags + ' -g'), 'v', 'v'])
128
129 test.run(arguments='CCFLAGS=--taco')
130 check(['0', '1', cc, string.strip(ccflags + ' -g'), 'v', 'v'])
131
132 test.write('custom.py', """
133 DEBUG_BUILD=0
134 RELEASE_BUILD=1
135 """)
136
137 test.run()
138 check(['1', '0', cc, string.strip(ccflags + ' -O'), 'v', 'v'])
139
140 test.run(arguments='DEBUG_BUILD=1')
141 check(['1', '1', cc, string.strip(ccflags + ' -O -g'), 'v', 'v'])
142
143 test.run(arguments='-h',
144          stdout = """\
145 scons: Reading SConscript files ...
146 1
147 0
148 %s
149 %s
150 v
151 v
152 scons: done reading SConscript files.
153 Variables settable in custom.py or on the command line:
154
155 RELEASE_BUILD: Set to 1 to build a release build
156     default: 0
157     actual: 1
158
159 DEBUG_BUILD: Set to 1 to build a debug build
160     default: 1
161     actual: 0
162
163 CC: The C compiler
164     default: None
165     actual: %s
166
167 VALIDATE: An option for testing validation
168     default: notset
169     actual: v
170
171 UNSPECIFIED: An option with no value
172     default: None
173     actual: None
174
175 Use scons -H for help about command-line options.
176 """%(cc, ccflags and ccflags + ' -O' or '-O', cc))
177
178 # Test saving of options and multi loading
179 #
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',
184          0,
185          None,
186          int)
187
188 opts.Add('DEBUG_BUILD',
189          'Set to 1 to build a debug build',
190          1,
191          None,
192          int)
193
194 opts.Add('UNSPECIFIED',
195          'An option with no value')
196
197 env = Environment(options = opts)
198
199 print env['RELEASE_BUILD']
200 print env['DEBUG_BUILD']
201
202 opts.Save('options.saved', env)
203 """)
204
205 # Check the save file by executing and comparing against
206 # the expected dictionary
207 def checkSave(file, expected):
208     gdict = {}
209     ldict = {}
210     execfile(file, gdict, ldict)
211     assert expected == ldict, "%s\n...not equal to...\n%s" % (expected, ldict)
212
213 # First test with no command line options
214 # This should just leave the custom.py settings
215 test.run()
216 check(['1','0'])
217 checkSave('options.saved', { 'RELEASE_BUILD':1, 'DEBUG_BUILD':0})
218
219 # Override with command line arguments
220 test.run(arguments='DEBUG_BUILD=3')
221 check(['1','3'])
222 checkSave('options.saved', {'RELEASE_BUILD':1, 'DEBUG_BUILD':3})
223
224 # Now make sure that saved options are overridding the custom.py
225 test.run()
226 check(['1','3'])
227 checkSave('options.saved', {'DEBUG_BUILD':3, 'RELEASE_BUILD':1})
228
229 # Load no options from file(s)
230 # Used to test for correct output in save option file
231 test.write('SConstruct', """
232 opts = Options()
233 opts.Add('RELEASE_BUILD',
234          'Set to 1 to build a release build',
235          '0',
236          None,
237          int)
238
239 opts.Add('DEBUG_BUILD',
240          'Set to 1 to build a debug build',
241          '1',
242          None,
243          int)
244
245 opts.Add('UNSPECIFIED',
246          'An option with no value')
247
248 opts.Add('LISTOPTION_TEST',
249          'testing list option persistence',
250          'none',
251          names = ['a','b','c',])
252
253 env = Environment(options = opts)
254
255 print env['RELEASE_BUILD']
256 print env['DEBUG_BUILD']
257 print env['LISTOPTION_TEST']
258
259 opts.Save('options.saved', env)
260 """)
261
262 # First check for empty output file when nothing is passed on command line
263 test.run()
264 check(['0','1'])
265 checkSave('options.saved', {})
266
267 # Now specify one option the same as default and make sure it doesn't write out
268 test.run(arguments='DEBUG_BUILD=1')
269 check(['0','1'])
270 checkSave('options.saved', {})
271
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')
274 check(['0','0'])
275 checkSave('options.saved',{'DEBUG_BUILD':0, 'LISTOPTION_TEST':'a,b'})
276
277 test.write('SConstruct', """
278 opts = Options('custom.py')
279 opts.Add('RELEASE_BUILD',
280          'Set to 1 to build a release build',
281          0,
282          None,
283          int)
284
285 opts.Add('DEBUG_BUILD',
286          'Set to 1 to build a debug build',
287          1,
288          None,
289          int)
290
291 opts.Add('CC',
292          'The C compiler')
293
294 opts.Add('UNSPECIFIED',
295          'An option with no value')
296
297 env = Environment(options=opts)
298
299 Help('Variables settable in custom.py or on the command line:\\n' + opts.GenerateHelpText(env,sort=cmp))
300
301 """)
302
303 test.run(arguments='-h',
304          stdout = """\
305 scons: Reading SConscript files ...
306 scons: done reading SConscript files.
307 Variables settable in custom.py or on the command line:
308
309 CC: The C compiler
310     default: None
311     actual: %s
312
313 DEBUG_BUILD: Set to 1 to build a debug build
314     default: 1
315     actual: 0
316
317 RELEASE_BUILD: Set to 1 to build a release build
318     default: 0
319     actual: 1
320
321 UNSPECIFIED: An option with no value
322     default: None
323     actual: None
324
325 Use scons -H for help about command-line options.
326 """%cc)
327
328 test.write('SConstruct', """
329 import SCons.Options
330 env1 = Environment(options = Options())
331 env2 = Environment(options = SCons.Options.Options())
332 """)
333
334 test.run()
335
336 test.pass_test()