From: Pete Batard Date: Thu, 21 Feb 2013 00:24:18 +0000 (+0000) Subject: Tests: Simplify stdout redirection and fix Windows assertion error on cleanup X-Git-Tag: upstream/1.0.21~534 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=76eecc6ce11d1bedee369859899901037dcd4cc2;p=platform%2Fupstream%2Flibusb.git 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 --- diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 62b3532..4d6f906 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 10617 +#define LIBUSB_NANO 10618 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 */