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.
4 * Since we don't use libtap for output, this looks like one big test. */
5 #include <ccan/tap/tap.h>
6 #include <ccan/tap/tap.c>
16 /* We dup stderr to here. */
19 /* write_all inlined here to avoid circular dependency. */
20 static void write_all(int fd, const void *data, size_t size)
25 done = write(fd, data, size);
28 data = (const char *)data + done;
33 /* Simple replacement for err() */
34 static void failmsg(const char *fmt, ...)
39 /* Write into buffer. */
41 vsprintf(buf, fmt, ap);
44 write_all(stderrfd, "# ", 2);
45 write_all(stderrfd, buf, strlen(buf));
46 write_all(stderrfd, "\n", 1);
50 static void expect(int fd, const char *pattern)
52 char buffer[PIPE_BUF+1];
55 r = read(fd, buffer, sizeof(buffer)-1);
57 failmsg("reading from pipe");
60 if (fnmatch(pattern, buffer, 0) != 0)
61 failmsg("Expected '%s' got '%s'", pattern, buffer);
64 int main(int argc, char *argv[])
71 stderrfd = dup(STDERR_FILENO);
73 err(1, "dup of stderr failed");
75 stdoutfd = dup(STDOUT_FILENO);
77 err(1, "dup of stdout failed");
80 failmsg("pipe failed");
82 if (dup2(p[1], STDERR_FILENO) < 0 || dup2(p[1], STDOUT_FILENO) < 0)
83 failmsg("Duplicating file descriptor");
86 expect(p[0], "1..10\n");
89 expect(p[0], "ok 1 - msg1\n");
92 expect(p[0], "not ok 2 - msg2\n"
93 "# Failed test (*test/run.c:main() at line 91)\n");
96 expect(p[0], "ok 3 - true\n");
99 expect(p[0], "not ok 4 - false\n"
100 "# Failed test (*test/run.c:main() at line 98)\n");
103 expect(p[0], "ok 5 - passed\n");
106 expect(p[0], "not ok 6 - failed\n"
107 "# Failed test (*test/run.c:main() at line 105)\n");
109 skip(2, "skipping %s", "test");
110 expect(p[0], "ok 7 # skip skipping test\n"
111 "ok 8 # skip skipping test\n");
115 expect(p[0], "not ok 9 - false # TODO todo\n"
116 "# Failed (TODO) test (*test/run.c:main() at line 114)\n");
118 expect(p[0], "ok 10 - true # TODO todo\n");
121 if (exit_status() != 3)
122 failmsg("Expected exit status 3, not %i", exit_status());
125 /* Manually run the atexit command. */
127 expect(p[0], "# Looks like you failed 2 tests of 9.\n");
130 write_all(stdoutfd, "ok 1 - All passed\n",
131 strlen("ok 1 - All passed\n"));