Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
test: add tap output
Browse files Browse the repository at this point in the history
Given UV_TAP_OUTPUT being set, test result output should use TAP formatting
  • Loading branch information
tjfontaine authored and bnoordhuis committed Feb 22, 2013
1 parent c98083e commit bfe269b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion test/run-benchmarks.c
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion test/run-tests.c
Expand Up @@ -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);
}
20 changes: 15 additions & 5 deletions test/runner-unix.c
Expand Up @@ -42,13 +42,16 @@
/* 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)) {
fprintf(stderr, "Running the tests as root is not safe.\n");
exit(1);
}

tap_output = (tap != NULL && atoi(tap) > 0);

/* Disable stdio output buffering. */
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
Expand Down Expand Up @@ -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;
}
Expand Down
32 changes: 25 additions & 7 deletions test/runner.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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. */
Expand All @@ -87,37 +89,46 @@ 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");
}

return failed;
}


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;
Expand Down Expand Up @@ -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");
Expand All @@ -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) {
Expand All @@ -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. */
Expand Down
8 changes: 7 additions & 1 deletion test/runner.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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_ */

0 comments on commit bfe269b

Please sign in to comment.