From bfe269b8a0ebe89b6b0695bdfb727dbf9ac273d5 Mon Sep 17 00:00:00 2001 From: Timothy J Fontaine Date: Thu, 21 Feb 2013 11:46:59 -0800 Subject: [PATCH] test: add tap output Given UV_TAP_OUTPUT being set, test result output should use TAP formatting --- test/run-benchmarks.c | 2 +- test/run-tests.c | 2 +- test/runner-unix.c | 20 +++++++++++++++----- test/runner.c | 32 +++++++++++++++++++++++++------- test/runner.h | 8 +++++++- 5 files changed, 49 insertions(+), 15 deletions(-) diff --git a/test/run-benchmarks.c b/test/run-benchmarks.c index af11beb9b4..06732b71d3 100644 --- a/test/run-benchmarks.c +++ b/test/run-benchmarks.c @@ -60,5 +60,5 @@ static int maybe_run_test(int argc, char **argv) { return 42; } - return run_test(argv[1], BENCHMARK_TIMEOUT, 1); + return run_test(argv[1], BENCHMARK_TIMEOUT, 1, 1); } diff --git a/test/run-tests.c b/test/run-tests.c index a81c7e5ff6..d84be6a1a5 100644 --- a/test/run-tests.c +++ b/test/run-tests.c @@ -155,5 +155,5 @@ static int maybe_run_test(int argc, char **argv) { return 1; } - return run_test(argv[1], TEST_TIMEOUT, 0); + return run_test(argv[1], TEST_TIMEOUT, 0, 1); } diff --git a/test/runner-unix.c b/test/runner-unix.c index 267cc7cbf4..ebda5f8f1b 100644 --- a/test/runner-unix.c +++ b/test/runner-unix.c @@ -42,6 +42,7 @@ /* Do platform-specific initialization. */ void platform_init(int argc, char **argv) { const char* var = getenv("UV_RUN_AS_ROOT"); + const char* tap = getenv("UV_TAP_OUTPUT"); /* Running the tests as root is not smart - don't do it. */ if (getuid() == 0 && (var == NULL || atoi(var) <= 0)) { @@ -49,6 +50,8 @@ void platform_init(int argc, char **argv) { exit(1); } + tap_output = (tap != NULL && atoi(tap) > 0); + /* Disable stdio output buffering. */ setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); @@ -261,19 +264,26 @@ int process_copy_output(process_info_t *p, int fd) { return -1; } - ssize_t nread, nwritten; + ssize_t nwritten; char buf[1024]; - while ((nread = read(fileno(p->stdout_file), buf, 1024)) > 0) { - nwritten = write(fd, buf, nread); - /* TODO: what if write doesn't write the whole buffer... */ + /* TODO: what if the line is longer than buf */ + while (fgets(buf, sizeof(buf), p->stdout_file) != NULL) { + /* TODO: what if write doesn't write the whole buffer... */ + nwritten = 0; + + if (tap_output) + nwritten += write(fd, "#", 1); + + nwritten += write(fd, buf, strlen(buf)); + if (nwritten < 0) { perror("write"); return -1; } } - if (nread < 0) { + if (ferror(p->stdout_file)) { perror("read"); return -1; } diff --git a/test/runner.c b/test/runner.c index 01d6c773d2..1b9a980926 100644 --- a/test/runner.c +++ b/test/runner.c @@ -28,6 +28,8 @@ char executable_path[PATHMAX] = { '\0' }; +int tap_output = 0; + static void log_progress(int total, int passed, int failed, const char* name) { if (total == 0) @@ -76,7 +78,7 @@ const char* fmt(double d) { int run_tests(int timeout, int benchmark_output) { - int total, passed, failed; + int total, passed, failed, current; task_entry_t* task; /* Count the number of tests. */ @@ -87,29 +89,35 @@ int run_tests(int timeout, int benchmark_output) { } } + if (tap_output) { + LOGF("1..%d\n", total); + } + /* Run all tests. */ passed = 0; failed = 0; + current = 1; for (task = TASKS; task->main; task++) { if (task->is_helper) { continue; } rewind_cursor(); - if (!benchmark_output) { + if (!benchmark_output && !tap_output) { log_progress(total, passed, failed, task->task_name); } - if (run_test(task->task_name, timeout, benchmark_output) == 0) { + if (run_test(task->task_name, timeout, benchmark_output, current) == 0) { passed++; } else { failed++; } + current++; } rewind_cursor(); - if (!benchmark_output) { + if (!benchmark_output && !tap_output) { log_progress(total, passed, failed, "Done.\n"); } @@ -117,7 +125,10 @@ int run_tests(int timeout, int benchmark_output) { } -int run_test(const char* test, int timeout, int benchmark_output) { +int run_test(const char* test, + int timeout, + int benchmark_output, + int test_count) { char errmsg[1024] = "no error"; process_info_t processes[1024]; process_info_t *main_proc; @@ -243,7 +254,9 @@ int run_test(const char* test, int timeout, int benchmark_output) { /* Show error and output from processes if the test failed. */ if (status != 0 || task->show_output) { - if (status != 0) { + if (tap_output) { + LOGF("not ok %d - %s\n#", test_count, test); + } else if (status != 0) { LOGF("\n`%s` failed: %s\n", test, errmsg); } else { LOGF("\n"); @@ -267,7 +280,10 @@ int run_test(const char* test, int timeout, int benchmark_output) { break; } } - LOG("=============================================================\n"); + + if (!tap_output) { + LOG("=============================================================\n"); + } /* In benchmark mode show concise output from the main process. */ } else if (benchmark_output) { @@ -286,6 +302,8 @@ int run_test(const char* test, int timeout, int benchmark_output) { } break; } + } else if (tap_output) { + LOGF("ok %d - %s\n", test_count, test); } /* Clean up all process handles. */ diff --git a/test/runner.h b/test/runner.h index 3919007bb5..8be9e39fcc 100644 --- a/test/runner.h +++ b/test/runner.h @@ -102,7 +102,10 @@ int run_tests(int timeout, int benchmark_output); /* * Run a single test. Starts up any helpers. */ -int run_test(const char* test, int timeout, int benchmark_output); +int run_test(const char* test, + int timeout, + int benchmark_output, + int test_count); /* * Run a test part, i.e. the test or one of its helpers. @@ -156,4 +159,7 @@ void process_cleanup(process_info_t *p); /* Move the console cursor one line up and back to the first column. */ void rewind_cursor(void); +/* trigger output as tap */ +extern int tap_output; + #endif /* RUNNER_H_ */