Skip to content

Commit c35aca2

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 <[email protected]>
1 parent 40eb9fc commit c35aca2

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

tests/test_log.py

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

0 commit comments

Comments
 (0)