mirror of
https://github.com/python/cpython.git
synced 2026-01-05 23:12:38 +00:00
[3.13] gh-71339: Use new assertion methods in the email tests (GH-129055) (GH-132501)
(cherry picked from commit 522766aa23)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
044fe0b2e1
commit
34d3495f32
6 changed files with 23 additions and 22 deletions
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
@ -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):
|
||||
|
|
@ -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):
|
||||
|
|
@ -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)
|
||||
|
|
@ -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)
|
||||
|
|
@ -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.
|
||||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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)
|
||||
|
||||
|
|
@ -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):
|
||||
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
@ -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))
|
||||
|
|
@ -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',
|
||||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue