]> git.ozlabs.org Git - ccan/blob - ccan/tap/test/run.c
Move modules to ccan/ tools to tools/
[ccan] / ccan / tap / test / run.c
1 /* We use the fact that pipes have a buffer greater than the size of
2  * any output, and change stdout and stderr to use that.
3  *
4  * Since we don't use libtap for output, this looks like one big test. */
5 #include "tap/tap.h"
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdarg.h>
9 #include <err.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <limits.h>
13 #include <stdbool.h>
14 #include <fnmatch.h>
15
16 /* We dup stderr to here. */
17 static int stderrfd;
18
19 /* Simple replacement for err() */
20 static void failmsg(const char *fmt, ...)
21 {
22         char buf[1024];
23         va_list ap;
24
25         /* Write into buffer. */
26         va_start(ap, fmt);
27         vsprintf(buf, fmt, ap);
28         va_end(ap);
29
30         write(stderrfd, "# ", 2);
31         write(stderrfd, buf, strlen(buf));
32         write(stderrfd, "\n", 1);
33         _exit(1);
34 }
35
36 static void expect(int fd, const char *pattern)
37 {
38         char buffer[PIPE_BUF+1];
39         int r;
40
41         r = read(fd, buffer, sizeof(buffer)-1);
42         if (r < 0)
43                 failmsg("reading from pipe");
44         buffer[r] = '\0';
45
46         if (fnmatch(pattern, buffer, 0) != 0)
47                 failmsg("Expected '%s' got '%s'", pattern, buffer);
48 }
49
50 int main(int argc, char *argv[])
51 {
52         int p[2];
53         int stdoutfd;
54
55         printf("1..1\n");
56         fflush(stdout);
57         stderrfd = dup(STDERR_FILENO);
58         if (stderrfd < 0)
59                 err(1, "dup of stderr failed");
60
61         stdoutfd = dup(STDOUT_FILENO);
62         if (stdoutfd < 0)
63                 err(1, "dup of stdout failed");
64
65         if (pipe(p) != 0)
66                 failmsg("pipe failed");
67
68         if (dup2(p[1], STDERR_FILENO) < 0 || dup2(p[1], STDOUT_FILENO) < 0)
69                 failmsg("Duplicating file descriptor");
70
71         plan_tests(10);
72         expect(p[0], "1..10\n");
73
74         ok(1, "msg1");
75         expect(p[0], "ok 1 - msg1\n");
76
77         ok(0, "msg2");
78         expect(p[0], "not ok 2 - msg2\n"
79                "#     Failed test (*tap/test/run.c:main() at line 77)\n");
80
81         ok1(true);
82         expect(p[0], "ok 3 - true\n");
83
84         ok1(false);
85         expect(p[0], "not ok 4 - false\n"
86                "#     Failed test (*tap/test/run.c:main() at line 84)\n");
87
88         pass("passed");
89         expect(p[0], "ok 5 - passed\n");
90
91         fail("failed");
92         expect(p[0], "not ok 6 - failed\n"
93                "#     Failed test (*tap/test/run.c:main() at line 91)\n");
94
95         skip(2, "skipping %s", "test");
96         expect(p[0], "ok 7 # skip skipping test\n"
97                "ok 8 # skip skipping test\n");
98
99         todo_start("todo");
100         ok1(false);
101         expect(p[0], "not ok 9 - false # TODO todo\n"
102                "#     Failed (TODO) test (*tap/test/run.c:main() at line 100)\n");
103         ok1(true);
104         expect(p[0], "ok 10 - true # TODO todo\n");
105         todo_end();
106
107         if (exit_status() != 3)
108                 failmsg("Expected exit status 3, not %i", exit_status());
109
110 #if 0
111         /* Manually run the atexit command. */
112         _cleanup();
113         expect(p[0], "# Looks like you failed 2 tests of 9.\n");
114 #endif
115
116         write(stdoutfd, "ok 1 - All passed\n", strlen("ok 1 - All passed\n"));
117         _exit(0);
118 }