From ab28ad8c3a3d32c417944dc32526e1f30c6615cf Mon Sep 17 00:00:00 2001 From: Thomas Jensen Date: Mon, 1 Nov 2021 14:19:52 +0100 Subject: [PATCH] Add workaround for mockable fprintf on MinGW --- src/regulex.c | 2 +- src/tools.c | 10 ++++++++++ src/tools.h | 8 ++++++++ utest/Makefile | 6 +++--- utest/global_mock.c | 20 ++++++++++---------- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/regulex.c b/src/regulex.c index 34947f8..bfa6252 100644 --- a/src/regulex.c +++ b/src/regulex.c @@ -55,7 +55,7 @@ pcre2_code *compile_pattern(char *pattern) if (re == NULL) { PCRE2_UCHAR buffer[256]; pcre2_get_error_message(errornumber, buffer, sizeof(buffer)); - fprintf(stderr, "Regular expression pattern \"%s\" failed to compile at position %d: %s\n", + bx_fprintf(stderr, "Regular expression pattern \"%s\" failed to compile at position %d: %s\n", pattern, (int) erroroffset, u32_strconv_to_output(buffer)); } return re; diff --git a/src/tools.c b/src/tools.c index 280b274..bc5f6da 100644 --- a/src/tools.c +++ b/src/tools.c @@ -737,4 +737,14 @@ char *bx_strndup(const char *s, size_t n) } + +void bx_fprintf(FILE *stream, const char *format, ...) +{ + va_list va; + va_start(va, format); + vfprintf(stream, format, va); + va_end(va); +} + + /*EOF*/ /* vim: set sw=4: */ diff --git a/src/tools.h b/src/tools.h index 26fc014..a2d610a 100644 --- a/src/tools.h +++ b/src/tools.h @@ -126,6 +126,14 @@ int tag_is_valid(char *tag); char *bx_strndup(const char *s, size_t n); +/** + * Just calls `fprintf()` internally, but this function can be mocked in unit tests. So, it's a "mockable `fprintf()`". + * @param stream Where to print, for example `stderr` + * @param format the format string, followed by the arguments of the format string + */ +void bx_fprintf(FILE *stream, const char *format, ...); + + #endif /*EOF*/ /* vim: set cindent sw=4: */ diff --git a/utest/Makefile b/utest/Makefile index 53ee963..b2ba069 100644 --- a/utest/Makefile +++ b/utest/Makefile @@ -25,7 +25,7 @@ UTEST_DIR = ../utest VPATH = $(SRC_DIR):$(SRC_DIR)/misc:$(UTEST_DIR) UTEST_NORM = global_mock.c tools_test.c regulex_test.o main.o -MOCKS = fprintf +MOCKS = bx_fprintf .PHONY: check_dir flags_unix flags_win32 flags_ utest @@ -47,8 +47,8 @@ flags_unix: $(eval UTEST_OBJ := $(UTEST_NORM:.c=.o)) flags_win32: - $(eval CFLAGS := -Os -s -m32 -I. -I$(SRC_DIR) -Wall -W $(CFLAGS_ADDTL)) - $(eval LDFLAGS := $(LDFLAGS) $(foreach MOCK,$(MOCKS),-Wl,--wrap=$(MOCK)) --coverage -s -m32 $(LDFLAGS_ADDTL)) + $(eval CFLAGS := -Os -s -std=c99 -m32 -I. -I$(SRC_DIR) -Wall -W $(CFLAGS_ADDTL)) + $(eval LDFLAGS := $(LDFLAGS) -s -std=c99 -m32 $(foreach MOCK,$(MOCKS),-Wl,--wrap=$(MOCK)) $(LDFLAGS_ADDTL)) $(eval UTEST_EXECUTABLE_NAME := unittest.exe) $(eval UTEST_OBJ := $(UTEST_NORM:.c=.o)) diff --git a/utest/global_mock.c b/utest/global_mock.c index 55624ce..910170d 100644 --- a/utest/global_mock.c +++ b/utest/global_mock.c @@ -65,25 +65,25 @@ void collect_reset() /** - * Mock of the `fprintf()` function which records its output instead of printing it. Assumes that no output string will - * be longer than 512 characters. - * @param __stream `stdout` or `stderr` - * @param __format the format string, followed by the arguments + * Mock of the `bx_fprintf()` function which records its output instead of printing it. Assumes that no output string + * will be longer than 512 characters. + * @param stream `stdout` or `stderr` + * @param format the format string, followed by the arguments */ -void __wrap_fprintf(FILE *__stream, const char *__format, ...) +void __wrap_bx_fprintf(FILE *stream, const char *format, ...) { - char **collect = __stream == stdout ? collect_out : collect_err; - int collect_size = __stream == stdout ? collect_out_size : collect_err_size; + char **collect = stream == stdout ? collect_out : collect_err; + int collect_size = stream == stdout ? collect_out_size : collect_err_size; collect = (char **) realloc(collect, ++collect_size * sizeof(char *)); char *s = (char *) malloc(512); va_list va; - va_start(va, __format); - vsprintf(s, __format, va); + va_start(va, format); + vsprintf(s, format, va); va_end(va); collect[collect_size - 1] = s; - if (__stream == stdout) { + if (stream == stdout) { collect_out = collect; collect_out_size = collect_size; }