Skip to content

Commit 1deecd8

Browse files
choppsv1rjarry
authored andcommitted
tests: add a logging test for new temp_log_options API
Add a new log unit test. Add a test for the newly exposed temp_log_options() function as well as the already implemented configure_logging() function. Signed-off-by: Christian Hopps <chopps@labn.net>
1 parent ad059d6 commit 1deecd8

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tests/test_log.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright (c) 2025, LabN Consulting, L.L.C.
2+
# SPDX-License-Identifier: MIT
3+
4+
import logging
5+
import os
6+
import sys
7+
import unittest
8+
9+
from libyang import Context, LibyangError, configure_logging, temp_log_options
10+
11+
12+
YANG_DIR = os.path.join(os.path.dirname(__file__), "yang")
13+
14+
15+
class LogTest(unittest.TestCase):
16+
def setUp(self):
17+
self.ctx = Context(YANG_DIR)
18+
configure_logging(False, logging.INFO)
19+
20+
def tearDown(self):
21+
if self.ctx is not None:
22+
self.ctx.destroy()
23+
self.ctx = None
24+
25+
def _cause_log(self):
26+
try:
27+
assert self.ctx is not None
28+
_ = self.ctx.parse_data_mem("bad", fmt="xml")
29+
except LibyangError:
30+
pass
31+
32+
@unittest.skipIf(sys.version_info < (3, 10), "Test requires Python 3.10+")
33+
def test_configure_logging(self):
34+
"""Test configure_logging API."""
35+
with self.assertNoLogs("libyang", level="ERROR"):
36+
self._cause_log()
37+
38+
configure_logging(True, logging.INFO)
39+
with self.assertLogs("libyang", level="ERROR"):
40+
self._cause_log()
41+
42+
@unittest.skipIf(sys.version_info < (3, 10), "Test requires Python 3.10+")
43+
def test_with_temp_log(self):
44+
"""Test configure_logging API."""
45+
configure_logging(True, logging.INFO)
46+
47+
with self.assertLogs("libyang", level="ERROR"):
48+
self._cause_log()
49+
50+
with self.assertNoLogs("libyang", level="ERROR"):
51+
with temp_log_options(0):
52+
self._cause_log()
53+
54+
with self.assertLogs("libyang", level="ERROR"):
55+
self._cause_log()

0 commit comments

Comments
 (0)