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