Skip to content

Commit 903661a

Browse files
committed
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 40eb9fc commit 903661a

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tests/test_log.py

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

0 commit comments

Comments
 (0)