3fa7e818d2b3de789e7d926ab465c5305eb7b6ec
[scons.git] / test / Deprecated / Options / PathOption.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 """
28 Test the PathOption canned option type, with tests for its 
29 various canned validators.
30 """
31
32 import os.path
33 import string
34
35 import TestSCons
36
37 test = TestSCons.TestSCons()
38
39 SConstruct_path = test.workpath('SConstruct')
40
41 def check(expect):
42     result = string.split(test.stdout(), '\n')
43     assert result[1:len(expect)+1] == expect, (result[1:len(expect)+1], expect)
44
45 #### test PathOption ####
46
47 test.subdir('lib', 'qt', ['qt', 'lib'], 'nolib' )
48 workpath = test.workpath()
49 libpath = os.path.join(workpath, 'lib')
50
51 test.write(SConstruct_path, """\
52 from SCons.Options.PathOption import PathOption
53 PO = PathOption
54
55 from SCons.Options import PathOption
56
57 qtdir = r'%s'
58
59 opts = Options(args=ARGUMENTS)
60 opts.AddOptions(
61     PathOption('qtdir', 'where the root of Qt is installed', qtdir),
62     PO('qt_libraries', 'where the Qt library is installed', r'%s'),
63     )
64
65 env = Environment(options=opts)
66 Help(opts.GenerateHelpText(env))
67
68 print env['qtdir']
69 print env['qt_libraries']
70 print env.subst('$qt_libraries')
71
72 Default(env.Alias('dummy', None))
73 """ % (workpath, os.path.join('$qtdir', 'lib') ))
74
75 qtpath = workpath
76 libpath = os.path.join(qtpath, 'lib')
77 test.run()
78 check([qtpath, os.path.join('$qtdir', 'lib'), libpath])
79
80 qtpath = os.path.join(workpath, 'qt')
81 libpath = os.path.join(qtpath, 'lib')
82 test.run(arguments=['qtdir=%s' % qtpath])
83 check([qtpath, os.path.join('$qtdir', 'lib'), libpath])
84
85 qtpath = workpath
86 libpath = os.path.join(qtpath, 'nolib')
87 test.run(arguments=['qt_libraries=%s' % libpath])
88 check([qtpath, libpath, libpath])
89
90 qtpath = os.path.join(workpath, 'qt')
91 libpath = os.path.join(workpath, 'nolib')
92 test.run(arguments=['qtdir=%s' % qtpath, 'qt_libraries=%s' % libpath])
93 check([qtpath, libpath, libpath])
94
95 qtpath = os.path.join(workpath, 'non', 'existing', 'path')
96 SConstruct_file_line = test.python_file_line(test.workpath('SConstruct'), 14)[:-1]
97
98 expect_stderr = """
99 scons: *** Path for option qtdir does not exist: %(qtpath)s
100 %(SConstruct_file_line)s
101 """ % locals()
102
103 test.run(arguments=['qtdir=%s' % qtpath], stderr=expect_stderr, status=2)
104
105 expect_stderr = """
106 scons: *** Path for option qt_libraries does not exist: %(qtpath)s
107 %(SConstruct_file_line)s
108 """ % locals()
109
110 test.run(arguments=['qt_libraries=%s' % qtpath], stderr=expect_stderr, status=2)
111
112
113
114 default_file = test.workpath('default_file')
115 default_subdir = test.workpath('default_subdir')
116
117 existing_subdir = test.workpath('existing_subdir')
118 test.subdir(existing_subdir)
119
120 existing_file = test.workpath('existing_file')
121 test.write(existing_file, "existing_file\n")
122
123 non_existing_subdir = test.workpath('non_existing_subdir')
124 non_existing_file = test.workpath('non_existing_file')
125
126
127
128 test.write('SConstruct', """\
129 opts = Options(args=ARGUMENTS)
130 opts.AddOptions(
131     PathOption('X', 'X variable', r'%s', validator=PathOption.PathAccept),
132     )
133
134 env = Environment(options=opts)
135
136 print env['X']
137
138 Default(env.Alias('dummy', None))
139 """ % default_subdir)
140
141 test.run()
142 check([default_subdir])
143
144 test.run(arguments=['X=%s' % existing_file])
145 check([existing_file])
146
147 test.run(arguments=['X=%s' % non_existing_file])
148 check([non_existing_file])
149
150 test.run(arguments=['X=%s' % existing_subdir])
151 check([existing_subdir])
152
153 test.run(arguments=['X=%s' % non_existing_subdir])
154 check([non_existing_subdir])
155
156 test.must_not_exist(non_existing_file)
157 test.must_not_exist(non_existing_subdir)
158
159
160
161 test.write(SConstruct_path, """\
162 opts = Options(args=ARGUMENTS)
163 opts.AddOptions(
164     PathOption('X', 'X variable', r'%s', validator=PathOption.PathIsFile),
165     )
166
167 env = Environment(options=opts)
168
169 print env['X']
170
171 Default(env.Alias('dummy', None))
172 """ % default_file)
173
174 SConstruct_file_line = test.python_file_line(test.workpath('SConstruct'), 6)[:-1]
175
176 expect_stderr = """
177 scons: *** File path for option X does not exist: %(default_file)s
178 %(SConstruct_file_line)s
179 """ % locals()
180
181 test.run(status=2, stderr=expect_stderr)
182
183 test.write(default_file, "default_file\n")
184
185 test.run()
186 check([default_file])
187
188 expect_stderr = """
189 scons: *** File path for option X is a directory: %(existing_subdir)s
190 %(SConstruct_file_line)s
191 """ % locals()
192
193 test.run(arguments=['X=%s' % existing_subdir], status=2, stderr=expect_stderr)
194
195 test.run(arguments=['X=%s' % existing_file])
196 check([existing_file])
197
198 expect_stderr = """
199 scons: *** File path for option X does not exist: %(non_existing_file)s
200 %(SConstruct_file_line)s
201 """ % locals()
202
203 test.run(arguments=['X=%s' % non_existing_file], status=2, stderr=expect_stderr)
204
205
206
207 test.write('SConstruct', """\
208 opts = Options(args=ARGUMENTS)
209 opts.AddOptions(
210     PathOption('X', 'X variable', r'%s', validator=PathOption.PathIsDir),
211     )
212
213 env = Environment(options=opts)
214
215 print env['X']
216
217 Default(env.Alias('dummy', None))
218 """ % default_subdir)
219
220 expect_stderr = """
221 scons: *** Directory path for option X does not exist: %(default_subdir)s
222 %(SConstruct_file_line)s
223 """ % locals()
224
225 test.run(status=2, stderr=expect_stderr)
226
227 test.subdir(default_subdir)
228
229 test.run()
230 check([default_subdir])
231
232 expect_stderr = """
233 scons: *** Directory path for option X is a file: %(existing_file)s
234 %(SConstruct_file_line)s
235 """ % locals()
236
237 test.run(arguments=['X=%s' % existing_file],
238          status=2,
239          stderr=expect_stderr)
240
241 test.run(arguments=['X=%s' % existing_subdir])
242 check([existing_subdir])
243
244 expect_stderr = """
245 scons: *** Directory path for option X does not exist: %(non_existing_subdir)s
246 %(SConstruct_file_line)s
247 """ % locals()
248
249 test.run(arguments=['X=%s' % non_existing_subdir],
250          status=2,
251          stderr=expect_stderr)
252
253
254
255 test.write('SConstruct', """\
256 opts = Options(args=ARGUMENTS)
257 opts.AddOptions(
258     PathOption('X', 'X variable', r'%s', validator=PathOption.PathIsDirCreate),
259     )
260
261 env = Environment(options=opts)
262
263 print env['X']
264
265 Default(env.Alias('dummy', None))
266 """ % default_subdir)
267
268 test.run()
269 check([default_subdir])
270
271 expect_stderr = """
272 scons: *** Path for option X is a file, not a directory: %(existing_file)s
273 %(SConstruct_file_line)s
274 """ % locals()
275
276 test.run(arguments=['X=%s' % existing_file], status=2, stderr=expect_stderr)
277
278 test.run(arguments=['X=%s' % existing_subdir])
279 check([existing_subdir])
280
281 test.run(arguments=['X=%s' % non_existing_subdir])
282 check([non_existing_subdir])
283
284 test.must_exist(non_existing_subdir)
285
286
287
288 test.pass_test()