has_tests.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "ccanlint.h"
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <limits.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <err.h>
  11. #include <ccan/talloc/talloc.h>
  12. static char test_is_not_dir[] = "test is not a directory";
  13. static void *check_has_tests(struct manifest *m)
  14. {
  15. struct stat st;
  16. if (lstat("test", &st) != 0) {
  17. if (errno != ENOENT)
  18. err(1, "statting test/");
  19. return "You have no test directory";
  20. }
  21. if (!S_ISDIR(st.st_mode))
  22. return test_is_not_dir;
  23. if (list_empty(&m->run_tests) && list_empty(&m->compile_ok_tests)) {
  24. if (list_empty(&m->compile_fail_tests))
  25. return "You have no tests in the test directory";
  26. else
  27. return "You have no positive tests in the test directory";
  28. }
  29. return NULL;
  30. }
  31. static const char *describe_has_tests(struct manifest *m, void *check_result)
  32. {
  33. return talloc_asprintf(m, "%s\n\n"
  34. "CCAN modules have a directory called test/ which contains tests.\n"
  35. "There are three kinds of tests: run, compile_ok and compile_fail:\n"
  36. "you can tell which type of test a C file is by its name, eg 'run.c'\n"
  37. "and 'run-simple.c' are both run tests.\n\n"
  38. "The simplest kind of test is a run test, which must compile with no\n"
  39. "warnings, and then run: it is expected to use libtap to report its\n"
  40. "results in a simple and portable format.\n"
  41. "compile_ok tests are a subset of run tests: they must compile and\n"
  42. "link, but aren't run.\n"
  43. "compile_fail tests are tests which should fail to compile (or emit\n"
  44. "warnings) or link when FAIL is defined, but should compile and link\n"
  45. "when it's not defined: this helps ensure unrelated errors don't make\n"
  46. "compilation fail.\n\n"
  47. "Note that the tests are not linked against the files in the\n"
  48. "above: you should directly #include those C files you want. This\n"
  49. "allows access to static functions and use special effects inside\n"
  50. "test files\n", (char *)check_result);
  51. }
  52. static void handle_no_tests(struct manifest *m, void *check_result)
  53. {
  54. FILE *run;
  55. struct ccan_file *i;
  56. if (check_result == test_is_not_dir)
  57. return;
  58. if (!ask("Should I create a template test/run.c file for you?"))
  59. return;
  60. if (mkdir("test", 0600) != 0) {
  61. if (errno != EEXIST)
  62. err(1, "Creating test/ directory");
  63. }
  64. run = fopen("test/run.c", "w");
  65. if (!run)
  66. err(1, "Trying to create a test/run.c");
  67. fputs("/* Include the main header first, to test it works */\n", run);
  68. fprintf(run, "#include \"%s/%s.h\"\n", m->basename, m->basename);
  69. fputs("/* Include the C files directly. */\n", run);
  70. list_for_each(&m->c_files, i, list)
  71. fprintf(run, "#include \"%s/%s\"\n", m->basename, i->name);
  72. fputs("#include \"tap/tap.h\"\n", run);
  73. fputs("\n", run);
  74. fputs("int main(int argc, char *argv[])\n", run);
  75. fputs("{\n", run);
  76. fputs("\t/* This is how many tests you plan to run\n", run);
  77. fputs("\tplan_tests(3);\n", run);
  78. fputs("\n", run);
  79. fputs("\t/* Simple thing we expect to succeed */\n", run);
  80. fputs("\tok1(some_test())\n", run);
  81. fputs("\t/* Same, with an explicit description of the test. */\n", run);
  82. fputs("\tok(some_test(), \"%s with no args should return 1\", \"some_test\")\n", run);
  83. fputs("\t/* How to print out messages for debugging. */\n", run);
  84. fputs("\tdiag(\"Address of some_test is %p\", &some_test)\n", run);
  85. fputs("\t/* Conditional tests must be explicitly skipped. */\n", run);
  86. fputs("#if HAVE_SOME_FEATURE\n", run);
  87. fputs("\tok1(test_some_feature())\n", run);
  88. fputs("#else\n", run);
  89. fputs("\tskip(1, \"Don\'t have SOME_FEATURE\")\n", run);
  90. fputs("#endif\n", run);
  91. fputs("\n", run);
  92. fputs("\t/* This exits depending on whether all tests passed */\n", run);
  93. fputs("\return exit_status()\n", run);
  94. fclose(run);
  95. }
  96. struct ccanlint has_tests = {
  97. .name = "No tests",
  98. .check = check_has_tests,
  99. .describe = describe_has_tests,
  100. .handle = handle_no_tests,
  101. };