From ebf526d6dac1b37171cda151cd2d8ac7e75d2dba Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 10:32:27 +0200 Subject: [PATCH] added test for multiple output logger streams --- tests/logging/CMakeLists.txt | 17 +++++++++++++++- tests/logging/streams.c | 33 +++++++++++++++++++++++++++++++ tests/logging/test_logging.py | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/logging/streams.c diff --git a/tests/logging/CMakeLists.txt b/tests/logging/CMakeLists.txt index 0db2412..dee6ddf 100644 --- a/tests/logging/CMakeLists.txt +++ b/tests/logging/CMakeLists.txt @@ -18,7 +18,7 @@ add_test(NAME logging_output COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_output) # ------------------------------------------------------- # -# CTEST 1 +# CTEST 2 # test the panic functionality of the logger add_executable(logging_panic @@ -31,3 +31,18 @@ set_target_properties(logging_panic add_test(NAME logging_panic WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_panic) + +# ------------------------------------------------------- # +# CTEST 3 +# test the ability to write to multiple output streams + +add_executable(logging_streams + ${PROJECT_SOURCE_DIR}/src/sys/log.c + streams.c) +set_target_properties(logging_streams + PROPERTIES + OUTPUT_NAME "stream" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) +add_test(NAME logging_streams + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_stream) diff --git a/tests/logging/streams.c b/tests/logging/streams.c new file mode 100644 index 0000000..97599da --- /dev/null +++ b/tests/logging/streams.c @@ -0,0 +1,33 @@ +// +// Created by servostar on 5/2/24. +// + +#include "sys/log.h" +#include + +static FILE* file; + +void close_file(void) { + if (file != NULL) { + fclose(file); + } +} + +int main(void) { + log_init(); + + // this should appear in stderr + INFO("should only be in stderr"); + + file = fopen("tmp/test.log", "w"); + if (file == NULL) { + PANIC("could not open file"); + } + atexit(close_file); + + log_register_stream(file); + + INFO("should be in both"); + + return 0; +} diff --git a/tests/logging/test_logging.py b/tests/logging/test_logging.py index 86816fd..0d32d48 100644 --- a/tests/logging/test_logging.py +++ b/tests/logging/test_logging.py @@ -2,6 +2,7 @@ import subprocess import sys import logging from logging import info, error +import os BIN_DIR = "bin/tests/logging/" @@ -47,6 +48,40 @@ def run_check_panic(): assert "after exit" not in output +def run_check_stream(): + info("started check panic...") + + info("creating temporary folder...") + + if not os.path.exists("tmp"): + os.mkdir("tmp") + + info("cleaning temporary folder...") + + if os.path.exists("tmp/test.log"): + os.remove("tmp/test.log") + + info("launching test binary...") + + p = subprocess.run(BIN_DIR + "stream", capture_output=True, text=True) + + info("checking exit code...") + + # check exit code + assert p.returncode == 0 + + with open("tmp/test.log", "r") as file: + assert "should be in both" in "".join(file.readlines()) + + output = p.stderr + + # check if logs appear (not) in default log output (stderr) + info("checking stderr...") + + assert "should only be in stderr" in output + assert "should be in both" in output + + if __name__ == "__main__": logging.basicConfig(level=logging.INFO) @@ -59,6 +94,8 @@ if __name__ == "__main__": run_check_output() case "check_panic": run_check_panic() + case "check_stream": + run_check_stream() case _: error(f"unknown target: {target}") exit(1)