has_tests.c 4.3 KB

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