From 2b2f143f280bae14ff4850cacca597acef4e1364 Mon Sep 17 00:00:00 2001 From: birgits Date: Fri, 2 Feb 2024 16:11:41 -0800 Subject: [PATCH] Make separate test for when logging file is written to user dir as this leads to problems on github with os Windows --- tests/tools/test_logger.py | 45 ++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/tests/tools/test_logger.py b/tests/tools/test_logger.py index 947acb76..2da11984 100644 --- a/tests/tools/test_logger.py +++ b/tests/tools/test_logger.py @@ -1,22 +1,26 @@ import logging import os +import pytest + from edisgo.tools.logger import setup_logger -class TestClass: - def test_setup_logger(self): - def check_file_output(filename, output): - with open(filename) as file: - last_line = file.readlines()[-1].split(" ")[3:] - last_line = " ".join(last_line) - assert last_line == output +def check_file_output(filename, output): + with open(filename) as file: + last_line = file.readlines()[-1].split(" ")[3:] + last_line = " ".join(last_line) + assert last_line == output + + +def reset_loggers(): + logger = logging.getLogger("edisgo") + logger.handlers.clear() + logger.propagate = True - def reset_loggers(): - logger = logging.getLogger("edisgo") - logger.handlers.clear() - logger.propagate = True +class TestClass: + def test_setup_logger(self): filename = os.path.join( os.path.expanduser("~"), ".edisgo", "log", "test_log.log" ) @@ -43,6 +47,19 @@ def reset_loggers(): reset_loggers() os.remove(filename) + @pytest.mark.runonlinux + def test_setup_logger_2(self): + """ + This test is only run on linux, as the log file is written to the user + home directory, which is not allowed when tests are run on github. + + """ + + # delete any existing log files + log_files = [_ for _ in os.listdir(os.getcwd()) if ".log" in _] + for log_file in log_files: + os.remove(log_file) + setup_logger( loggers=[ {"name": "edisgo", "file_level": "debug", "stream_level": "debug"}, @@ -52,11 +69,7 @@ def reset_loggers(): ) logger = logging.getLogger("edisgo") - filename = [_ for _ in os.listdir(os.getcwd()) if ".log" in _] - # if not 1 there are several log files, which shouldn't be the case and could - # mess up the following tests - assert len(filename) == 1 - filename = filename[0] + filename = [_ for _ in os.listdir(os.getcwd()) if ".log" in _][0] # Test that edisgo logger writes to file. logger.debug("edisgo") check_file_output(filename, "edisgo - DEBUG: edisgo\n")