Skip to content

[3.13] gh-71339: Use new assertion methods in the email tests (GH-129055) #132501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Lib/test/test_email/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from email.message import Message
from email._policybase import compat32
from test.support import load_package_tests
from test.support.testcase import ExtraAssertions
from test.test_email import __file__ as landmark

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


# Base test class
class TestEmailBase(unittest.TestCase):
class TestEmailBase(unittest.TestCase, ExtraAssertions):

maxDiff = None
# Currently the default policy is compat32. By setting that as the default
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_email/test_contentmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_get_message_non_rfc822_or_external_body_yields_bytes(self):

The real body is in another message.
"""))
self.assertEqual(raw_data_manager.get_content(m)[:10], b'To: foo@ex')
self.assertStartsWith(raw_data_manager.get_content(m), b'To: foo@ex')

def test_set_text_plain(self):
m = self._make_message()
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_email/test_defect_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_same_boundary_inner_outer(self):
msg = self._str_msg(source)
if self.raise_expected: return
inner = msg.get_payload(0)
self.assertTrue(hasattr(inner, 'defects'))
self.assertHasAttr(inner, 'defects')
self.assertEqual(len(self.get_defects(inner)), 1)
self.assertIsInstance(self.get_defects(inner)[0],
errors.StartBoundaryNotFoundDefect)
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_lying_multipart(self):
with self._raise_point(errors.NoBoundaryInMultipartDefect):
msg = self._str_msg(source)
if self.raise_expected: return
self.assertTrue(hasattr(msg, 'defects'))
self.assertHasAttr(msg, 'defects')
self.assertEqual(len(self.get_defects(msg)), 2)
self.assertIsInstance(self.get_defects(msg)[0],
errors.NoBoundaryInMultipartDefect)
Expand Down
18 changes: 8 additions & 10 deletions Lib/test/test_email/test_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def test_make_boundary(self):
self.assertEqual(msg.items()[0][1], 'multipart/form-data')
# Trigger creation of boundary
msg.as_string()
self.assertEqual(msg.items()[0][1][:33],
'multipart/form-data; boundary="==')
self.assertStartsWith(msg.items()[0][1],
'multipart/form-data; boundary="==')
# XXX: there ought to be tests of the uniqueness of the boundary, too.

def test_message_rfc822_only(self):
Expand Down Expand Up @@ -303,7 +303,7 @@ def test_as_string(self):
self.assertEqual(text, str(msg))
fullrepr = msg.as_string(unixfrom=True)
lines = fullrepr.split('\n')
self.assertTrue(lines[0].startswith('From '))
self.assertStartsWith(lines[0], 'From ')
self.assertEqual(text, NL.join(lines[1:]))

def test_as_string_policy(self):
Expand Down Expand Up @@ -372,7 +372,7 @@ def test_as_bytes(self):
self.assertEqual(data, bytes(msg))
fullrepr = msg.as_bytes(unixfrom=True)
lines = fullrepr.split(b'\n')
self.assertTrue(lines[0].startswith(b'From '))
self.assertStartsWith(lines[0], b'From ')
self.assertEqual(data, b'\n'.join(lines[1:]))

def test_as_bytes_policy(self):
Expand Down Expand Up @@ -2203,7 +2203,7 @@ def test_same_boundary_inner_outer(self):
msg = self._msgobj('msg_15.txt')
# XXX We can probably eventually do better
inner = msg.get_payload(0)
self.assertTrue(hasattr(inner, 'defects'))
self.assertHasAttr(inner, 'defects')
self.assertEqual(len(inner.defects), 1)
self.assertIsInstance(inner.defects[0],
errors.StartBoundaryNotFoundDefect)
Expand Down Expand Up @@ -2315,7 +2315,7 @@ def test_no_separating_blank_line(self):
# test_defect_handling
def test_lying_multipart(self):
msg = self._msgobj('msg_41.txt')
self.assertTrue(hasattr(msg, 'defects'))
self.assertHasAttr(msg, 'defects')
self.assertEqual(len(msg.defects), 2)
self.assertIsInstance(msg.defects[0],
errors.NoBoundaryInMultipartDefect)
Expand Down Expand Up @@ -3659,9 +3659,7 @@ def test_make_msgid_idstring(self):
def test_make_msgid_default_domain(self):
with patch('socket.getfqdn') as mock_getfqdn:
mock_getfqdn.return_value = domain = 'pythontest.example.com'
self.assertTrue(
email.utils.make_msgid().endswith(
'@' + domain + '>'))
self.assertEndsWith(email.utils.make_msgid(), '@' + domain + '>')

def test_Generator_linend(self):
# Issue 14645.
Expand Down Expand Up @@ -4128,7 +4126,7 @@ def test_CRLFLF_at_end_of_part(self):
"--BOUNDARY--\n"
)
msg = email.message_from_string(m)
self.assertTrue(msg.get_payload(0).get_payload().endswith('\r\n'))
self.assertEndsWith(msg.get_payload(0).get_payload(), '\r\n')


class Test8BitBytesHandling(TestEmailBase):
Expand Down
9 changes: 5 additions & 4 deletions Lib/test/test_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from test.support import os_helper
from test.support import refleak_helper
from test.support import socket_helper
from test.support.testcase import ExtraAssertions
import unittest
import textwrap
import mailbox
Expand Down Expand Up @@ -1266,7 +1267,7 @@ def test_relock(self):
self._box.close()


class TestMbox(_TestMboxMMDF, unittest.TestCase):
class TestMbox(_TestMboxMMDF, unittest.TestCase, ExtraAssertions):

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

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

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


class TestMMDF(_TestMboxMMDF, unittest.TestCase):
Expand Down Expand Up @@ -2358,7 +2359,7 @@ def test_empty_maildir(self):
# Test for regression on bug #117490:
# Make sure the boxes attribute actually gets set.
self.mbox = mailbox.Maildir(os_helper.TESTFN)
#self.assertTrue(hasattr(self.mbox, "boxes"))
#self.assertHasAttr(self.mbox, "boxes")
#self.assertEqual(len(self.mbox.boxes), 0)
self.assertIsNone(self.mbox.next())
self.assertIsNone(self.mbox.next())
Expand Down
9 changes: 5 additions & 4 deletions Lib/test/test_poplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from test.support import threading_helper
from test.support import asynchat
from test.support import asyncore
from test.support.testcase import ExtraAssertions


test_support.requires_working_socket(module=True)
Expand Down Expand Up @@ -255,9 +256,9 @@ def handle_error(self):
raise


class TestPOP3Class(TestCase):
class TestPOP3Class(TestCase, ExtraAssertions):
def assertOK(self, resp):
self.assertTrue(resp.startswith(b"+OK"))
self.assertStartsWith(resp, b"+OK")

def setUp(self):
self.server = DummyPOP3Server((HOST, PORT))
Expand Down Expand Up @@ -324,7 +325,7 @@ def test_list(self):
self.assertEqual(self.client.list()[1:],
([b'1 1', b'2 2', b'3 3', b'4 4', b'5 5'],
25))
self.assertTrue(self.client.list('1').endswith(b"OK 1 1"))
self.assertEndsWith(self.client.list('1'), b"OK 1 1")

def test_retr(self):
expected = (b'+OK 116 bytes',
Expand Down Expand Up @@ -459,7 +460,7 @@ def test_context(self):
context=ctx)
self.assertIsInstance(self.client.sock, ssl.SSLSocket)
self.assertIs(self.client.sock.context, ctx)
self.assertTrue(self.client.noop().startswith(b'+OK'))
self.assertStartsWith(self.client.noop(), b'+OK')

def test_stls(self):
self.assertRaises(poplib.error_proto, self.client.stls)
Expand Down
Loading