diff options
author | Pete Batard <pete@akeo.ie> | 2013-02-21 00:24:18 +0000 |
---|---|---|
committer | Pete Batard <pete@akeo.ie> | 2013-02-22 00:49:29 +0000 |
commit | 76eecc6ce11d1bedee369859899901037dcd4cc2 (patch) | |
tree | c5362b1616f4c54126539fcaa5b67b20f4264ffc /tests | |
parent | 4a924eb1c4a302426efdde7196b299e045f4ff96 (diff) | |
download | libusb-76eecc6ce11d1bedee369859899901037dcd4cc2.tar.gz |
Tests: Simplify stdout redirection and fix Windows assertion error on cleanup
* cleanup_test_output() produced an assertion error when compiled with MSVC
due to closing ctx->output_file
* set ctx->output_file to stdout by default
Diffstat (limited to 'tests')
-rw-r--r-- | tests/libusbx_testlib.h | 3 | ||||
-rw-r--r-- | tests/testlib.c | 76 |
2 files changed, 44 insertions, 35 deletions
diff --git a/tests/libusbx_testlib.h b/tests/libusbx_testlib.h index cb9ac9c..06dbc8e 100644 --- a/tests/libusbx_testlib.h +++ b/tests/libusbx_testlib.h @@ -53,7 +53,8 @@ typedef struct { int test_count; bool list_tests; bool verbose; - int output_fd; + int old_stdout; + int old_stderr; FILE* output_file; int null_fd; } libusbx_testlib_ctx; diff --git a/tests/testlib.c b/tests/testlib.c index 3a91933..45bc5d4 100644 --- a/tests/testlib.c +++ b/tests/testlib.c @@ -79,17 +79,23 @@ static void print_usage(int argc, char ** argv) static void cleanup_test_output(libusbx_testlib_ctx * ctx) { #ifndef DISABLE_STDOUT_REDIRECTION - if (ctx->output_file != NULL) { - fclose(ctx->output_file); - ctx->output_file = NULL; - } - if (ctx->output_fd != INVALID_FD) { - close(ctx->output_fd); - ctx->output_fd = INVALID_FD; - } - if (ctx->null_fd != INVALID_FD) { - close(ctx->null_fd); - ctx->null_fd = INVALID_FD; + if (!ctx->verbose) { + if (ctx->old_stdout != INVALID_FD) { + _dup2(ctx->old_stdout, STDOUT_FILENO); + ctx->old_stdout = INVALID_FD; + } + if (ctx->old_stderr != INVALID_FD) { + _dup2(ctx->old_stderr, STDERR_FILENO); + ctx->old_stderr = INVALID_FD; + } + if (ctx->null_fd != INVALID_FD) { + close(ctx->null_fd); + ctx->null_fd = INVALID_FD; + } + if (ctx->output_file != stdout) { + fclose(ctx->output_file); + ctx->output_file = stdout; + } } #endif } @@ -100,26 +106,23 @@ static void cleanup_test_output(libusbx_testlib_ctx * ctx) */ static int setup_test_output(libusbx_testlib_ctx * ctx) { -#ifdef DISABLE_STDOUT_REDIRECTION - ctx->output_fd = STDOUT_FILENO; - ctx->output_file = stdout; - return 0; -#else - /* Keep a copy of STDOUT for test output */ - ctx->output_fd = dup(STDOUT_FILENO); - if (ctx->output_fd < 0) { - ctx->output_fd = INVALID_FD; - printf("Failed to duplicate output handle: %d\n", errno); - return 1; - } - ctx->output_file = fdopen(ctx->output_fd, "w"); - if (!ctx->output_file) { - cleanup_test_output(ctx); - printf("Failed to open FILE for output handle: %d\n", errno); - return 1; - } +#ifndef DISABLE_STDOUT_REDIRECTION /* Stop output to stdout and stderr from being displayed if using non-verbose output */ if (!ctx->verbose) { + /* Keep a copy of STDOUT and STDERR */ + ctx->old_stdout = dup(STDOUT_FILENO); + if (ctx->old_stdout < 0) { + ctx->old_stdout = INVALID_FD; + printf("Failed to duplicate stdout handle: %d\n", errno); + return 1; + } + ctx->old_stderr = dup(STDERR_FILENO); + if (ctx->old_stderr < 0) { + ctx->old_stderr = INVALID_FD; + cleanup_test_output(ctx); + printf("Failed to duplicate stderr handle: %d\n", errno); + return 1; + } /* Redirect STDOUT_FILENO and STDERR_FILENO to /dev/null or "nul"*/ ctx->null_fd = open(NULL_PATH, O_WRONLY); if (ctx->null_fd < 0) { @@ -133,17 +136,21 @@ static int setup_test_output(libusbx_testlib_ctx * ctx) cleanup_test_output(ctx); return 1; } + ctx->output_file = fdopen(ctx->old_stdout, "w"); + if (!ctx->output_file) { + cleanup_test_output(ctx); + printf("Failed to open FILE for output handle: %d\n", errno); + return 1; + } } - return 0; #endif + return 0; } void libusbx_testlib_logf(libusbx_testlib_ctx * ctx, const char* fmt, ...) { va_list va; - if (!ctx->output_file) - return; va_start(va, fmt); vfprintf(ctx->output_file, fmt, va); va_end(va); @@ -171,8 +178,9 @@ int libusbx_testlib_run_tests(int argc, ctx.test_count = 0; ctx.list_tests = false; ctx.verbose = false; - ctx.output_fd = INVALID_FD; - ctx.output_file = NULL; + ctx.old_stdout = INVALID_FD; + ctx.old_stderr = INVALID_FD; + ctx.output_file = stdout; ctx.null_fd = INVALID_FD; /* Parse command line options */ |