Skip to content

Commit

Permalink
test logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Helveg committed Dec 12, 2023
1 parent bc5b2f9 commit 6ded040
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/test_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest
from pathlib import Path

from glia._fs import get_cache_path, log


class TestLog(unittest.TestCase):
def test_log_hash(self):
# We use the fact that Python salts hashes per session (see
# https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHASHSEED) to get a
# a unique logfile hash per process, test this.
self.assertEqual(id("5"), id("5"))

def test_log(self):
log_path = Path(get_cache_path(f"{id('5')}.txt"))
log_path.unlink(missing_ok=True)
log("hello world")
self.assertTrue(log_path.exists(), "Logs not created")
self.assertIn("hello world", log_path.read_text(), "Log not logged")

def test_exc(self):
log_path = Path(get_cache_path(f"{id('5')}.txt"))
log_path.unlink(missing_ok=True)
try:
raise RuntimeError()
except RuntimeError as e:
log("hello world", exc=e)
self.assertTrue(log_path.exists(), "Logs not created")
# Check log level elevation to ERROR
self.assertIn("ERROR] hello world", log_path.read_text(), "Exc not logged")

0 comments on commit 6ded040

Please sign in to comment.