Rename libustcmd to libustctl
[ust.git] / tests / tap.c
CommitLineData
48beefc9
NC
1/* Copyright (C) 2010 Nils Carlson
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17#include <pthread.h>
18#include <stdarg.h>
19#include <stdio.h>
20#include <unistd.h>
21
22static int tap_planned = -1;
23static int tap_count = 1;
24static int tap_passed = 0;
25
26static pthread_t stdout_thread;
27static int pipefd[2];
28static FILE *pipe_r_file;
29static FILE *normal_stdout;
30
31static void *_tap_comment_stdout(void *_unused)
32{
33 char line[4096];
34
35 while (fgets(&line[0], 4096, pipe_r_file)) {
36 if (strncmp(line, "_TAP", 4)) {
37 fprintf(normal_stdout, "# %s", line);
38 } else {
39 fprintf(normal_stdout, &line[4]);
40 }
41 }
42 pthread_exit(0);
43}
44
45static void tap_comment_stdout(void)
46{
47 int stdout_fileno, new_stdout, result, fd;
48
49 if (pipe(pipefd) < 0) {
50 perror("# Failed to open pipe");
51 return;
52 }
53
54 pipe_r_file = fdopen(pipefd[0], "r");
55 if (!pipe_r_file) {
56 perror("# Couldn't create a FILE from the pipe");
57 goto close_pipe;
58 }
59
60 stdout_fileno = fileno(stdout);
61 if (stdout_fileno < 0) {
62 perror("# Couldn't get fileno for stdout!?");
63 goto close_pipe_r_file;
64 }
65
66 new_stdout = dup(stdout_fileno);
67 if (new_stdout < 0) {
68 perror("# Couldn't dup stdout");
69 goto close_pipe_r_file;
70 }
71
72 normal_stdout = fdopen(new_stdout, "w");
73 if (!normal_stdout) {
74 perror("# Could create a FILE from new_stdout");
75 goto close_dup_stdout;
76 }
77
78 result = pthread_create(&stdout_thread, NULL,
79 _tap_comment_stdout, NULL);
80 if (result < 0) {
81 perror("# Couldn't start stdout_thread");
82 goto close_normal_stdout;
83 }
84
85 fclose(stdout);
86 fclose(stderr);
87
88 fd = dup(pipefd[1]);
89 if (fd != STDOUT_FILENO) {
90 fprintf(stderr, "# Failed to open a new stdout!\n");
91 goto close_normal_stdout;
92 }
93
94 stdout = fdopen(fd, "w");
95 if (!stdout) {
96 perror("Couldn't open a new stdout");
97 goto close_fd;
98 }
99
100 fd = dup(pipefd[1]);
101 if (fd != STDERR_FILENO) {
102 fprintf(stderr, "# Failed to open a new stderr!\n");
103 goto close_fd;
104 }
105
106 stderr = fdopen(fd, "w");
107 if (!stderr) {
108 perror("Couldn't open a new stderr");
109 goto close_fd;
110 }
111
112 setlinebuf(stdout);
113 setlinebuf(stderr);
114 setlinebuf(pipe_r_file);
115
116 return;
117
118close_fd:
119 close(fd);
120
121close_normal_stdout:
122 fclose(normal_stdout);
123
124close_dup_stdout:
125 close(new_stdout);
126
127close_pipe_r_file:
128 fclose(pipe_r_file);
129
130close_pipe:
131 close(pipefd[0]);
132 close(pipefd[1]);
133
134 return;
135}
136
137void tap_plan(int count)
138{
139 printf("1..%d\n", count);
140
141 tap_count = 1;
142 tap_planned = count;
143
144 tap_comment_stdout();
145
146}
147
148int tap_status(void)
149{
150 if (tap_passed == tap_planned) {
151 return 0;
152 } else {
153 return 1;
154 }
155}
156
157void tap_ok(int bool, const char *format, ...)
158{
159 va_list args;
160 char *ok_string = "_TAPok";
161 char *not_ok_string = "_TAPnot ok";
162 char string[4000];
163
164 va_start(args, format);
165 vsprintf(string, format, args);
166 va_end(args);
167
168 printf("%s %d - %s\n", bool ? ok_string : not_ok_string,
169 tap_count++, string);
170
171 if (bool)
172 tap_passed++;
173}
This page took 0.027394 seconds and 4 git commands to generate.