From: Michael Tremer Date: Thu, 25 Jan 2024 16:44:56 +0000 (+0000) Subject: tests: Add Python tests for context X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ecfab818469a973c35db6300cde4bbc6eccd2778;p=pakfire.git tests: Add Python tests for context Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 2310118c..0eae4f2c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1136,6 +1136,7 @@ TESTS_ENVIRONMENT = \ topdir="$(shell pwd)" dist_check_SCRIPTS = \ + tests/python/ctx.py \ tests/python/keys.py \ tests/python/jail.py \ tests/python/package.py diff --git a/tests/python/ctx.py b/tests/python/ctx.py new file mode 100755 index 00000000..b7e1126a --- /dev/null +++ b/tests/python/ctx.py @@ -0,0 +1,78 @@ +#!/usr/bin/python3 +############################################################################### +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2024 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +import logging +import pakfire + +import tests + +class CtxTests(tests.TestCase): + """ + This tests the pakfire context + """ + def test_create(self): + """ + Create a context without any further ado + """ + ctx = self.setup_ctx() + + self.assertIsInstance(ctx, pakfire.Ctx) + + # Logging + + def test_log_default(self): + """ + Tests if there is any log output if the default logger is being used + """ + logger = logging.getLogger("pakfire") + + with self.assertLogs(logger, logging.DEBUG): + self.setup_pakfire() + + def test_log_custom(self): + """ + Tests if log messages can be received using a custom logger + """ + # A simple logger that stores everything it receives in a buffer + class Logger(object): + def __init__(self): + self.buffer = [] + + def log(self, *args): + self.buffer.append(args) + + logger = Logger() + + # Setup a new context + ctx = self.setup_ctx() + + # Configure the logger + ctx.set_logger(logger) + + # Create a new Pakfire instance to create some log messages + self.setup_pakfire(ctx=ctx) + + # Check if we have received anything + self.assertTrue(logger.buffer) + + +if __name__ == "__main__": + tests.main()