Skip to content

Commit

Permalink
pythongh-71339: Use new assertion methods in the email tests
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Jan 20, 2025
1 parent c463270 commit b5655c1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
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
6 changes: 3 additions & 3 deletions Lib/test/test_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -1303,12 +1303,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 @@ -2357,7 +2357,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
6 changes: 3 additions & 3 deletions Lib/test/test_poplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def handle_error(self):

class TestPOP3Class(TestCase):
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 @@ -293,7 +293,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 @@ -428,7 +428,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

0 comments on commit b5655c1

Please sign in to comment.