From 34b7af9f889397caef88f2dbb00347434eae6695 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Wed, 13 Nov 2019 16:58:44 +0200 Subject: test: cleanup abd simplify diagnostic test logic. Signed-off-by: Gediminas Jakutis --- test/diagnostic.c | 96 ++++++++++--------------------------------------------- 1 file changed, 17 insertions(+), 79 deletions(-) (limited to 'test/diagnostic.c') diff --git a/test/diagnostic.c b/test/diagnostic.c index 363743b..5f85ea6 100644 --- a/test/diagnostic.c +++ b/test/diagnostic.c @@ -25,22 +25,13 @@ #include "test.h" #include "rin/diagnostic.h" -struct stdiocap { - int pipefd[2]; - int origfd; - int backupfd; - FILE *pipe; -}; - static int err_test(void); static int warn_test(void); static int fixme_test(void); static int info_test(void); static int format_test(void); -static struct stdiocap capture_stdio_start(FILE *stream); -static void capture_stdio_stop(struct stdiocap *cap); static char *visible_newlines(const char * const in); -static int test_default_channels(const char * const in, const char * const expected, size_t i, int usearg, const char *func_name, void (*func)(const char *, const char *, ...)); +static int test_default_channels(const char * const in, const char * const expected, size_t i, const char *func_name, enum rin_diag_outstream chan); int diagnostic_test(char *testname) { @@ -64,15 +55,13 @@ int diagnostic_test(char *testname) static int err_test(void) { size_t i; - int usearg; int ret; static const char *in[] = {"", "test", "test number is %zu", "%zu tests ran"}; static const char *expected[] = {"error:err_test:\n", "error:err_test:test\n", "error:err_test:test number is 3\n", "error:err_test:4 tests ran\n"}; for (i = 0; i < arrlen(in); ++i) { - usearg = i < (arrlen(in) / 2) ? 0 : 1; - ret = test_default_channels(in[i], expected[i], i, usearg, __func__, __rin_err); + ret = test_default_channels(in[i], expected[i], i, __func__, rin_diag_err); } return ret; @@ -81,15 +70,13 @@ static int err_test(void) static int warn_test(void) { size_t i; - int usearg; int ret; static const char *in[] = {"", "test", "test number is %zu", "%zu tests ran"}; static const char *expected[] = {"warning:warn_test:\n", "warning:warn_test:test\n", "warning:warn_test:test number is 3\n", "warning:warn_test:4 tests ran\n"}; for (i = 0; i < arrlen(in); ++i) { - usearg = i < (arrlen(in) / 2) ? 0 : 1; - ret = test_default_channels(in[i], expected[i], i, usearg, __func__, __rin_warn); + ret = test_default_channels(in[i], expected[i], i, __func__, rin_diag_warn); } return ret; @@ -98,15 +85,13 @@ static int warn_test(void) static int fixme_test(void) { size_t i; - int usearg; int ret; static const char *in[] = {"", "test", "test number is %zu", "%zu tests ran"}; static const char *expected[] = {"fixme:fixme_test:\n", "fixme:fixme_test:test\n", "fixme:fixme_test:test number is 3\n", "fixme:fixme_test:4 tests ran\n"}; for (i = 0; i < arrlen(in); ++i) { - usearg = i < (arrlen(in) / 2) ? 0 : 1; - ret = test_default_channels(in[i], expected[i], i, usearg, __func__, __rin_fixme); + ret = test_default_channels(in[i], expected[i], i, __func__, rin_diag_fixme); } return ret; @@ -115,23 +100,15 @@ static int fixme_test(void) static int info_test(void) { size_t i; - int usearg; int ret; static const char *in[] = {"", "test", "test number is %zu", "%zu tests ran"}; static const char *expected[] = {"info:info_test:\n", "info:info_test:test\n", "info:info_test:test number is 3\n", "info:info_test:4 tests ran\n"}; - /* `ninja test` seems to be intercepting stdout, so we cannot test the default outstream */ - rin_diag_set_outstream(rin_diag_info, stderr); - for (i = 0; i < arrlen(in); ++i) { - usearg = i < (arrlen(in) / 2) ? 0 : 1; - ret = test_default_channels(in[i], expected[i], i, usearg, __func__, __rin_info); + ret = test_default_channels(in[i], expected[i], i, __func__, rin_diag_info); } - /* restore the default, kind of */ - rin_diag_set_outstream(rin_diag_info, NULL); - return ret; } @@ -140,44 +117,6 @@ static int format_test(void) return 77; } -static struct stdiocap capture_stdio_start(FILE *stream) -{ - struct stdiocap ret; - - ret.origfd = fileno(stream); - ret.backupfd = dup(ret.origfd); - - if (pipe(ret.pipefd)) { - close(ret.backupfd); - ret.backupfd = -1; - ret.origfd = -1; - ret.pipefd[0] = -1; - ret.pipefd[1] = -1; - ret.pipe = NULL; - } - - dup2(ret.pipefd[1], ret.origfd); - ret.pipe = fdopen(ret.pipefd[0], "r"); - return ret; -} - -static void capture_stdio_stop(struct stdiocap *cap) -{ - /* restore original fd to the stream */ - dup2(cap->backupfd, cap->origfd); - close(cap->backupfd); - close(cap->pipefd[1]); - close(cap->pipefd[0]); - - /* close everything we opened and invalidate all the descriptors */ - fclose(cap->pipe); - cap->backupfd = -1; - cap->origfd = -1; - cap->pipefd[0] = -1; - cap->pipefd[1] = -1; - cap->pipe = NULL; -} - static char *visible_newlines(const char * const in) { char *ret; @@ -207,32 +146,31 @@ static char *visible_newlines(const char * const in) return ret; } -static int test_default_channels(const char * const in, const char * const expected, size_t i, int usearg, const char *func_name, void (*func)(const char *, const char *, ...)) +static int test_default_channels(const char * const in, const char * const expected, size_t i, const char *func_name, enum rin_diag_outstream chan) { char buf[64] = {0}; - struct stdiocap cap; int ret = 0; + void (*func[])(const char *, const char *, ...) = {__rin_err, __rin_warn, __rin_fixme, __rin_info}; + FILE *capstream; - cap = capture_stdio_start(stderr); - - if (!cap.pipe) { + if (!(capstream = tmpfile())) { ret = -1; goto fail; } - if (usearg) { - func(func_name, in, i + 1); - } else { - func(func_name, in); - } + rin_diag_set_outstream(chan, capstream); + func[chan](func_name, in, i + 1); + rewind(capstream); - if (!(fgets(buf, sizeof(buf), cap.pipe))) { - ret = 1; + if (!(fgets(buf, sizeof(buf), capstream)) && strlen(expected)) { + ret = -1; goto fail; } fail: - capture_stdio_stop(&cap); + if (capstream) { + fclose(capstream); + } if (ret == -1) { ret = ok(1, "%s: unexpected test program failure on iteration %zu", __func__, i); -- cgit v1.2.3