Skip to content

Commit

Permalink
Merge pull request #1144 from lsst/tickets/DM-48490
Browse files Browse the repository at this point in the history
DM-48940: Modify DEBUG logging test to be more robust
  • Loading branch information
timj authored Jan 17, 2025
2 parents e3be3c4 + b9ebac3 commit 524ffd8
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions python/lsst/daf/butler/tests/cliLogTestBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,28 +405,27 @@ def testFileLogging(self) -> None:
result = self.runner.invoke(butlerCli, args)
self.assertEqual(result.exit_code, 0, clickResultMsg(result))

# Record to test. Test one in the middle that we know is
# a DEBUG message. The first message might come from
# python itself since warnings are redirected to log
# messages.
num = 4
# We have no real control over other packages causing lsst
# DEBUG log messages to turn up (for example from misconfigured
# CLI plugins). Check for any DEBUG messages.
# We know there are at least this number of log
# messages issued.
min_records = 10

n_records = 5
if suffix == ".json":
records = ButlerLogRecords.from_file(filename)
self.assertGreater(len(records), num)
self.assertEqual(records[num].levelname, "DEBUG", str(records[num]))
self.assertGreater(len(records), min_records)
self.assertEqual(records[0].MDC, dict(K1="v1", K2="v2", K3="v3"))

self.assertGreater(len(records), n_records)
# Find a DEBUG message.
self.assertTrue(any(rec.levelname == "DEBUG" for rec in records), records)
else:
with open(filename) as filed:
records_text = filed.readlines()
self.assertGreater(len(records_text), num)
self.assertIn("DEBUG", records_text[num], str(records_text[num]))
self.assertNotIn("{", records_text[num], str(records_text[num]))
self.assertGreater(len(records_text), min_records) # Counting lines not records.
content = "".join(records_text)
self.assertFalse(content.startswith("{"), f"Checking for JSON content in {content}")

self.assertGreater(len(records_text), n_records)
self.assertTrue(any("DEBUG:" in rec for rec in records_text), content)

def testLogTty(self) -> None:
"""Verify that log output to terminal can be suppressed."""
Expand Down

0 comments on commit 524ffd8

Please sign in to comment.