http://scons.tigris.org/issues/show_bug.cgi?id=2345
[scons.git] / src / engine / SCons / Variables / PathVariableTests.py
1 #
2 # __COPYRIGHT__
3 #
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:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
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.
22 #
23
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
25
26 import os.path
27 import sys
28 import unittest
29
30 import SCons.Errors
31 import SCons.Variables
32
33 import TestCmd
34
35 class PathVariableTestCase(unittest.TestCase):
36     def test_PathVariable(self):
37         """Test PathVariable creation"""
38         opts = SCons.Variables.Variables()
39         opts.Add(SCons.Variables.PathVariable('test',
40                                           'test option help',
41                                           '/default/path'))
42
43         o = opts.options[0]
44         assert o.key == 'test', o.key
45         assert o.help == 'test option help ( /path/to/test )', repr(o.help)
46         assert o.default == '/default/path', o.default
47         assert o.validator is not None, o.validator
48         assert o.converter is None, o.converter
49
50     def test_PathExists(self):
51         """Test the PathExists validator"""
52         opts = SCons.Variables.Variables()
53         opts.Add(SCons.Variables.PathVariable('test',
54                                           'test option help',
55                                           '/default/path',
56                                           SCons.Variables.PathVariable.PathExists))
57
58         test = TestCmd.TestCmd(workdir='')
59         test.write('exists', 'exists\n')
60
61         o = opts.options[0]
62
63         o.validator('X', test.workpath('exists'), {})
64
65         dne = test.workpath('does_not_exist')
66         try:
67             o.validator('X', dne, {})
68         except SCons.Errors.UserError, e:
69             assert str(e) == 'Path for option X does not exist: %s' % dne, e
70         except:
71             raise Exception("did not catch expected UserError")
72
73     def test_PathIsDir(self):
74         """Test the PathIsDir validator"""
75         opts = SCons.Variables.Variables()
76         opts.Add(SCons.Variables.PathVariable('test',
77                                           'test option help',
78                                           '/default/path',
79                                           SCons.Variables.PathVariable.PathIsDir))
80
81         test = TestCmd.TestCmd(workdir='')
82         test.subdir('dir')
83         test.write('file', "file\n")
84
85         o = opts.options[0]
86
87         o.validator('X', test.workpath('dir'), {})
88
89         f = test.workpath('file')
90         try:
91             o.validator('X', f, {})
92         except SCons.Errors.UserError, e:
93             assert str(e) == 'Directory path for option X is a file: %s' % f, e
94         except:
95             raise Exception("did not catch expected UserError")
96
97         dne = test.workpath('does_not_exist')
98         try:
99             o.validator('X', dne, {})
100         except SCons.Errors.UserError, e:
101             assert str(e) == 'Directory path for option X does not exist: %s' % dne, e
102         except:
103             raise Exception("did not catch expected UserError")
104
105     def test_PathIsDirCreate(self):
106         """Test the PathIsDirCreate validator"""
107         opts = SCons.Variables.Variables()
108         opts.Add(SCons.Variables.PathVariable('test',
109                                           'test option help',
110                                           '/default/path',
111                                           SCons.Variables.PathVariable.PathIsDirCreate))
112
113         test = TestCmd.TestCmd(workdir='')
114         test.write('file', "file\n")
115
116         o = opts.options[0]
117
118         d = test.workpath('dir')
119         o.validator('X', d, {})
120         assert os.path.isdir(d)
121
122         f = test.workpath('file')
123         try:
124             o.validator('X', f, {})
125         except SCons.Errors.UserError, e:
126             assert str(e) == 'Path for option X is a file, not a directory: %s' % f, e
127         except:
128             raise Exception("did not catch expected UserError")
129
130     def test_PathIsFile(self):
131         """Test the PathIsFile validator"""
132         opts = SCons.Variables.Variables()
133         opts.Add(SCons.Variables.PathVariable('test',
134                                           'test option help',
135                                           '/default/path',
136                                           SCons.Variables.PathVariable.PathIsFile))
137
138         test = TestCmd.TestCmd(workdir='')
139         test.subdir('dir')
140         test.write('file', "file\n")
141
142         o = opts.options[0]
143
144         o.validator('X', test.workpath('file'), {})
145
146         d = test.workpath('d')
147         try:
148             o.validator('X', d, {})
149         except SCons.Errors.UserError, e:
150             assert str(e) == 'File path for option X does not exist: %s' % d, e
151         except:
152             raise Exception("did not catch expected UserError")
153
154         dne = test.workpath('does_not_exist')
155         try:
156             o.validator('X', dne, {})
157         except SCons.Errors.UserError, e:
158             assert str(e) == 'File path for option X does not exist: %s' % dne, e
159         except:
160             raise Exception("did not catch expected UserError")
161
162     def test_PathAccept(self):
163         """Test the PathAccept validator"""
164         opts = SCons.Variables.Variables()
165         opts.Add(SCons.Variables.PathVariable('test',
166                                           'test option help',
167                                           '/default/path',
168                                           SCons.Variables.PathVariable.PathAccept))
169
170         test = TestCmd.TestCmd(workdir='')
171         test.subdir('dir')
172         test.write('file', "file\n")
173
174         o = opts.options[0]
175
176         o.validator('X', test.workpath('file'), {})
177
178         d = test.workpath('d')
179         o.validator('X', d, {})
180
181         dne = test.workpath('does_not_exist')
182         o.validator('X', dne, {})
183
184     def test_validator(self):
185         """Test the PathVariable validator argument"""
186         opts = SCons.Variables.Variables()
187         opts.Add(SCons.Variables.PathVariable('test',
188                                           'test option help',
189                                           '/default/path'))
190
191         test = TestCmd.TestCmd(workdir='')
192         test.write('exists', 'exists\n')
193
194         o = opts.options[0]
195
196         o.validator('X', test.workpath('exists'), {})
197
198         dne = test.workpath('does_not_exist')
199         try:
200             o.validator('X', dne, {})
201         except SCons.Errors.UserError, e:
202             expect = 'Path for option X does not exist: %s' % dne
203             assert str(e) == expect, e
204         else:
205             raise Exception("did not catch expected UserError")
206
207         def my_validator(key, val, env):
208             raise Exception("my_validator() got called for %s, %s!" % (key, val))
209
210         opts = SCons.Variables.Variables()
211         opts.Add(SCons.Variables.PathVariable('test2',
212                                           'more help',
213                                           '/default/path/again',
214                                           my_validator))
215
216         o = opts.options[0]
217
218         try:
219             o.validator('Y', 'value', {})
220         except Exception, e:
221             assert str(e) == 'my_validator() got called for Y, value!', e
222         else:
223             raise Exception("did not catch expected exception from my_validator()")
224
225
226
227
228 if __name__ == "__main__":
229     suite = unittest.makeSuite(PathVariableTestCase, 'test_')
230     if not unittest.TextTestRunner().run(suite).wasSuccessful():
231         sys.exit(1)
232
233 # Local Variables:
234 # tab-width:4
235 # indent-tabs-mode:nil
236 # End:
237 # vim: set expandtab tabstop=4 shiftwidth=4: