28
28
import SCons .Variables
29
29
30
30
import TestCmd
31
+ from TestCmd import IS_WINDOWS
31
32
32
33
class PathVariableTestCase (unittest .TestCase ):
33
34
def test_PathVariable (self ) -> None :
34
35
"""Test PathVariable creation"""
35
36
opts = SCons .Variables .Variables ()
36
37
opts .Add (SCons .Variables .PathVariable ('test' ,
37
- 'test option help' ,
38
+ 'test build variable help' ,
38
39
'/default/path' ))
39
40
40
41
o = opts .options [0 ]
41
42
assert o .key == 'test' , o .key
42
- assert o .help == 'test option help ( /path/to/test )' , repr (o .help )
43
+ assert o .help == 'test build variable help ( /path/to/test )' , repr (o .help )
43
44
assert o .default == '/default/path' , o .default
44
45
assert o .validator is not None , o .validator
45
46
assert o .converter is None , o .converter
@@ -48,30 +49,27 @@ def test_PathExists(self):
48
49
"""Test the PathExists validator"""
49
50
opts = SCons .Variables .Variables ()
50
51
opts .Add (SCons .Variables .PathVariable ('test' ,
51
- 'test option help' ,
52
+ 'test build variable help' ,
52
53
'/default/path' ,
53
54
SCons .Variables .PathVariable .PathExists ))
54
55
55
56
test = TestCmd .TestCmd (workdir = '' )
56
57
test .write ('exists' , 'exists\n ' )
57
58
58
59
o = opts .options [0 ]
59
-
60
60
o .validator ('X' , test .workpath ('exists' ), {})
61
61
62
62
dne = test .workpath ('does_not_exist' )
63
- try :
63
+ with self . assertRaises ( SCons . Errors . UserError ) as cm :
64
64
o .validator ('X' , dne , {})
65
- except SCons .Errors .UserError as e :
66
- assert str (e ) == 'Path for option X does not exist: %s' % dne , e
67
- except :
68
- raise Exception ("did not catch expected UserError" )
65
+ e = cm .exception
66
+ self .assertEqual (str (e ), f"Path for variable 'X' does not exist: { dne } " )
69
67
70
68
def test_PathIsDir (self ):
71
69
"""Test the PathIsDir validator"""
72
70
opts = SCons .Variables .Variables ()
73
71
opts .Add (SCons .Variables .PathVariable ('test' ,
74
- 'test option help' ,
72
+ 'test build variable help' ,
75
73
'/default/path' ,
76
74
SCons .Variables .PathVariable .PathIsDir ))
77
75
@@ -80,30 +78,25 @@ def test_PathIsDir(self):
80
78
test .write ('file' , "file\n " )
81
79
82
80
o = opts .options [0 ]
83
-
84
81
o .validator ('X' , test .workpath ('dir' ), {})
85
82
86
83
f = test .workpath ('file' )
87
- try :
84
+ with self . assertRaises ( SCons . Errors . UserError ) as cm :
88
85
o .validator ('X' , f , {})
89
- except SCons .Errors .UserError as e :
90
- assert str (e ) == 'Directory path for option X is a file: %s' % f , e
91
- except :
92
- raise Exception ("did not catch expected UserError" )
86
+ e = cm .exception
87
+ self .assertEqual (str (e ), f"Directory path for variable 'X' is a file: { f } " )
93
88
94
89
dne = test .workpath ('does_not_exist' )
95
- try :
90
+ with self . assertRaises ( SCons . Errors . UserError ) as cm :
96
91
o .validator ('X' , dne , {})
97
- except SCons .Errors .UserError as e :
98
- assert str (e ) == 'Directory path for option X does not exist: %s' % dne , e
99
- except Exception as e :
100
- raise Exception ("did not catch expected UserError" ) from e
92
+ e = cm .exception
93
+ self .assertEqual (str (e ), f"Directory path for variable 'X' does not exist: { dne } " )
101
94
102
95
def test_PathIsDirCreate (self ):
103
96
"""Test the PathIsDirCreate validator"""
104
97
opts = SCons .Variables .Variables ()
105
98
opts .Add (SCons .Variables .PathVariable ('test' ,
106
- 'test option help' ,
99
+ 'test build variable help' ,
107
100
'/default/path' ,
108
101
SCons .Variables .PathVariable .PathIsDirCreate ))
109
102
@@ -117,26 +110,26 @@ def test_PathIsDirCreate(self):
117
110
assert os .path .isdir (d )
118
111
119
112
f = test .workpath ('file' )
120
- try :
113
+ with self . assertRaises ( SCons . Errors . UserError ) as cm :
121
114
o .validator ('X' , f , {})
122
- except SCons .Errors .UserError as e :
123
- assert str (e ) == 'Path for option X is a file, not a directory: %s' % f , e
124
- except Exception as e :
125
- raise Exception ("did not catch expected UserError" ) from e
115
+ e = cm .exception
116
+ self .assertEqual (str (e ), f"Path for variable 'X' is a file, not a directory: { f } " )
126
117
127
- f = '/yyy/zzz' # this not exists and should fail to create
128
- try :
118
+ # pick a directory path that can't be mkdir'd
119
+ if IS_WINDOWS :
120
+ f = r'\\noserver\noshare\yyy\zzz'
121
+ else :
122
+ f = '/yyy/zzz'
123
+ with self .assertRaises (SCons .Errors .UserError ) as cm :
129
124
o .validator ('X' , f , {})
130
- except SCons .Errors .UserError as e :
131
- assert str (e ) == 'Path for option X could not be created: %s' % f , e
132
- except Exception as e :
133
- raise Exception ("did not catch expected UserError" ) from e
125
+ e = cm .exception
126
+ self .assertEqual (str (e ), f"Path for variable 'X' could not be created: { f } " )
134
127
135
128
def test_PathIsFile (self ):
136
129
"""Test the PathIsFile validator"""
137
130
opts = SCons .Variables .Variables ()
138
131
opts .Add (SCons .Variables .PathVariable ('test' ,
139
- 'test option help' ,
132
+ 'test build variable help' ,
140
133
'/default/path' ,
141
134
SCons .Variables .PathVariable .PathIsFile ))
142
135
@@ -145,30 +138,25 @@ def test_PathIsFile(self):
145
138
test .write ('file' , "file\n " )
146
139
147
140
o = opts .options [0 ]
148
-
149
141
o .validator ('X' , test .workpath ('file' ), {})
150
142
151
143
d = test .workpath ('d' )
152
- try :
144
+ with self . assertRaises ( SCons . Errors . UserError ) as cm :
153
145
o .validator ('X' , d , {})
154
- except SCons .Errors .UserError as e :
155
- assert str (e ) == 'File path for option X does not exist: %s' % d , e
156
- except :
157
- raise Exception ("did not catch expected UserError" )
146
+ e = cm .exception
147
+ self .assertEqual (str (e ), f"File path for variable 'X' does not exist: { d } " )
158
148
159
149
dne = test .workpath ('does_not_exist' )
160
- try :
150
+ with self . assertRaises ( SCons . Errors . UserError ) as cm :
161
151
o .validator ('X' , dne , {})
162
- except SCons .Errors .UserError as e :
163
- assert str (e ) == 'File path for option X does not exist: %s' % dne , e
164
- except :
165
- raise Exception ("did not catch expected UserError" )
152
+ e = cm .exception
153
+ self .assertEqual (str (e ), f"File path for variable 'X' does not exist: { dne } " )
166
154
167
155
def test_PathAccept (self ) -> None :
168
156
"""Test the PathAccept validator"""
169
157
opts = SCons .Variables .Variables ()
170
158
opts .Add (SCons .Variables .PathVariable ('test' ,
171
- 'test option help' ,
159
+ 'test build variable help' ,
172
160
'/default/path' ,
173
161
SCons .Variables .PathVariable .PathAccept ))
174
162
@@ -177,7 +165,6 @@ def test_PathAccept(self) -> None:
177
165
test .write ('file' , "file\n " )
178
166
179
167
o = opts .options [0 ]
180
-
181
168
o .validator ('X' , test .workpath ('file' ), {})
182
169
183
170
d = test .workpath ('d' )
@@ -190,44 +177,37 @@ def test_validator(self):
190
177
"""Test the PathVariable validator argument"""
191
178
opts = SCons .Variables .Variables ()
192
179
opts .Add (SCons .Variables .PathVariable ('test' ,
193
- 'test option help' ,
180
+ 'test variable help' ,
194
181
'/default/path' ))
195
182
196
183
test = TestCmd .TestCmd (workdir = '' )
197
184
test .write ('exists' , 'exists\n ' )
198
185
199
186
o = opts .options [0 ]
200
-
201
187
o .validator ('X' , test .workpath ('exists' ), {})
202
188
203
189
dne = test .workpath ('does_not_exist' )
204
- try :
190
+ with self . assertRaises ( SCons . Errors . UserError ) as cm :
205
191
o .validator ('X' , dne , {})
206
- except SCons . Errors . UserError as e :
207
- expect = ' Path for option X does not exist: %s' % dne
208
- assert str ( e ) == expect , e
209
- else :
210
- raise Exception ( "did not catch expected UserError" )
192
+ e = cm . exception
193
+ self . assertEqual ( str ( e ), f" Path for variable 'X' does not exist: { dne } " )
194
+
195
+ class ValidatorError ( Exception ) :
196
+ pass
211
197
212
198
def my_validator (key , val , env ):
213
- raise Exception ( "my_validator() got called for %s, %s!" % ( key , val ) )
199
+ raise ValidatorError ( f "my_validator() got called for { key !r } , { val } !" )
214
200
215
201
opts = SCons .Variables .Variables ()
216
202
opts .Add (SCons .Variables .PathVariable ('test2' ,
217
203
'more help' ,
218
204
'/default/path/again' ,
219
205
my_validator ))
220
-
221
206
o = opts .options [0 ]
222
-
223
- try :
207
+ with self .assertRaises (ValidatorError ) as cm :
224
208
o .validator ('Y' , 'value' , {})
225
- except Exception as e :
226
- assert str (e ) == 'my_validator() got called for Y, value!' , e
227
- else :
228
- raise Exception ("did not catch expected exception from my_validator()" )
229
-
230
-
209
+ e = cm .exception
210
+ self .assertEqual (str (e ), f"my_validator() got called for 'Y', value!" )
231
211
232
212
233
213
if __name__ == "__main__" :
0 commit comments