Updates to PR 4374 from mwichmann to correct config file hash changes
[scons.git] / test / Variables / ListVariable.py
blob0067c5677f7ecc03ba0de7b5a99e4193623121a6
1 #!/usr/bin/env python
3 # MIT License
5 # Copyright The SCons Foundation
7 # Permission is hereby granted, free of charge, to any person obtaining
8 # a copy of this software and associated documentation files (the
9 # "Software"), to deal in the Software without restriction, including
10 # without limitation the rights to use, copy, modify, merge, publish,
11 # distribute, sublicense, and/or sell copies of the Software, and to
12 # permit persons to whom the Software is furnished to do so, subject to
13 # the following conditions:
15 # The above copyright notice and this permission notice shall be included
16 # in all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 """
27 Test the ListVariable canned Variable type.
28 """
30 import os
32 import TestSCons
34 test = TestSCons.TestSCons()
36 SConstruct_path = test.workpath('SConstruct')
38 def check(expect):
39 result = test.stdout().split('\n')
40 r = result[1:len(expect)+1]
41 assert r == expect, (r, expect)
45 test.write(SConstruct_path, """\
46 from SCons.Variables.ListVariable import ListVariable
47 LV = ListVariable
49 from SCons.Variables import ListVariable
51 list_of_libs = Split('x11 gl qt ical')
53 optsfile = 'scons.variables'
54 opts = Variables(optsfile, args=ARGUMENTS)
55 opts.AddVariables(
56 ListVariable('shared',
57 'libraries to build as shared libraries',
58 'all',
59 names = list_of_libs,
60 map = {'GL':'gl', 'QT':'qt'}),
61 LV('listvariable', 'listvariable help', 'all', names=['l1', 'l2', 'l3'])
64 DefaultEnvironment(tools=[]) # test speedup
65 env = Environment(variables=opts)
66 opts.Save(optsfile, env)
67 Help(opts.GenerateHelpText(env))
69 print(env['shared'])
71 if 'ical' in env['shared']:
72 print('1')
73 else:
74 print('0')
76 print(" ".join(env['shared']))
78 print(env.subst('$shared'))
79 # Test subst_path() because it's used in $CPPDEFINES expansions.
80 print(env.subst_path('$shared'))
81 Default(env.Alias('dummy', None))
82 """)
84 test.run()
85 check(['all', '1', 'gl ical qt x11', 'gl ical qt x11',
86 "['gl ical qt x11']"])
88 expect = "shared = 'all'"+os.linesep+"listvariable = 'all'"+os.linesep
89 test.must_match(test.workpath('scons.variables'), expect)
91 check(['all', '1', 'gl ical qt x11', 'gl ical qt x11',
92 "['gl ical qt x11']"])
94 test.run(arguments='shared=none')
95 check(['none', '0', '', '', "['']"])
97 test.run(arguments='shared=')
98 check(['none', '0', '', '', "['']"])
100 test.run(arguments='shared=x11,ical')
101 check(['ical,x11', '1', 'ical x11', 'ical x11',
102 "['ical x11']"])
104 test.run(arguments='shared=x11,,ical,,')
105 check(['ical,x11', '1', 'ical x11', 'ical x11',
106 "['ical x11']"])
108 test.run(arguments='shared=GL')
109 check(['gl', '0', 'gl', 'gl'])
111 test.run(arguments='shared=QT,GL')
112 check(['gl,qt', '0', 'gl qt', 'gl qt', "['gl qt']"])
115 expect_stderr = """
116 scons: *** Error converting option: shared
117 Invalid value(s) for option: foo
118 """ + test.python_file_line(SConstruct_path, 20)
120 test.run(arguments='shared=foo', stderr=expect_stderr, status=2)
122 # be paranoid in testing some more combinations
124 expect_stderr = """
125 scons: *** Error converting option: shared
126 Invalid value(s) for option: foo
127 """ + test.python_file_line(SConstruct_path, 20)
129 test.run(arguments='shared=foo,ical', stderr=expect_stderr, status=2)
131 expect_stderr = """
132 scons: *** Error converting option: shared
133 Invalid value(s) for option: foo
134 """ + test.python_file_line(SConstruct_path, 20)
136 test.run(arguments='shared=ical,foo', stderr=expect_stderr, status=2)
138 expect_stderr = """
139 scons: *** Error converting option: shared
140 Invalid value(s) for option: foo
141 """ + test.python_file_line(SConstruct_path, 20)
143 test.run(arguments='shared=ical,foo,x11', stderr=expect_stderr, status=2)
145 expect_stderr = """
146 scons: *** Error converting option: shared
147 Invalid value(s) for option: foo,bar
148 """ + test.python_file_line(SConstruct_path, 20)
150 test.run(arguments='shared=foo,x11,,,bar', stderr=expect_stderr, status=2)
154 test.write('SConstruct', """
155 from SCons.Variables import ListVariable
157 opts = Variables(args=ARGUMENTS)
158 opts.AddVariables(
159 ListVariable('gpib',
160 'comment',
161 ['ENET', 'GPIB'],
162 names = ['ENET', 'GPIB', 'LINUX_GPIB', 'NO_GPIB']),
165 DefaultEnvironment(tools=[]) # test speedup
166 env = Environment(variables=opts)
167 Help(opts.GenerateHelpText(env))
169 print(env['gpib'])
170 Default(env.Alias('dummy', None))
171 """)
173 test.run(stdout=test.wrap_stdout(read_str="ENET,GPIB\n", build_str="""\
174 scons: Nothing to be done for `dummy'.
175 """))
179 test.pass_test()
181 # Local Variables:
182 # tab-width:4
183 # indent-tabs-mode:nil
184 # End:
185 # vim: set expandtab tabstop=4 shiftwidth=4: