tests_exist.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <ccan/tal/str/str.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <limits.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <err.h>
  12. static void check_tests_exist(struct manifest *m,
  13. unsigned int *timeleft, struct score *score);
  14. static struct ccanlint tests_exist = {
  15. .key = "tests_exist",
  16. .name = "Module has test directory with tests in it",
  17. .check = check_tests_exist,
  18. .needs = "info_exists"
  19. };
  20. REGISTER_TEST(tests_exist);
  21. static void handle_no_tests(struct manifest *m, struct score *score)
  22. {
  23. FILE *run;
  24. struct ccan_file *i;
  25. char *test_dir = tal_fmt(m, "%s/test", m->dir), *run_file;
  26. printf(
  27. "CCAN modules have a directory called test/ which contains tests.\n"
  28. "There are four kinds of tests: api, run, compile_ok and compile_fail:\n"
  29. "you can tell which type of test a C file is by its name, eg 'run.c'\n"
  30. "and 'run-simple.c' are both run tests.\n\n"
  31. "The simplest kind of test is a run test, which must compile with no\n"
  32. "warnings, and then run: it is expected to use ccan/tap to report its\n"
  33. "results in a simple and portable format. It should #include the C\n"
  34. "files from the module directly (so it can probe the internals): the\n"
  35. "module will not be linked in. The test will be run in a temporary\n"
  36. "directory, with the test directory symlinked under test/.\n\n"
  37. "api tests are just like a run test, except it is a guarantee of API\n"
  38. "stability: this test should pass on all future versions of the\n"
  39. "module. They *are* linked to the module, since they should only\n"
  40. "test the API, not the internal state.\n\n"
  41. "compile_ok tests are a subset of run tests: they must compile and\n"
  42. "link, but aren't run.\n\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 only API tests are linked against the files in the module!\n"
  48. );
  49. if (!ask("Should I create a template test/run.c file for you?"))
  50. return;
  51. if (mkdir(test_dir, 0700) != 0) {
  52. if (errno != EEXIST)
  53. err(1, "Creating test/ directory");
  54. }
  55. run_file = tal_fmt(test_dir, "%s/run.c", test_dir);
  56. run = fopen(run_file, "w");
  57. if (!run)
  58. err(1, "Trying to create a test/run.c");
  59. fprintf(run, "#include <ccan/%s/%s.h>\n", m->modname, m->basename);
  60. if (!list_empty(&m->c_files)) {
  61. fputs("/* Include the C files directly. */\n", run);
  62. list_for_each(&m->c_files, i, list)
  63. fprintf(run, "#include <ccan/%s/%s>\n",
  64. m->modname, i->name);
  65. }
  66. fprintf(run, "%s",
  67. "#include <ccan/tap/tap.h>\n\n"
  68. "int main(void)\n"
  69. "{\n"
  70. " /* This is how many tests you plan to run */\n"
  71. " plan_tests(3);\n"
  72. "\n"
  73. " /* Simple thing we expect to succeed */\n"
  74. " ok1(some_test())\n"
  75. " /* Same, with an explicit description of the test. */\n"
  76. " ok(some_test(), \"%s with no args should return 1\", \"some_test\")\n"
  77. " /* How to print out messages for debugging. */\n"
  78. " diag(\"Address of some_test is %p\", &some_test)\n"
  79. " /* Conditional tests must be explicitly skipped. */\n"
  80. "#if HAVE_SOME_FEATURE\n"
  81. " ok1(test_some_feature())\n"
  82. "#else\n"
  83. " skip(1, \"Don\'t have SOME_FEATURE\")\n"
  84. "#endif\n"
  85. "\n"
  86. " /* This exits depending on whether all tests passed */\n"
  87. " return exit_status();\n"
  88. "}\n");
  89. fclose(run);
  90. }
  91. static void check_tests_exist(struct manifest *m,
  92. unsigned int *timeleft, struct score *score)
  93. {
  94. struct stat st;
  95. char *test_dir = tal_fmt(m, "%s/test", m->dir);
  96. if (lstat(test_dir, &st) != 0) {
  97. score->error = tal_strdup(score, "No test directory");
  98. if (errno != ENOENT)
  99. err(1, "statting %s", test_dir);
  100. tests_exist.handle = handle_no_tests;
  101. /* We "pass" this. */
  102. score->pass = true;
  103. return;
  104. }
  105. if (!S_ISDIR(st.st_mode)) {
  106. score->error = tal_strdup(score, "test is not a directory");
  107. return;
  108. }
  109. if (list_empty(&m->api_tests)
  110. && list_empty(&m->run_tests)
  111. && list_empty(&m->compile_ok_tests)
  112. && list_empty(&m->compile_fail_tests)) {
  113. score->error = tal_strdup(score, "No tests in test directory");
  114. tests_exist.handle = handle_no_tests;
  115. return;
  116. }
  117. score->pass = true;
  118. score->score = score->total;
  119. }