While fixing #69 [1] (which has now been pushed), I got bothered by an
assertion error that was due to closing stdout, and since I didn't
initially get what the stdout redirection was all about here, I decided
to (hopefully) simplify it.
The attached patch is the result of that.
It also switches the context test to using INFO rather then DEBUG, as a
lot of polluting info is generated in verbose mode otherwise, and I
don't think this impacts the test.
Note that I haven't tested this patch on WinCE, or any other platform
but Windows for that matter.
Regards,
/Pete
[1] https://github.com/libusbx/libusbx/issues/69
>From 726a4370cfea10d578541e8f846adc377b0ab146 Mon Sep 17 00:00:00 2001
From: Pete Batard <p...@akeo.ie>
Date: Thu, 21 Feb 2013 00:24:18 +0000
Subject: [PATCH] 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
* also set log level for test_default_context_change() to INFO rather than DEBUG
---
tests/libusbx_testlib.h | 3 +-
tests/stress.c | 6 ++--
tests/testlib.c | 76 +++++++++++++++++++++++++++----------------------
3 files changed, 48 insertions(+), 39 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/stress.c b/tests/stress.c
index 26860a0..b5f8df3 100644
--- a/tests/stress.c
+++ b/tests/stress.c
@@ -128,9 +128,9 @@ static libusbx_testlib_result
test_default_context_change(libusbx_testlib_ctx *
return TEST_STATUS_FAILURE;
}
- /* Enable debug output, to be sure to use the context */
- libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_DEBUG);
- libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_DEBUG);
+ /* Enable log output, to be sure to use the context */
+ libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_INFO);
+ libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_INFO);
/* Now create a reference to the default context */
r = libusb_init(NULL);
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 */
--
1.8.0.msysgit.0
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_feb
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel