From 54957a144bce03d14b337b3eb3007ea220266880 Mon Sep 17 00:00:00 2001 From: David Sargeant Date: Sat, 25 Nov 2023 11:34:07 -0700 Subject: [PATCH] Change bx_fprintf to a function pointer to enable unit testing on MacOS --- src/tools.c | 15 ++++++++++++++- src/tools.h | 21 +++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/tools.c b/src/tools.c index 4d9aca8..58a8de7 100644 --- a/src/tools.c +++ b/src/tools.c @@ -42,6 +42,15 @@ static pcre2_code *pattern_ascii_id = NULL; static pcre2_code *pattern_ascii_id_strict = NULL; +/** + * Initialize the `bx_fprintf` function pointer to point to the original + * `bx_fprintf` function, now renamed `bx_fprintf_original`. During unit + * tests, this will be replaced with `__wrap_bx_fprintf`, which stores + * the result that would have been printed so the output can be validated. + * This is necessary for unit testing and CI to work with MacOS. + */ +bx_fprintf_t bx_fprintf = bx_fprintf_original; + static pcre2_code *get_pattern_ascii_id(int strict) { @@ -849,7 +858,7 @@ char *bx_strndup(const char *s, size_t n) -void bx_fprintf(FILE *stream, const char *format, ...) +void bx_fprintf_original(FILE *stream, const char *format, ...) { va_list va; va_start(va, format); @@ -858,6 +867,10 @@ void bx_fprintf(FILE *stream, const char *format, ...) } +void set_bx_fprintf(bx_fprintf_t bx_fprintf_function) { + bx_fprintf = bx_fprintf_function; +} + FILE *bx_fopens(bxstr_t *pathname, char *mode) { diff --git a/src/tools.h b/src/tools.h index 65b3ba0..b483240 100644 --- a/src/tools.h +++ b/src/tools.h @@ -40,6 +40,18 @@ } +/** + * Define type for a function pointer to specify which `bx_fprintf` function + * will be called. This enables unit testing on MacOS, since Apple's `ld` + * linker does not support the `--wrap` flag that GNU `ld` does. + */ +typedef void (*bx_fprintf_t)(FILE *stream, const char *format, ...); + +/** + * Declare function pointer to be changed when running unit tests + */ +extern bx_fprintf_t bx_fprintf; + int empty_line(const line_t *line); @@ -173,7 +185,7 @@ int tag_is_valid(char *tag); /** * Duplicate at most `n` bytes from the given string `s`. Memory for the new string is obtained with `malloc()`, and - * can be freed with `free()`. A terminating null byte is added. We include this implementation because the libc's + * can be freed with `free()`. A terminating null byte is added. We include this implementation because the libc's * `strndup()` is not consistently available across all platforms. * @param s a string * @param n maximum number of characters to copy (excluding the null byte) @@ -187,9 +199,14 @@ char *bx_strndup(const char *s, size_t n); * @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, ...); +void bx_fprintf_original(FILE *stream, const char *format, ...); +/** + * Set the bx_fprintf_ptr function pointer to point to a specific function + */ +void set_bx_fprintf(bx_fprintf_t func_to_use); + /** * Determine if the given string is an "ASCII ID", which means: * - It consists only of the letters `abcdefghijklmnopqrstuvwxyz-0123456789`. If not in strict mode, upper case A-Z