run.c 2.6 KB

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