Skip to content

Commit d5012a1

Browse files
committed
fix: replace assertRaisesRegexp with assertRaisesRegex
1 parent fc3394a commit d5012a1

File tree

13 files changed

+28
-23
lines changed

13 files changed

+28
-23
lines changed

stem/util/test_tools.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,15 @@ def run(self, result: Optional[Any] = None) -> Any:
238238
def assertRaisesWith(self, exc_type: Type[Exception], exc_msg: str, *args: Any, **kwargs: Any) -> None:
239239
"""
240240
Asserts the given invokation raises the expected excepiton. This is
241-
similar to unittest's assertRaises and assertRaisesRegexp, but checks
241+
similar to unittest's assertRaises and assertRaisesRegex, but checks
242242
for an exact match.
243243
244244
This method is **not** being vended to external users and may be
245245
changed without notice. If you want this method to be part of our
246246
vended API then please let us know.
247247
"""
248248

249-
return self.assertRaisesRegexp(exc_type, '^%s$' % re.escape(exc_msg), *args, **kwargs)
249+
return self.assertRaisesRegex(exc_type, '^%s$' % re.escape(exc_msg), *args, **kwargs)
250250

251251
def shortDescription(self):
252252
# Python now prints the first line of a test's docstring by default.

test/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import stem.util.enum
2121
import stem.version
2222

