From 4b24e0645cd9da75c3e42b2b6d5a167fba1cbb84 Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 09:49:14 +0200 Subject: [PATCH] added test for logger panic --- tests/logging/CMakeLists.txt | 24 +++++++++-- tests/logging/check_output.py | 22 ---------- tests/logging/{main.c => output.c} | 0 tests/logging/panic.c | 20 ++++++++++ tests/logging/test_logging.py | 64 ++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 26 deletions(-) delete mode 100644 tests/logging/check_output.py rename tests/logging/{main.c => output.c} (100%) create mode 100644 tests/logging/panic.c create mode 100644 tests/logging/test_logging.py diff --git a/tests/logging/CMakeLists.txt b/tests/logging/CMakeLists.txt index ebc9962..9c90431 100644 --- a/tests/logging/CMakeLists.txt +++ b/tests/logging/CMakeLists.txt @@ -1,9 +1,25 @@ include(CTest) -add_executable(logging ${PROJECT_SOURCE_DIR}/src/sys/log.c main.c) -target_include_directories(logging PUBLIC ${PROJECT_SOURCE_DIR}/src) -set_target_properties(logging +# ------------------------------------------------------- # +# CTEST 1 +# test the default output of the logger + +add_executable(logging_output ${PROJECT_SOURCE_DIR}/src/sys/log.c output.c) +target_include_directories(logging_output PUBLIC ${PROJECT_SOURCE_DIR}/src) +set_target_properties(logging_output PROPERTIES OUTPUT_NAME "output" RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) -add_test(NAME logging WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/check_output.py) +add_test(NAME logging_output WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_output) + +# ------------------------------------------------------- # +# CTEST 1 +# test the panic functionality of the logger + +add_executable(logging_panic ${PROJECT_SOURCE_DIR}/src/sys/log.c panic.c) +target_include_directories(logging_panic PUBLIC ${PROJECT_SOURCE_DIR}/src) +set_target_properties(logging_panic + PROPERTIES + OUTPUT_NAME "panic" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) +add_test(NAME logging_panic WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_panic) diff --git a/tests/logging/check_output.py b/tests/logging/check_output.py deleted file mode 100644 index b908cda..0000000 --- a/tests/logging/check_output.py +++ /dev/null @@ -1,22 +0,0 @@ -import subprocess - - -def run_logger_test(): - p = subprocess.run("bin/tests/logging/output", capture_output=True, text=True) - - # check exit code - if p.returncode != 0: - exit(p.returncode) - - output = p.stderr - - # check if logs appear in default log output (stderr) - - assert "logging some debug..." in output - assert "logging some info..." in output - assert "logging some warning..." in output - assert "logging some error..." in output - - -if __name__ == "__main__": - run_logger_test() diff --git a/tests/logging/main.c b/tests/logging/output.c similarity index 100% rename from tests/logging/main.c rename to tests/logging/output.c diff --git a/tests/logging/panic.c b/tests/logging/panic.c new file mode 100644 index 0000000..ecd8815 --- /dev/null +++ b/tests/logging/panic.c @@ -0,0 +1,20 @@ +// +// Created by servostar on 5/2/24. +// + +#include "sys/log.h" + +int main(void) { + log_init(); + + // this should appear in stderr + INFO("before exit"); + + PANIC("oooops something happened"); + + // this should NOT appear in stderr + // ^^^ + ERROR("after exit"); + + return 0; +} diff --git a/tests/logging/test_logging.py b/tests/logging/test_logging.py new file mode 100644 index 0000000..da88b19 --- /dev/null +++ b/tests/logging/test_logging.py @@ -0,0 +1,64 @@ +import subprocess +import sys +import logging +from logging import info, error + +BIN_DIR = "bin/tests/logging/" + + +def run_logger_test(): + info("started check output...") + + p = subprocess.run(BIN_DIR + "output", capture_output=True, text=True) + + info("checking exit code...") + + # check exit code + assert p.returncode == 0 + + output = p.stderr + + # check if logs appear in default log output (stderr) + info("checking stderr...") + + assert "logging some debug..." in output + assert "logging some info..." in output + assert "logging some warning..." in output + assert "logging some error..." in output + + +def run_check_panic(): + info("started check panic...") + + p = subprocess.run(BIN_DIR + "panic", capture_output=True, text=True) + + info("checking exit code...") + + # check exit code + assert p.returncode == 1 + + output = p.stderr + + # check if logs appear (not) in default log output (stderr) + info("checking stderr...") + + assert "before exit" in output + assert "oooops something happened" in output + assert "after exit" not in output + + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + + target = sys.argv[1] + + info(f"starting logging test suite with target: {target}") + + match target: + case "check_output": + run_logger_test() + case "check_panic": + run_check_panic() + case _: + error(f"unknown target: {target}") + exit(1)