added release.txt blurb. Fixed spelling typo in Defaults.xml
[scons.git] / test / Variables / PathVariable.py
blobeffbd49dc29ce15c279d305c7200e4859890ffd4
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 PathVariable canned variable type, with tests for its
28 various canned validators.
29 """
31 import os.path
33 import TestSCons
35 test = TestSCons.TestSCons()
37 SConstruct_path = test.workpath('SConstruct')
39 def check(expect):
40 result = test.stdout().split('\n')
41 assert result[1:len(expect)+1] == expect, (result[1:len(expect)+1], expect)
44 test.subdir('lib', 'qt', ['qt', 'lib'], 'nolib')
45 workpath = test.workpath()
47 test.write(SConstruct_path, """\
48 from SCons.Variables.PathVariable import PathVariable as PV
49 from SCons.Variables import PathVariable
51 qtdir = r'%s'
53 opts = Variables(args=ARGUMENTS)
54 opts.AddVariables(
55 PathVariable('qtdir', 'where the root of Qt is installed', qtdir),
56 PV('qt_libraries', 'where the Qt library is installed', r'%s'),
59 _ = DefaultEnvironment(tools=[]) # test speedup
60 env = Environment(variables=opts, tools=[])
61 Help(opts.GenerateHelpText(env))
63 print(env['qtdir'])
64 print(env['qt_libraries'])
65 print(env.subst('$qt_libraries'))
67 Default(env.Alias('dummy', None))
68 """ % (workpath, os.path.join('$qtdir', 'lib')))
70 qtpath = workpath
71 libpath = os.path.join(qtpath, 'lib')
72 test.run()
73 check([qtpath, os.path.join('$qtdir', 'lib'), libpath])
75 qtpath = os.path.join(workpath, 'qt')
76 libpath = os.path.join(qtpath, 'lib')
77 test.run(arguments=['qtdir=%s' % qtpath])
78 check([qtpath, os.path.join('$qtdir', 'lib'), libpath])
80 qtpath = workpath
81 libpath = os.path.join(qtpath, 'nolib')
82 test.run(arguments=['qt_libraries=%s' % libpath])
83 check([qtpath, libpath, libpath])
85 qtpath = os.path.join(workpath, 'qt')
86 libpath = os.path.join(workpath, 'nolib')
87 test.run(arguments=['qtdir=%s' % qtpath, 'qt_libraries=%s' % libpath])
88 check([qtpath, libpath, libpath])
90 qtpath = os.path.join(workpath, 'non', 'existing', 'path')
91 SConstruct_file_line = test.python_file_line(test.workpath('SConstruct'), 13)[:-1]
93 expect_stderr = """
94 scons: *** Path for variable 'qtdir' does not exist: %(qtpath)s
95 %(SConstruct_file_line)s
96 """ % locals()
98 test.run(arguments=['qtdir=%s' % qtpath], stderr=expect_stderr, status=2)
100 expect_stderr = """
101 scons: *** Path for variable 'qt_libraries' does not exist: %(qtpath)s
102 %(SConstruct_file_line)s
103 """ % locals()
105 test.run(arguments=['qt_libraries=%s' % qtpath], stderr=expect_stderr, status=2)
107 default_file = test.workpath('default_file')
108 default_subdir = test.workpath('default_subdir')
109 existing_subdir = test.workpath('existing_subdir')
110 test.subdir(existing_subdir)
112 existing_file = test.workpath('existing_file')
113 test.write(existing_file, "existing_file\n")
115 non_existing_subdir = test.workpath('non_existing_subdir')
116 non_existing_file = test.workpath('non_existing_file')
118 test.write('SConstruct', """\
119 opts = Variables(args=ARGUMENTS)
120 opts.AddVariables(
121 PathVariable('X', 'X variable', r'%s', validator=PathVariable.PathAccept),
124 DefaultEnvironment(tools=[]) # test speedup
125 env = Environment(variables=opts)
127 print(env['X'])
129 Default(env.Alias('dummy', None))
130 """ % default_subdir)
132 test.run()
133 check([default_subdir])
135 test.run(arguments=['X=%s' % existing_file])
136 check([existing_file])
138 test.run(arguments=['X=%s' % non_existing_file])
139 check([non_existing_file])
141 test.run(arguments=['X=%s' % existing_subdir])
142 check([existing_subdir])
144 test.run(arguments=['X=%s' % non_existing_subdir])
145 check([non_existing_subdir])
147 test.must_not_exist(non_existing_file)
148 test.must_not_exist(non_existing_subdir)
150 test.write(SConstruct_path, """\
151 opts = Variables(args=ARGUMENTS)
152 opts.AddVariables(
153 PathVariable('X', 'X variable', r'%s', validator=PathVariable.PathIsFile),
156 DefaultEnvironment(tools=[]) # test speedup
157 env = Environment(variables=opts)
159 print(env['X'])
161 Default(env.Alias('dummy', None))
162 """ % default_file)
164 SConstruct_file_line = test.python_file_line(test.workpath('SConstruct'), 7)[:-1]
166 expect_stderr = """
167 scons: *** File path for variable 'X' does not exist: %(default_file)s
168 %(SConstruct_file_line)s
169 """ % locals()
171 test.run(status=2, stderr=expect_stderr)
173 test.write(default_file, "default_file\n")
174 test.run()
175 check([default_file])
177 expect_stderr = """
178 scons: *** File path for variable 'X' is a directory: %(existing_subdir)s
179 %(SConstruct_file_line)s
180 """ % locals()
182 test.run(arguments=['X=%s' % existing_subdir], status=2, stderr=expect_stderr)
184 test.run(arguments=['X=%s' % existing_file])
185 check([existing_file])
187 expect_stderr = """
188 scons: *** File path for variable 'X' does not exist: %(non_existing_file)s
189 %(SConstruct_file_line)s
190 """ % locals()
192 test.run(arguments=['X=%s' % non_existing_file], status=2, stderr=expect_stderr)
194 test.write('SConstruct', """\
195 opts = Variables(args=ARGUMENTS)
196 opts.AddVariables(
197 PathVariable('X', 'X variable', r'%s', validator=PathVariable.PathIsDir),
200 DefaultEnvironment(tools=[]) # test speedup
201 env = Environment(variables=opts)
203 print(env['X'])
205 Default(env.Alias('dummy', None))
206 """ % default_subdir)
208 expect_stderr = """
209 scons: *** Directory path for variable 'X' does not exist: %(default_subdir)s
210 %(SConstruct_file_line)s
211 """ % locals()
213 test.run(status=2, stderr=expect_stderr)
215 test.subdir(default_subdir)
216 test.run()
217 check([default_subdir])
219 expect_stderr = """
220 scons: *** Directory path for variable 'X' is a file: %(existing_file)s
221 %(SConstruct_file_line)s
222 """ % locals()
224 test.run(arguments=['X=%s' % existing_file],
225 status=2,
226 stderr=expect_stderr)
228 test.run(arguments=['X=%s' % existing_subdir])
229 check([existing_subdir])
231 expect_stderr = """
232 scons: *** Directory path for variable 'X' does not exist: %(non_existing_subdir)s
233 %(SConstruct_file_line)s
234 """ % locals()
236 test.run(arguments=['X=%s' % non_existing_subdir],
237 status=2,
238 stderr=expect_stderr)
240 test.write('SConstruct', """\
241 opts = Variables(args=ARGUMENTS)
242 opts.AddVariables(
243 PathVariable('X', 'X variable', r'%s', validator=PathVariable.PathIsDirCreate),
246 DefaultEnvironment(tools=[]) # test speedup
247 env = Environment(variables=opts)
249 print(env['X'])
251 Default(env.Alias('dummy', None))
252 """ % default_subdir)
254 test.run()
255 check([default_subdir])
257 expect_stderr = """
258 scons: *** Path for variable 'X' is a file, not a directory: %(existing_file)s
259 %(SConstruct_file_line)s
260 """ % locals()
262 test.run(arguments=['X=%s' % existing_file], status=2, stderr=expect_stderr)
264 test.run(arguments=['X=%s' % existing_subdir])
265 check([existing_subdir])
267 test.run(arguments=['X=%s' % non_existing_subdir])
268 check([non_existing_subdir])
270 test.must_exist(non_existing_subdir)
272 test.pass_test()
274 # Local Variables:
275 # tab-width:4
276 # indent-tabs-mode:nil
277 # End:
278 # vim: set expandtab tabstop=4 shiftwidth=4: