From c099a60ecf649cd76da74a50b5d83297f9918fde Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 10:50:24 +0200 Subject: [PATCH] added test for compile time log level filter --- tests/logging/CMakeLists.txt | 15 +++++++++++++++ tests/logging/level.c | 18 ++++++++++++++++++ tests/logging/test_logging.py | 23 +++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 tests/logging/level.c diff --git a/tests/logging/CMakeLists.txt b/tests/logging/CMakeLists.txt index dee6ddf..0f14079 100644 --- a/tests/logging/CMakeLists.txt +++ b/tests/logging/CMakeLists.txt @@ -46,3 +46,18 @@ set_target_properties(logging_streams add_test(NAME logging_streams WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_stream) + +# ------------------------------------------------------- # +# CTEST 4 +# test compile time log level switch + +add_executable(logging_level + ${PROJECT_SOURCE_DIR}/src/sys/log.c + level.c) +set_target_properties(logging_level + PROPERTIES + OUTPUT_NAME "level" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) +add_test(NAME logging_level + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_level) diff --git a/tests/logging/level.c b/tests/logging/level.c new file mode 100644 index 0000000..6a4f055 --- /dev/null +++ b/tests/logging/level.c @@ -0,0 +1,18 @@ +// +// Created by servostar on 5/2/24. +// + +#include "sys/log.h" + +#define LOG_LEVEL LOG_LEVEL_WARNING + +int main(void) { + log_init(); + + DEBUG("logging some debug..."); + INFO("logging some info..."); + WARN("logging some warning..."); + ERROR("logging some error..."); + + return 0; +} diff --git a/tests/logging/test_logging.py b/tests/logging/test_logging.py index 0d32d48..246a9d5 100644 --- a/tests/logging/test_logging.py +++ b/tests/logging/test_logging.py @@ -28,6 +28,27 @@ def run_check_output(): assert "logging some error..." in output +def run_check_level(): + info("started check level...") + + p = subprocess.run(BIN_DIR + "level", 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..." not in output + assert "logging some info..." not in output + assert "logging some warning..." in output + assert "logging some error..." in output + + def run_check_panic(): info("started check panic...") @@ -96,6 +117,8 @@ if __name__ == "__main__": run_check_panic() case "check_stream": run_check_stream() + case "check_level": + run_check_level() case _: error(f"unknown target: {target}") exit(1)