Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 34d3495

Browse files
[3.13] gh-71339: Use new assertion methods in the email tests (GH-129055) (GH-132501)
(cherry picked from commit 522766a) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 044fe0b commit 34d3495

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

Lib/test/test_email/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from email.message import Message
66
from email._policybase import compat32
77
from test.support import load_package_tests
8+
from test.support.testcase import ExtraAssertions
89
from test.test_email import __file__ as landmark
910

1011
# Load all tests in package
@@ -20,7 +21,7 @@ def openfile(filename, *args, **kws):
2021

2122

2223
# Base test class
23-
class TestEmailBase(unittest.TestCase):
24+
class TestEmailBase(unittest.TestCase, ExtraAssertions):
2425

2526
maxDiff = None
2627
# Currently the default policy is compat32. By setting that as the default

Lib/test/test_email/test_contentmanager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def test_get_message_non_rfc822_or_external_body_yields_bytes(self):
288288
289289
The real body is in another message.
290290
"""))
291-
self.assertEqual(raw_data_manager.get_content(m)[:10], b'To: foo@ex')
291+
self.assertStartsWith(raw_data_manager.get_content(m), b'To: foo@ex')
292292

293293
def test_set_text_plain(self):
294294
m = self._make_message()

Lib/test/test_email/test_defect_handling.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_same_boundary_inner_outer(self):
5757
msg = self._str_msg(source)
5858
if self.raise_expected: return
5959
inner = msg.get_payload(0)
60-
self.assertTrue(hasattr(inner, 'defects'))
60+
self.assertHasAttr(inner, 'defects')
6161
self.assertEqual(len(self.get_defects(inner)), 1)
6262
self.assertIsInstance(self.get_defects(inner)[0],
6363
errors.StartBoundaryNotFoundDefect)
@@ -151,7 +151,7 @@ def test_lying_multipart(self):
151151
with self._raise_point(errors.NoBoundaryInMultipartDefect):
152152
msg = self._str_msg(source)
153153
if self.raise_expected: return
154-
self.assertTrue(hasattr(msg, 'defects'))
154+
self.assertHasAttr(msg, 'defects')
155155
self.assertEqual(len(self.get_defects(msg)), 2)
156156
self.assertIsInstance(self.get_defects(msg)[0],
157157
errors.NoBoundaryInMultipartDefect)

Lib/test/test_email/test_email.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ def test_make_boundary(self):
210210
self.assertEqual(msg.items()[0][1], 'multipart/form-data')
211211
# Trigger creation of boundary
212212
msg.as_string()
213-
self.assertEqual(msg.items()[0][1][:33],
214-
'multipart/form-data; boundary="==')
213+
self.assertStartsWith(msg.items()[0][1],
214+
'multipart/form-data; boundary="==')
215215
# XXX: there ought to be tests of the uniqueness of the boundary, too.
216216

217217
def test_message_rfc822_only(self):
@@ -303,7 +303,7 @@ def test_as_string(self):
303303
self.assertEqual(text, str(msg))
304304
fullrepr = msg.as_string(unixfrom=True)
305305
lines = fullrepr.split('\n')
306-
self.assertTrue(lines[0].startswith('From '))
306+
self.assertStartsWith(lines[0], 'From ')
307307
self.assertEqual(text, NL.join(lines[1:]))
308308

309309
def test_as_string_policy(self):
@@ -372,7 +372,7 @@ def test_as_bytes(self):
372372
self.assertEqual(data, bytes(msg))
373373
fullrepr = msg.as_bytes(unixfrom=True)
374374
lines = fullrepr.split(b'\n')
375-
self.assertTrue(lines[0].startswith(b'From '))
375+
self.assertStartsWith(lines[0], b'From ')
376376
self.assertEqual(data, b'\n'.join(lines[1:]))
377377

378378
def test_as_bytes_policy(self):
@@ -2203,7 +2203,7 @@ def test_same_boundary_inner_outer(self):
22032203
msg = self._msgobj('msg_15.txt')
22042204
# XXX We can probably eventually do better
22052205
inner = msg.get_payload(0)
2206-
self.assertTrue(hasattr(inner, 'defects'))
2206+
self.assertHasAttr(inner, 'defects')
22072207
self.assertEqual(len(inner.defects), 1)
22082208
self.assertIsInstance(inner.defects[0],
22092209
errors.StartBoundaryNotFoundDefect)
@@ -2315,7 +2315,7 @@ def test_no_separating_blank_line(self):
23152315
# test_defect_handling
23162316
def test_lying_multipart(self):
23172317
msg = self._msgobj('msg_41.txt')
2318-
self.assertTrue(hasattr(msg, 'defects'))
2318+
self.assertHasAttr(msg, 'defects')
23192319
self.assertEqual(len(msg.defects), 2)
23202320
self.assertIsInstance(msg.defects[0],
23212321
errors.NoBoundaryInMultipartDefect)
@@ -3659,9 +3659,7 @@ def test_make_msgid_idstring(self):
36593659
def test_make_msgid_default_domain(self):
36603660
with patch('socket.getfqdn') as mock_getfqdn:
36613661
mock_getfqdn.return_value = domain = 'pythontest.example.com'
3662-
self.assertTrue(
3663-
email.utils.make_msgid().endswith(
3664-
'@' + domain + '>'))
3662+
self.assertEndsWith(email.utils.make_msgid(), '@' + domain + '>')
36653663

36663664
def test_Generator_linend(self):
36673665
# Issue 14645.
@@ -4128,7 +4126,7 @@ def test_CRLFLF_at_end_of_part(self):
41284126
"--BOUNDARY--\n"
41294127
)
41304128
msg = email.message_from_string(m)
4131-
self.assertTrue(msg.get_payload(0).get_payload().endswith('\r\n'))
4129+
self.assertEndsWith(msg.get_payload(0).get_payload(), '\r\n')
41324130

41334131

41344132
class Test8BitBytesHandling(TestEmailBase):

Lib/test/test_mailbox.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from test.support import os_helper
1414
from test.support import refleak_helper
1515
from test.support import socket_helper
16+
from test.support.testcase import ExtraAssertions
1617
import unittest
1718
import textwrap
1819
import mailbox
@@ -1266,7 +1267,7 @@ def test_relock(self):
12661267
self._box.close()
12671268

12681269

1269-
class TestMbox(_TestMboxMMDF, unittest.TestCase):
1270+
class TestMbox(_TestMboxMMDF, unittest.TestCase, ExtraAssertions):
12701271

12711272
_factory = lambda self, path, factory=None: mailbox.mbox(path, factory)
12721273

@@ -1304,12 +1305,12 @@ def test_message_separator(self):
13041305
self._box.add('From: foo\n\n0') # No newline at the end
13051306
with open(self._path, encoding='utf-8') as f:
13061307
data = f.read()
1307-
self.assertEqual(data[-3:], '0\n\n')
1308+
self.assertEndsWith(data, '0\n\n')
13081309

13091310
self._box.add('From: foo\n\n0\n') # Newline at the end
13101311
with open(self._path, encoding='utf-8') as f:
13111312
data = f.read()
1312-
self.assertEqual(data[-3:], '0\n\n')
1313+
self.assertEndsWith(data, '0\n\n')
13131314

13141315

13151316
class TestMMDF(_TestMboxMMDF, unittest.TestCase):
@@ -2358,7 +2359,7 @@ def test_empty_maildir(self):
23582359
# Test for regression on bug #117490:
23592360
# Make sure the boxes attribute actually gets set.
23602361
self.mbox = mailbox.Maildir(os_helper.TESTFN)
2361-
#self.assertTrue(hasattr(self.mbox, "boxes"))
2362+
#self.assertHasAttr(self.mbox, "boxes")
23622363
#self.assertEqual(len(self.mbox.boxes), 0)
23632364
self.assertIsNone(self.mbox.next())
23642365
self.assertIsNone(self.mbox.next())

Lib/test/test_poplib.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from test.support import threading_helper
1818
from test.support import asynchat
1919
from test.support import asyncore
20+
from test.support.testcase import ExtraAssertions
2021

2122

2223
test_support.requires_working_socket(module=True)
@@ -255,9 +256,9 @@ def handle_error(self):
255256
raise
256257

257258

258-
class TestPOP3Class(TestCase):
259+
class TestPOP3Class(TestCase, ExtraAssertions):
259260
def assertOK(self, resp):
260-
self.assertTrue(resp.startswith(b"+OK"))
261+
self.assertStartsWith(resp, b"+OK")
261262

262263
def setUp(self):
263264
self.server = DummyPOP3Server((HOST, PORT))
@@ -324,7 +325,7 @@ def test_list(self):
324325
self.assertEqual(self.client.list()[1:],
325326
([b'1 1', b'2 2', b'3 3', b'4 4', b'5 5'],
326327
25))
327-
self.assertTrue(self.client.list('1').endswith(b"OK 1 1"))
328+
self.assertEndsWith(self.client.list('1'), b"OK 1 1")
328329

329330
def test_retr(self):
330331
expected = (b'+OK 116 bytes',
@@ -459,7 +460,7 @@ def test_context(self):
459460
context=ctx)
460461
self.assertIsInstance(self.client.sock, ssl.SSLSocket)
461462
self.assertIs(self.client.sock.context, ctx)
462-
self.assertTrue(self.client.noop().startswith(b'+OK'))
463+
self.assertStartsWith(self.client.noop(), b'+OK')
463464

464465
def test_stls(self):
465466
self.assertRaises(poplib.error_proto, self.client.stls)

0 commit comments

Comments
 (0)