Skip to content

Commit 31ed836

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 31ed836

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/test_log.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
def test_configure_logging(self):
37+
"""Test configure_logging API."""
38+
with self.assertNoLogs("libyang", level="ERROR"):
39+
self._cause_log()
40+
41+
configure_logging(True, logging.INFO)
42+
with self.assertLogs("libyang", level="ERROR"):
43+
self._cause_log()
44+
45+
@unittest.skipIf(sys.version_info < (3, 10), "assertNoLogs python3.10 or higher")
46+
def test_with_temp_log(self):
47+
"""Test configure_logging API."""
48+
configure_logging(True, logging.INFO)
49+
50+
with self.assertLogs("libyang", level="ERROR"):
51+
self._cause_log()
52+
53+
with self.assertNoLogs("libyang", level="ERROR"):
54+
with temp_log_options(0):
55+
self._cause_log()
56+
57+
with self.assertLogs("libyang", level="ERROR"):
58+
self._cause_log()

0 commit comments

Comments
 (0)