36
36
import difflib
37
37
import os
38
38
import pprint
39
- import re
40
39
import sys
41
40
from contextlib import contextmanager
42
41
from io import StringIO
43
42
44
43
from unittest import TestCase as OrigTestCase
44
+ from easybuild .base import fancylogger
45
45
46
46
47
47
def nicediff (txta , txtb , offset = 5 ):
@@ -66,7 +66,7 @@ def nicediff(txta, txtb, offset=5):
66
66
67
67
68
68
class TestCase (OrigTestCase ):
69
- """Enhanced test case, provides extra functionality (e.g. an assertRaisesRegex method) ."""
69
+ """Enhanced test case, provides extra functionality."""
70
70
71
71
longMessage = True # print both standard messgae and custom message
72
72
@@ -140,42 +140,9 @@ def setUp(self):
140
140
self .orig_sys_stdout = sys .stdout
141
141
self .orig_sys_stderr = sys .stderr
142
142
143
- def convert_exception_to_str (self , err ):
144
- """Convert an Exception instance to a string."""
145
- msg = err
146
- if hasattr (err , 'msg' ):
147
- msg = err .msg
148
- elif hasattr (err , 'message' ):
149
- msg = err .message
150
- if not msg :
151
- # rely on str(msg) in case err.message is empty
152
- msg = err
153
- elif hasattr (err , 'args' ): # KeyError in Python 2.4 only provides message via 'args' attribute
154
- msg = err .args [0 ]
155
- else :
156
- msg = err
157
- try :
158
- res = str (msg )
159
- except UnicodeEncodeError :
160
- res = msg .encode ('utf8' , 'replace' )
161
-
162
- return res
163
-
164
- def assertErrorRegex (self , error , regex , call , * args , ** kwargs ):
165
- """
166
- Convenience method to match regex with the expected error message.
167
- Example: self.assertErrorRegex(OSError, "No such file or directory", os.remove, '/no/such/file')
168
- """
169
- try :
170
- call (* args , ** kwargs )
171
- str_kwargs = ['=' .join ([k , str (v )]) for (k , v ) in kwargs .items ()]
172
- str_args = ', ' .join (list (map (str , args )) + str_kwargs )
173
- self .fail ("Expected errors with %s(%s) call should occur" % (call .__name__ , str_args ))
174
- except error as err :
175
- msg = self .convert_exception_to_str (err )
176
- if isinstance (regex , str ):
177
- regex = re .compile (regex )
178
- self .assertTrue (regex .search (msg ), "Pattern '%s' is found in '%s'" % (regex .pattern , msg ))
143
+ def assertErrorRegex (* args , ** kwargs ):
144
+ fancylogger .getLogger ().deprecated ('assertErrorRegex is deprecated, use assertRaisesRegex instead' , '6.0' )
145
+ return OrigTestCase .assertRaisesRegex (* args , ** kwargs )
179
146
180
147
def mock_stdout (self , enable ):
181
148
"""Enable/disable mocking stdout."""
0 commit comments