tap.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*-
  2. * Copyright (c) 2004 Nik Clayton
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #include <ctype.h>
  28. #include <stdarg.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include "tap.h"
  33. static int no_plan = 0;
  34. static int skip_all = 0;
  35. static int have_plan = 0;
  36. static unsigned int test_count = 0; /* Number of tests that have been run */
  37. static unsigned int e_tests = 0; /* Expected number of tests to run */
  38. static unsigned int failures = 0; /* Number of tests that failed */
  39. static char *todo_msg = NULL;
  40. static const char *todo_msg_fixed = "libtap malloc issue";
  41. static int todo = 0;
  42. static int test_died = 0;
  43. static int test_pid;
  44. /* Encapsulate the pthread code in a conditional. In the absence of
  45. libpthread the code does nothing */
  46. #if HAVE_LIBPTHREAD
  47. #include <pthread.h>
  48. static pthread_mutex_t M = PTHREAD_MUTEX_INITIALIZER;
  49. # define LOCK pthread_mutex_lock(&M)
  50. # define UNLOCK pthread_mutex_unlock(&M)
  51. #else
  52. # define LOCK
  53. # define UNLOCK
  54. #endif
  55. static void
  56. _expected_tests(unsigned int tests)
  57. {
  58. printf("1..%d\n", tests);
  59. e_tests = tests;
  60. }
  61. static void
  62. diagv(const char *fmt, va_list ap)
  63. {
  64. fputs("# ", stdout);
  65. vfprintf(stdout, fmt, ap);
  66. fputs("\n", stdout);
  67. }
  68. static void
  69. _diag(const char *fmt, ...)
  70. {
  71. va_list ap;
  72. va_start(ap, fmt);
  73. diagv(fmt, ap);
  74. va_end(ap);
  75. }
  76. /*
  77. * Generate a test result.
  78. *
  79. * ok -- boolean, indicates whether or not the test passed.
  80. * test_name -- the name of the test, may be NULL
  81. * test_comment -- a comment to print afterwards, may be NULL
  82. */
  83. unsigned int
  84. _gen_result(int ok, const char *func, const char *file, unsigned int line,
  85. const char *test_name, ...)
  86. {
  87. va_list ap;
  88. char *local_test_name = NULL;
  89. char *c;
  90. int name_is_digits;
  91. LOCK;
  92. test_count++;
  93. /* Start by taking the test name and performing any printf()
  94. expansions on it */
  95. if(test_name != NULL) {
  96. va_start(ap, test_name);
  97. if (vasprintf(&local_test_name, test_name, ap) < 0)
  98. local_test_name = NULL;
  99. va_end(ap);
  100. /* Make sure the test name contains more than digits
  101. and spaces. Emit an error message and exit if it
  102. does */
  103. if(local_test_name) {
  104. name_is_digits = 1;
  105. for(c = local_test_name; *c != '\0'; c++) {
  106. if(!isdigit(*c) && !isspace(*c)) {
  107. name_is_digits = 0;
  108. break;
  109. }
  110. }
  111. if(name_is_digits) {
  112. _diag(" You named your test '%s'. You shouldn't use numbers for your test names.", local_test_name);
  113. _diag(" Very confusing.");
  114. }
  115. }
  116. }
  117. if(!ok) {
  118. printf("not ");
  119. failures++;
  120. }
  121. printf("ok %d", test_count);
  122. if(test_name != NULL) {
  123. printf(" - ");
  124. /* Print the test name, escaping any '#' characters it
  125. might contain */
  126. if(local_test_name != NULL) {
  127. flockfile(stdout);
  128. for(c = local_test_name; *c != '\0'; c++) {
  129. if(*c == '#')
  130. fputc('\\', stdout);
  131. fputc((int)*c, stdout);
  132. }
  133. funlockfile(stdout);
  134. } else { /* vasprintf() failed, use a fixed message */
  135. printf("%s", todo_msg_fixed);
  136. }
  137. }
  138. /* If we're in a todo_start() block then flag the test as being
  139. TODO. todo_msg should contain the message to print at this
  140. point. If it's NULL then asprintf() failed, and we should
  141. use the fixed message.
  142. This is not counted as a failure, so decrement the counter if
  143. the test failed. */
  144. if(todo) {
  145. printf(" # TODO %s", todo_msg ? todo_msg : todo_msg_fixed);
  146. if(!ok)
  147. failures--;
  148. }
  149. printf("\n");
  150. if(!ok)
  151. _diag(" Failed %stest (%s:%s() at line %d)",
  152. todo ? "(TODO) " : "", file, func, line);
  153. free(local_test_name);
  154. UNLOCK;
  155. if (!ok && tap_fail_callback)
  156. tap_fail_callback();
  157. /* We only care (when testing) that ok is positive, but here we
  158. specifically only want to return 1 or 0 */
  159. return ok ? 1 : 0;
  160. }
  161. /*
  162. * Cleanup at the end of the run, produce any final output that might be
  163. * required.
  164. */
  165. static void
  166. _cleanup(void)
  167. {
  168. /* If we forked, don't do cleanup in child! */
  169. if (getpid() != test_pid)
  170. return;
  171. LOCK;
  172. /* If plan_no_plan() wasn't called, and we don't have a plan,
  173. and we're not skipping everything, then something happened
  174. before we could produce any output */
  175. if(!no_plan && !have_plan && !skip_all) {
  176. _diag("Looks like your test died before it could output anything.");
  177. UNLOCK;
  178. return;
  179. }
  180. if(test_died) {
  181. _diag("Looks like your test died just after %d.", test_count);
  182. UNLOCK;
  183. return;
  184. }
  185. /* No plan provided, but now we know how many tests were run, and can
  186. print the header at the end */
  187. if(!skip_all && (no_plan || !have_plan)) {
  188. printf("1..%d\n", test_count);
  189. }
  190. if((have_plan && !no_plan) && e_tests < test_count) {
  191. _diag("Looks like you planned %d tests but ran %d extra.",
  192. e_tests, test_count - e_tests);
  193. UNLOCK;
  194. return;
  195. }
  196. if((have_plan || !no_plan) && e_tests > test_count) {
  197. _diag("Looks like you planned %d tests but only ran %d.",
  198. e_tests, test_count);
  199. if(failures) {
  200. _diag("Looks like you failed %d tests of %d run.",
  201. failures, test_count);
  202. }
  203. UNLOCK;
  204. return;
  205. }
  206. if(failures)
  207. _diag("Looks like you failed %d tests of %d.",
  208. failures, test_count);
  209. UNLOCK;
  210. }
  211. /*
  212. * Initialise the TAP library. Will only do so once, however many times it's
  213. * called.
  214. */
  215. static void
  216. _tap_init(void)
  217. {
  218. static int run_once = 0;
  219. if(!run_once) {
  220. test_pid = getpid();
  221. atexit(_cleanup);
  222. /* stdout needs to be unbuffered so that the output appears
  223. in the same place relative to stderr output as it does
  224. with Test::Harness */
  225. // setbuf(stdout, 0);
  226. run_once = 1;
  227. }
  228. }
  229. /*
  230. * Note that there's no plan.
  231. */
  232. void
  233. plan_no_plan(void)
  234. {
  235. LOCK;
  236. _tap_init();
  237. if(have_plan != 0) {
  238. fprintf(stderr, "You tried to plan twice!\n");
  239. test_died = 1;
  240. UNLOCK;
  241. exit(255);
  242. }
  243. have_plan = 1;
  244. no_plan = 1;
  245. UNLOCK;
  246. }
  247. /*
  248. * Note that the plan is to skip all tests
  249. */
  250. void
  251. plan_skip_all(const char *reason)
  252. {
  253. LOCK;
  254. _tap_init();
  255. skip_all = 1;
  256. printf("1..0");
  257. if(reason != NULL)
  258. printf(" # Skip %s", reason);
  259. printf("\n");
  260. UNLOCK;
  261. }
  262. /*
  263. * Note the number of tests that will be run.
  264. */
  265. void
  266. plan_tests(unsigned int tests)
  267. {
  268. LOCK;
  269. _tap_init();
  270. if(have_plan != 0) {
  271. fprintf(stderr, "You tried to plan twice!\n");
  272. test_died = 1;
  273. UNLOCK;
  274. exit(255);
  275. }
  276. if(tests == 0) {
  277. fprintf(stderr, "You said to run 0 tests! You've got to run something.\n");
  278. test_died = 1;
  279. UNLOCK;
  280. exit(255);
  281. }
  282. have_plan = 1;
  283. _expected_tests(tests);
  284. UNLOCK;
  285. }
  286. void
  287. diag(const char *fmt, ...)
  288. {
  289. va_list ap;
  290. LOCK;
  291. va_start(ap, fmt);
  292. diagv(fmt, ap);
  293. va_end(ap);
  294. UNLOCK;
  295. }
  296. void
  297. skip(unsigned int n, const char *fmt, ...)
  298. {
  299. va_list ap;
  300. char *skip_msg;
  301. LOCK;
  302. va_start(ap, fmt);
  303. if (vasprintf(&skip_msg, fmt, ap) < 0)
  304. skip_msg = NULL;
  305. va_end(ap);
  306. while(n-- > 0) {
  307. test_count++;
  308. printf("ok %d # skip %s\n", test_count,
  309. skip_msg != NULL ?
  310. skip_msg : "libtap():malloc() failed");
  311. }
  312. free(skip_msg);
  313. UNLOCK;
  314. }
  315. void
  316. todo_start(const char *fmt, ...)
  317. {
  318. va_list ap;
  319. LOCK;
  320. va_start(ap, fmt);
  321. if (vasprintf(&todo_msg, fmt, ap) < 0)
  322. todo_msg = NULL;
  323. va_end(ap);
  324. todo = 1;
  325. UNLOCK;
  326. }
  327. void
  328. todo_end(void)
  329. {
  330. LOCK;
  331. todo = 0;
  332. free(todo_msg);
  333. UNLOCK;
  334. }
  335. int
  336. exit_status(void)
  337. {
  338. int r;
  339. LOCK;
  340. /* If there's no plan, just return the number of failures */
  341. if(no_plan || !have_plan) {
  342. UNLOCK;
  343. return failures;
  344. }
  345. /* Ran too many tests? Return the number of tests that were run
  346. that shouldn't have been */
  347. if(e_tests < test_count) {
  348. r = test_count - e_tests;
  349. UNLOCK;
  350. return r;
  351. }
  352. /* Return the number of tests that failed + the number of tests
  353. that weren't run */
  354. r = failures + e_tests - test_count;
  355. UNLOCK;
  356. return r;
  357. }