]> git.ozlabs.org Git - ccan/blob - tap/test/run.c
First primitive cut of ccanlint
[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         fflush(stdout);
56         stderrfd = dup(STDERR_FILENO);
57         if (stderrfd < 0)
58                 err(1, "dup of stderr failed");
59
60         stdoutfd = dup(STDOUT_FILENO);
61         if (stdoutfd < 0)
62                 err(1, "dup of stdout failed");
63
64         if (pipe(p) != 0)
65                 failmsg("pipe failed");
66
67         if (dup2(p[1], STDERR_FILENO) < 0 || dup2(p[1], STDOUT_FILENO) < 0)
68                 failmsg("Duplicating file descriptor");
69
70         plan_tests(10);
71         expect(p[0], "1..10\n");
72
73         ok(1, "msg1");
74         expect(p[0], "ok 1 - msg1\n");
75
76         ok(0, "msg2");
77         expect(p[0], "not ok 2 - msg2\n"
78                "#     Failed test (tap/test/run.c:main() at line 76)\n");
79
80         ok1(true);
81         expect(p[0], "ok 3 - true\n");
82
83         ok1(false);
84         expect(p[0], "not ok 4 - false\n"
85                "#     Failed test (tap/test/run.c:main() at line 83)\n");
86
87         pass("passed");
88         expect(p[0], "ok 5 - passed\n");
89
90         fail("failed");
91         expect(p[0], "not ok 6 - failed\n"
92                "#     Failed test (tap/test/run.c:main() at line 90)\n");
93
94         skip(2, "skipping %s", "test");
95         expect(p[0], "ok 7 # skip skipping test\n"
96                "ok 8 # skip skipping test\n");
97
98         todo_start("todo");
99         ok1(false);
100         expect(p[0], "not ok 9 - false # TODO todo\n"
101                "#     Failed (TODO) test (tap/test/run.c:main() at line 99)\n");
102         ok1(true);
103         expect(p[0], "ok 10 - true # TODO todo\n");
104         todo_end();
105
106         if (exit_status() != 3)
107                 failmsg("Expected exit status 3, not %i", exit_status());
108
109 #if 0
110         /* Manually run the atexit command. */
111         _cleanup();
112         expect(p[0], "# Looks like you failed 2 tests of 9.\n");
113 #endif
114
115         write(stdoutfd, "ok 1 - All passed\n", strlen("ok 1 - All passed\n"));
116         _exit(0);
117 }