Skip to content

Commit

Permalink
pythongh-71339: Use new assertion methods in test_logging (pythonGH-1…
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka authored Jan 14, 2025
1 parent b5ee025 commit f7ceb31
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,7 @@ def test_basic(self):
self.assertEqual(mailfrom, 'me')
self.assertEqual(rcpttos, ['you'])
self.assertIn('\nSubject: Log\n', data)
self.assertTrue(data.endswith('\n\nHello \u2713'))
self.assertEndsWith(data, '\n\nHello \u2713')
h.close()

def process_message(self, *args):
Expand Down Expand Up @@ -3524,7 +3524,7 @@ def test_config14_ok(self):
self.assertEqual(h.foo, 'bar')
self.assertEqual(h.terminator, '!\n')
logging.warning('Exclamation')
self.assertTrue(output.getvalue().endswith('Exclamation!\n'))
self.assertEndsWith(output.getvalue(), 'Exclamation!\n')

def test_config15_ok(self):

Expand Down Expand Up @@ -4281,7 +4281,7 @@ def test_queue_handler(self):
msg = self.next_message()
self.que_logger.warning(msg)
data = self.queue.get_nowait()
self.assertTrue(isinstance(data, logging.LogRecord))
self.assertIsInstance(data, logging.LogRecord)
self.assertEqual(data.name, self.que_logger.name)
self.assertEqual((data.msg, data.args), (msg, None))

Expand Down Expand Up @@ -4879,14 +4879,14 @@ def test_formatting(self):
r.removeHandler(h)
h.close()
r = h.records[0]
self.assertTrue(r.exc_text.startswith('Traceback (most recent '
'call last):\n'))
self.assertTrue(r.exc_text.endswith('\nRuntimeError: '
'deliberate mistake'))
self.assertTrue(r.stack_info.startswith('Stack (most recent '
'call last):\n'))
self.assertTrue(r.stack_info.endswith('logging.exception(\'failed\', '
'stack_info=True)'))
self.assertStartsWith(r.exc_text,
'Traceback (most recent call last):\n')
self.assertEndsWith(r.exc_text,
'\nRuntimeError: deliberate mistake')
self.assertStartsWith(r.stack_info,
'Stack (most recent call last):\n')
self.assertEndsWith(r.stack_info,
"logging.exception('failed', stack_info=True)")


class LastResortTest(BaseTest):
Expand Down Expand Up @@ -5229,8 +5229,8 @@ class LogRecordTest(BaseTest):
def test_str_rep(self):
r = logging.makeLogRecord({})
s = str(r)
self.assertTrue(s.startswith('<LogRecord: '))
self.assertTrue(s.endswith('>'))
self.assertStartsWith(s, '<LogRecord: ')
self.assertEndsWith(s, '>')

def test_dict_arg(self):
h = RecordingHandler()
Expand Down Expand Up @@ -5880,14 +5880,14 @@ def test_extra_in_records(self):
self.adapter.critical('foo should be here')
self.assertEqual(len(self.recording.records), 1)
record = self.recording.records[0]
self.assertTrue(hasattr(record, 'foo'))
self.assertHasAttr(record, 'foo')
self.assertEqual(record.foo, '1')

def test_extra_not_merged_by_default(self):
self.adapter.critical('foo should NOT be here', extra={'foo': 'nope'})
self.assertEqual(len(self.recording.records), 1)
record = self.recording.records[0]
self.assertFalse(hasattr(record, 'foo'))
self.assertNotHasAttr(record, 'foo')

def test_extra_merged(self):
self.adapter = logging.LoggerAdapter(logger=self.logger,
Expand All @@ -5897,8 +5897,8 @@ def test_extra_merged(self):
self.adapter.critical('foo and bar should be here', extra={'bar': '2'})
self.assertEqual(len(self.recording.records), 1)
record = self.recording.records[0]
self.assertTrue(hasattr(record, 'foo'))
self.assertTrue(hasattr(record, 'bar'))
self.assertHasAttr(record, 'foo')
self.assertHasAttr(record, 'bar')
self.assertEqual(record.foo, '1')
self.assertEqual(record.bar, '2')

Expand All @@ -5910,7 +5910,7 @@ def test_extra_merged_log_call_has_precedence(self):
self.adapter.critical('foo shall be min', extra={'foo': '2'})
self.assertEqual(len(self.recording.records), 1)
record = self.recording.records[0]
self.assertTrue(hasattr(record, 'foo'))
self.assertHasAttr(record, 'foo')
self.assertEqual(record.foo, '2')


Expand Down Expand Up @@ -6624,18 +6624,19 @@ def namer(filename):
p = '%s.log.' % prefix
for c in candidates:
d, fn = os.path.split(c)
self.assertTrue(fn.startswith(p))
self.assertStartsWith(fn, p)
elif prefix.startswith('d.e'):
for c in candidates:
d, fn = os.path.split(c)
self.assertTrue(fn.endswith('.log'), fn)
self.assertTrue(fn.startswith(prefix + '.') and
fn[len(prefix) + 2].isdigit())
self.assertEndsWith(fn, '.log')
self.assertStartsWith(fn, prefix + '.')
self.assertTrue(fn[len(prefix) + 2].isdigit())
elif prefix == 'g':
for c in candidates:
d, fn = os.path.split(c)
self.assertTrue(fn.endswith('.oldlog'))
self.assertTrue(fn.startswith('g') and fn[1].isdigit())
self.assertEndsWith(fn, '.oldlog')
self.assertStartsWith(fn, 'g')
self.assertTrue(fn[1].isdigit())

def test_compute_files_to_delete_same_filename_different_extensions(self):
# See GH-93205 for background
Expand Down Expand Up @@ -6673,7 +6674,7 @@ def test_compute_files_to_delete_same_filename_different_extensions(self):
matcher = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}\Z")
for c in candidates:
d, fn = os.path.split(c)
self.assertTrue(fn.startswith(prefix+'.'))
self.assertStartsWith(fn, prefix+'.')
suffix = fn[(len(prefix)+1):]
self.assertRegex(suffix, matcher)

Expand Down

0 comments on commit f7ceb31

Please sign in to comment.