23+
from unittest import TestCase
24+
2325
__all__ = [
2426
'network',
2527
'output',
@@ -78,6 +80,9 @@
7880
if os.path.exists(os.path.join(STEM_BASE, '.travis.yml')):
7981
IGNORED_FILE_TYPES.append('.travis.yml')
8082

83+
# Allow test cases to run on both Python2 and Python3
84+
if not hasattr(TestCase, 'assertRaisesRegex'):
85+
TestCase.assertRaisesRegex = TestCase.assertRaisesRegexp
8186

8287
def get_new_capabilities():
8388
"""

test/integ/control/controller.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,13 +1745,13 @@ async def test_hidden_service_auth_invalid(self):
17451745
invalid_service_id = 'xxxxxxxxyvhz3ofkv7gwf5hpzqvhonpr3gbax2cc7dee3xcnt7dmtlx2gu7vyvid'
17461746
exc_msg = "^%%s response didn't have an OK status: Invalid v3 (addr|address) \"%s\"$" % invalid_service_id
17471747

1748-
with self.assertRaisesRegexp(stem.ProtocolError, exc_msg % 'ONION_CLIENT_AUTH_ADD'):
1748+
with self.assertRaisesRegex(stem.ProtocolError, exc_msg % 'ONION_CLIENT_AUTH_ADD'):
17491749
await controller.add_hidden_service_auth(invalid_service_id, PRIVATE_KEY)
17501750

1751-
with self.assertRaisesRegexp(stem.ProtocolError, exc_msg % 'ONION_CLIENT_AUTH_REMOVE'):
1751+
with self.assertRaisesRegex(stem.ProtocolError, exc_msg % 'ONION_CLIENT_AUTH_REMOVE'):
17521752
await controller.remove_hidden_service_auth(invalid_service_id)
17531753

1754-
with self.assertRaisesRegexp(stem.ProtocolError, exc_msg % 'ONION_CLIENT_AUTH_VIEW'):
1754+
with self.assertRaisesRegex(stem.ProtocolError, exc_msg % 'ONION_CLIENT_AUTH_VIEW'):
17551755
await controller.list_hidden_service_auth(invalid_service_id)
17561756

17571757
invalid_key = 'XXXXXXXXXFCV0c0ELDKKDpSFgVIB8Yow8Evj5iD+GoiTtK878NkQ='

test/integ/descriptor/collector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_downloading_consensus_invalid_type(self):
8888
)
8989

9090
for args, expected_msg in test_values:
91-
self.assertRaisesRegexp(ValueError, re.escape(expected_msg), list, stem.descriptor.collector.get_consensus(**args))
91+
self.assertRaisesRegex(ValueError, re.escape(expected_msg), list, stem.descriptor.collector.get_consensus(**args))
9292

9393
def _test_index(self, compression):
9494
if compression and not compression.available:

test/unit/client/cell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def test_relay_cell(self):
213213
self.assertEqual(3257622417, RelayCell(5, 'RELAY_BEGIN_DIR', '', digest.digest(), 564346860).digest)
214214
self.assertEqual(3257622417, RelayCell(5, 'RELAY_BEGIN_DIR', '', 3257622417, 564346860).digest)
215215
self.assertRaisesWith(ValueError, 'RELAY cell digest must be a hash, string, or int but was a list', RelayCell, 5, 'RELAY_BEGIN_DIR', '', [], 564346860)
216-
self.assertRaisesRegexp(ValueError, "Invalid enumeration 'NO_SUCH_COMMAND', options are RELAY_BEGIN, RELAY_DATA", RelayCell, 5, 'NO_SUCH_COMMAND', '', 5, 564346860)
216+
self.assertRaisesRegex(ValueError, "Invalid enumeration 'NO_SUCH_COMMAND', options are RELAY_BEGIN, RELAY_DATA", RelayCell, 5, 'NO_SUCH_COMMAND', '', 5, 564346860)
217217

218218
mismatched_data_length_bytes = b''.join((
219219
b'\x00\x01', # circ ID

test/unit/descriptor/certificate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def test_non_base64(self):
117117
"""
118118

119119
exc_msg = re.escape("Ed25519 certificate wasn't propoerly base64 encoded (Incorrect padding):")
120-
self.assertRaisesRegexp(ValueError, exc_msg, Ed25519Certificate.from_base64, '\x02\x0323\x04')
120+
self.assertRaisesRegex(ValueError, exc_msg, Ed25519Certificate.from_base64, '\x02\x0323\x04')
121121

122122
def test_too_short(self):
123123
"""

test/unit/descriptor/collector.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,19 @@ def test_index_retries(self, urlopen_mock):
102102
urlopen_mock.side_effect = OSError('boom')
103103

104104
collector = CollecTor(retries = 0)
105-
self.assertRaisesRegexp(OSError, 'boom', collector.index)
105+
self.assertRaisesRegex(OSError, 'boom', collector.index)
106106
self.assertEqual(1, urlopen_mock.call_count)
107107

108108
urlopen_mock.reset_mock()
109109

110110
collector = CollecTor(retries = 4)
111-
self.assertRaisesRegexp(OSError, 'boom', collector.index)
111+
self.assertRaisesRegex(OSError, 'boom', collector.index)
112112
self.assertEqual(5, urlopen_mock.call_count)
113113

114114
@patch('urllib.request.urlopen', Mock(return_value = io.BytesIO(b'not json')))
115115
def test_index_malformed_json(self):
116116
collector = CollecTor()
117-
self.assertRaisesRegexp(ValueError, 'Expecting value: line 1 column 1', collector.index, Compression.PLAINTEXT)
117+
self.assertRaisesRegex(ValueError, 'Expecting value: line 1 column 1', collector.index, Compression.PLAINTEXT)
118118

119119
def test_index_malformed_compression(self):
120120
for compression in (Compression.GZIP, Compression.BZ2, Compression.LZMA):
@@ -123,7 +123,7 @@ def test_index_malformed_compression(self):
123123

124124
with patch('urllib.request.urlopen', Mock(return_value = io.BytesIO(b'not compressed'))):
125125
collector = CollecTor()
126-
self.assertRaisesRegexp(OSError, 'Failed to decompress as %s' % compression, collector.index, compression)
126+
self.assertRaisesRegex(OSError, 'Failed to decompress as %s' % compression, collector.index, compression)
127127

128128
@patch('stem.descriptor.collector.CollecTor.index', Mock(return_value = EXAMPLE_INDEX))
129129
def test_files(self):

test/unit/descriptor/remote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def test_response_header_code(self):
181181
skip_crypto_validation = not test.require.CRYPTOGRAPHY_AVAILABLE,
182182
)
183183

184-
self.assertRaisesRegexp(stem.ProtocolError, "^Response should begin with HTTP success, but was 'HTTP/1.0 500 Kaboom'", request.run)
184+
self.assertRaisesRegex(stem.ProtocolError, "^Response should begin with HTTP success, but was 'HTTP/1.0 500 Kaboom'", request.run)
185185

186186
@mock_download(TEST_DESCRIPTOR)
187187
def test_reply_header_data(self):

test/unit/directory/authority.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ def test_from_remote(self):
7272

7373
@patch('urllib.request.urlopen', Mock(return_value = io.BytesIO(b'')))
7474
def test_from_remote_empty(self):
75-
self.assertRaisesRegexp(stem.DownloadFailed, 'no content', stem.directory.Authority.from_remote)
75+
self.assertRaisesRegex(stem.DownloadFailed, 'no content', stem.directory.Authority.from_remote)

test/unit/directory/fallback.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ def test_from_remote(self):
100100

101101
@patch('urllib.request.urlopen', Mock(return_value = io.BytesIO(b'')))
102102
def test_from_remote_empty(self):
103-
self.assertRaisesRegexp(stem.DownloadFailed, 'no content', stem.directory.Fallback.from_remote)
103+
self.assertRaisesRegex(stem.DownloadFailed, 'no content', stem.directory.Fallback.from_remote)
104104

105105
@patch('urllib.request.urlopen', Mock(return_value = io.BytesIO(b'\n'.join(FALLBACK_GITLAB_CONTENT.splitlines()[1:]))))
106106
def test_from_remote_no_header(self):
107-
self.assertRaisesRegexp(OSError, 'does not have a type field indicating it is fallback directory metadata', stem.directory.Fallback.from_remote)
107+
self.assertRaisesRegex(OSError, 'does not have a type field indicating it is fallback directory metadata', stem.directory.Fallback.from_remote)
108108

109109
@patch('urllib.request.urlopen', Mock(return_value = io.BytesIO(FALLBACK_GITLAB_CONTENT.replace(b'version=4.0.0', b'version'))))
110110
def test_from_remote_malformed_header(self):
111-
self.assertRaisesRegexp(OSError, 'Malformed fallback directory header line: /\\* version \\*/', stem.directory.Fallback.from_remote)
111+
self.assertRaisesRegex(OSError, 'Malformed fallback directory header line: /\\* version \\*/', stem.directory.Fallback.from_remote)
112112

113113
def test_from_remote_malformed(self):
114114
test_values = {
@@ -121,7 +121,7 @@ def test_from_remote_malformed(self):
121121

122122
for entry, expected in test_values.items():
123123
with patch('urllib.request.urlopen', Mock(return_value = io.BytesIO(entry))):
124-
self.assertRaisesRegexp(OSError, re.escape(expected), stem.directory.Fallback.from_remote)
124+
self.assertRaisesRegex(OSError, re.escape(expected), stem.directory.Fallback.from_remote)
125125

126126
def test_persistence(self):
127127
expected = {

0 commit comments

Comments
 (0)