added test for logger panic

This commit is contained in:
Sven Vogel 2024-05-02 09:49:14 +02:00
parent b386c11043
commit 4b24e0645c
5 changed files with 104 additions and 26 deletions

View File

@ -1,9 +1,25 @@
include(CTest) include(CTest)
add_executable(logging ${PROJECT_SOURCE_DIR}/src/sys/log.c main.c) # ------------------------------------------------------- #
target_include_directories(logging PUBLIC ${PROJECT_SOURCE_DIR}/src) # CTEST 1
set_target_properties(logging # 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 PROPERTIES
OUTPUT_NAME "output" OUTPUT_NAME "output"
RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) 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)

View File

@ -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()

20
tests/logging/panic.c Normal file
View File

@ -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;
}

View File

@ -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)