Skip to content

Commit

Permalink
Test error output in catch tests (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
hummeltech authored Mar 24, 2024
1 parent 743cf65 commit 2ce51df
Show file tree
Hide file tree
Showing 12 changed files with 932 additions and 440 deletions.
8 changes: 7 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ bin_PROGRAMS = \
render_speedtest
noinst_PROGRAMS = \
gen_tile_test \
renderd_config_test \
renderd_test \
render_expired_test \
render_list_test \
Expand Down Expand Up @@ -108,6 +109,10 @@ gen_tile_test_CFLAGS = -DMAIN_ALREADY_DEFINED
gen_tile_test_CXXFLAGS = $(renderd_CXXFLAGS)
gen_tile_test_LDADD = $(renderd_LDADD) catch_test_common.o

renderd_config_test_SOURCES = \
tests/renderd_config_test.cpp
renderd_config_test_LDADD = $(GLIB_LIBS) catch_main.o catch_test_common.o

renderd_test_SOURCES = \
tests/renderd_test.cpp
renderd_test_LDADD = $(GLIB_LIBS) catch_main.o catch_test_common.o
Expand All @@ -132,8 +137,9 @@ CLEANFILES=*.slo mod_tile.la stderr.out src/*.slo src/*.lo src/.libs/* src/*.la

COMMA=,

test: gen_tile_test renderd_test render_expired_test render_list_test render_old_test render_speedtest_test
test: gen_tile_test renderd_config_test renderd_test render_expired_test render_list_test render_old_test render_speedtest_test
./gen_tile_test
./renderd_config_test
./renderd_test
./render_expired_test
./render_list_test
Expand Down
2 changes: 1 addition & 1 deletion src/render_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ int main(int argc, char **argv)
}

if (min_y_passed && max_y_passed && max_y < min_y) {
g_logger(G_LOG_LEVEL_CRITICAL, "Specified min-y (%i) is larger than max-x (%i).", min_y, max_y);
g_logger(G_LOG_LEVEL_CRITICAL, "Specified min-y (%i) is larger than max-y (%i).", min_y, max_y);
return 1;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/renderd.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,12 @@ int main(int argc, char **argv)

g_logger(G_LOG_LEVEL_INFO, "Renderd started (version %s)", VERSION);

process_config_file(config_file_name, active_renderd_section_num, G_LOG_LEVEL_INFO);

if (config_file_name_passed) {
free((void *)config_file_name);
}

g_logger(G_LOG_LEVEL_INFO, "Initialising request queue");
render_request_queue = request_queue_init();

Expand All @@ -794,12 +800,6 @@ int main(int argc, char **argv)
return 1;
}

process_config_file(config_file_name, active_renderd_section_num, G_LOG_LEVEL_INFO);

if (config_file_name_passed) {
free((void *)config_file_name);
}

fd = server_socket_init(&config);

#if 0
Expand Down
18 changes: 17 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ add_test(
COMMAND renderd_test
)

add_test(
NAME renderd_config_test
COMMAND renderd_config_test
)

foreach(STORAGE_BACKEND_INDEX RANGE ${STORAGE_BACKENDS_LENGTH})
# Get STORAGE_BACKEND from STORAGE_BACKENDS list
list(GET STORAGE_BACKENDS ${STORAGE_BACKEND_INDEX} STORAGE_BACKEND)
Expand Down Expand Up @@ -1162,7 +1167,7 @@ target_include_directories(catch_test_common_o PRIVATE ${GLIB_INCLUDE_DIRS})

add_compile_definitions(
PROJECT_BINARY_DIR="${PROJECT_BINARY_DIR}/src"
RENDERD_CONF="${PROJECT_SOURCE_DIR}/etc/renderd/renderd.conf"
RENDERD_CONF="${PROJECT_SOURCE_DIR}/etc/renderd/renderd.conf.examples"
)
include_directories(${PROJECT_SOURCE_DIR}/includes)
link_libraries(${GLIB_LIBRARIES})
Expand Down Expand Up @@ -1221,3 +1226,14 @@ add_executable(renderd_test
$<TARGET_OBJECTS:catch_main_o>
renderd_test.cpp
)

#-----------------------------------------------------------------------------
#
# renderd_config_test
#
#-----------------------------------------------------------------------------

add_executable(renderd_config_test
$<TARGET_OBJECTS:catch_main_o>
renderd_config_test.cpp
)
46 changes: 24 additions & 22 deletions tests/catch/catch_test_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@
#include <glib.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unistd.h>

extern int foreground;
#include "catch_test_common.hpp"

typedef struct _captured_stdio {
int temp_fd;
int pipes[2];
} captured_stdio;
extern int foreground;

captured_stdio captured_stderr;
captured_stdio captured_stdout;

std::string read_stderr(int buffer_size)
{
char buffer[buffer_size];
read(captured_stderr.pipes[0], buffer, buffer_size);
return buffer;
}

void capture_stderr()
{
// Flush stderr first if you've previously printed something
Expand All @@ -52,7 +55,7 @@ void capture_stderr()
captured_stderr.pipes[1] = pipes[1];
}

std::string get_captured_stderr(bool print = false)
std::string get_captured_stderr(bool print)
{
// Terminate captured output with a zero
write(captured_stderr.pipes[1], "", 1);
Expand All @@ -61,12 +64,8 @@ std::string get_captured_stderr(bool print = false)
fflush(stderr);
dup2(captured_stderr.temp_fd, fileno(stderr));

// Save & return the captured output
std::string log_lines;
const int buffer_size = 4096;
char buffer[buffer_size];
read(captured_stderr.pipes[0], buffer, buffer_size);
log_lines += buffer;
// Save & return the captured stderr
std::string log_lines = read_stderr();

if (print) {
std::cout << "err_log_lines: " << log_lines << "\n";
Expand All @@ -75,6 +74,13 @@ std::string get_captured_stderr(bool print = false)
return log_lines;
}

std::string read_stdout(int buffer_size)
{
char buffer[buffer_size];
read(captured_stdout.pipes[0], buffer, buffer_size);
return buffer;
}

void capture_stdout()
{
// Flush stdout first if you've previously printed something
Expand All @@ -94,7 +100,7 @@ void capture_stdout()
captured_stdout.pipes[1] = pipes[1];
}

std::string get_captured_stdout(bool print = false)
std::string get_captured_stdout(bool print)
{
// Terminate captured output with a zero
write(captured_stdout.pipes[1], "", 1);
Expand All @@ -103,12 +109,8 @@ std::string get_captured_stdout(bool print = false)
fflush(stdout);
dup2(captured_stdout.temp_fd, fileno(stdout));

// Save & return the captured output
std::string log_lines;
const int buffer_size = 4096;
char buffer[buffer_size];
read(captured_stdout.pipes[0], buffer, buffer_size);
log_lines += buffer;
// Save & return the captured stdout
std::string log_lines = read_stdout();

if (print) {
std::cout << "out_log_lines: " << log_lines << "\n";
Expand All @@ -117,7 +119,7 @@ std::string get_captured_stdout(bool print = false)
return log_lines;
}

void start_capture(bool debug = false)
void start_capture(bool debug)
{
foreground = debug ? 1 : 0;

Expand All @@ -135,7 +137,7 @@ void start_capture(bool debug = false)
capture_stdout();
}

std::tuple<std::string, std::string> end_capture(bool print = false)
std::tuple<std::string, std::string> end_capture(bool print)
{
setenv("G_MESSAGES_DEBUG", "", 1);
#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 79
Expand Down
8 changes: 8 additions & 0 deletions tests/catch/catch_test_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
#ifndef CATCH_TEST_COMMON_HPP
#define CATCH_TEST_COMMON_HPP

typedef struct _captured_stdio {
int temp_fd;
int pipes[2];
} captured_stdio;

std::string read_stderr(int buffer_size = 16384);
std::string read_stdout(int buffer_size = 16384);

void capture_stderr();
void capture_stdout();

Expand Down
Loading

0 comments on commit 2ce51df

Please sign in to comment.