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:
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
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.
24 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
31 import SCons.Variables
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',
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
50 def test_PathExists(self):
51 """Test the PathExists validator"""
52 opts = SCons.Variables.Variables()
53 opts.Add(SCons.Variables.PathVariable('test',
56 SCons.Variables.PathVariable.PathExists))
58 test = TestCmd.TestCmd(workdir='')
59 test.write('exists', 'exists\n')
63 o.validator('X', test.workpath('exists'), {})
65 dne = test.workpath('does_not_exist')
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
71 raise "did not catch expected UserError"
73 def test_PathIsDir(self):
74 """Test the PathIsDir validator"""
75 opts = SCons.Variables.Variables()
76 opts.Add(SCons.Variables.PathVariable('test',
79 SCons.Variables.PathVariable.PathIsDir))
81 test = TestCmd.TestCmd(workdir='')
83 test.write('file', "file\n")
87 o.validator('X', test.workpath('dir'), {})
89 f = test.workpath('file')
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
95 raise "did not catch expected UserError"
97 dne = test.workpath('does_not_exist')
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
103 raise "did not catch expected UserError"
105 def test_PathIsDirCreate(self):
106 """Test the PathIsDirCreate validator"""
107 opts = SCons.Variables.Variables()
108 opts.Add(SCons.Variables.PathVariable('test',
111 SCons.Variables.PathVariable.PathIsDirCreate))
113 test = TestCmd.TestCmd(workdir='')
114 test.write('file', "file\n")
118 d = test.workpath('dir')
119 o.validator('X', d, {})
120 assert os.path.isdir(d)
122 f = test.workpath('file')
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
128 raise "did not catch expected UserError"
130 def test_PathIsFile(self):
131 """Test the PathIsFile validator"""
132 opts = SCons.Variables.Variables()
133 opts.Add(SCons.Variables.PathVariable('test',
136 SCons.Variables.PathVariable.PathIsFile))
138 test = TestCmd.TestCmd(workdir='')
140 test.write('file', "file\n")
144 o.validator('X', test.workpath('file'), {})
146 d = test.workpath('d')
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
152 raise "did not catch expected UserError"
154 dne = test.workpath('does_not_exist')
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
160 raise "did not catch expected UserError"
162 def test_PathAccept(self):
163 """Test the PathAccept validator"""
164 opts = SCons.Variables.Variables()
165 opts.Add(SCons.Variables.PathVariable('test',
168 SCons.Variables.PathVariable.PathAccept))
170 test = TestCmd.TestCmd(workdir='')
172 test.write('file', "file\n")
176 o.validator('X', test.workpath('file'), {})
178 d = test.workpath('d')
179 o.validator('X', d, {})
181 dne = test.workpath('does_not_exist')
182 o.validator('X', dne, {})
184 def test_validator(self):
185 """Test the PathVariable validator argument"""
186 opts = SCons.Variables.Variables()
187 opts.Add(SCons.Variables.PathVariable('test',
191 test = TestCmd.TestCmd(workdir='')
192 test.write('exists', 'exists\n')
196 o.validator('X', test.workpath('exists'), {})
198 dne = test.workpath('does_not_exist')
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
205 raise "did not catch expected UserError"
207 def my_validator(key, val, env):
208 raise Exception, "my_validator() got called for %s, %s!" % (key, val)
210 opts = SCons.Variables.Variables()
211 opts.Add(SCons.Variables.PathVariable('test2',
213 '/default/path/again',
219 o.validator('Y', 'value', {})
221 assert str(e) == 'my_validator() got called for Y, value!', e
223 raise "did not catch expected exception from my_validator()"
228 if __name__ == "__main__":
229 suite = unittest.makeSuite(PathVariableTestCase, 'test_')
230 if not unittest.TextTestRunner().run(suite).wasSuccessful():
235 # indent-tabs-mode:nil
237 # vim: set expandtab tabstop=4 shiftwidth=4: