examples_run.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <tools/tools.h>
  3. #include <ccan/foreach/foreach.h>
  4. #include <ccan/str/str.h>
  5. #include <ccan/cast/cast.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <stdint.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <ctype.h>
  13. #include <assert.h>
  14. static const char *can_run(struct manifest *m)
  15. {
  16. struct list_head *list;
  17. if (safe_mode)
  18. return "Safe mode enabled";
  19. foreach_ptr(list, &m->examples, &m->mangled_examples)
  20. if (!list_empty(list))
  21. return NULL;
  22. return "No examples";
  23. }
  24. /* Very dumb scanner, allocates %s-strings. */
  25. static bool scan_forv(const void *ctx,
  26. const char *input, const char *fmt, va_list *args)
  27. {
  28. va_list ap;
  29. bool ret;
  30. if (input[0] == '\0' || fmt[0] == '\0')
  31. return input[0] == fmt[0];
  32. va_copy(ap, *args);
  33. if (cisspace(fmt[0])) {
  34. /* One format space can swallow many input spaces */
  35. ret = false;
  36. while (cisspace(input[0])) {
  37. if (scan_forv(ctx, ++input, fmt+1, &ap)) {
  38. ret = true;
  39. break;
  40. }
  41. }
  42. } else if (fmt[0] != '%') {
  43. if (toupper(input[0]) != toupper(fmt[0]))
  44. ret = false;
  45. else
  46. ret = scan_forv(ctx, input+1, fmt+1, &ap);
  47. } else {
  48. char **p = va_arg(ap, char **);
  49. unsigned int len;
  50. ret = false;
  51. assert(fmt[1] == 's');
  52. for (len = 1; input[len-1]; len++) {
  53. ret = scan_forv(ctx, input + len, fmt+2, &ap);
  54. if (ret) {
  55. *p = tal_strndup(ctx, input, len);
  56. ret = true;
  57. break;
  58. }
  59. }
  60. }
  61. va_end(ap);
  62. return ret;
  63. }
  64. static bool scan_for(const void *ctx, const char *input, const char *fmt, ...)
  65. {
  66. bool ret;
  67. va_list ap;
  68. va_start(ap, fmt);
  69. ret = scan_forv(ctx, input, fmt, &ap);
  70. va_end(ap);
  71. return ret;
  72. }
  73. static char *find_expect(struct ccan_file *file,
  74. char **lines, char **input, bool *exact,
  75. unsigned *line)
  76. {
  77. char *expect;
  78. const char *fmt;
  79. for (; lines[*line]; (*line)++) {
  80. char *p = lines[*line] + strspn(lines[*line], " \t");
  81. if (!strstarts(p, "//"))
  82. continue;
  83. p += strspn(p, "/ ");
  84. foreach_ptr(fmt,
  85. "given '%s', outputs '%s'",
  86. "given '%s' outputs '%s'",
  87. "given \"%s\", outputs \"%s\"",
  88. "given \"%s\" outputs \"%s\"") {
  89. if (scan_for(file, p, fmt, input, &expect)) {
  90. *exact = true;
  91. return expect;
  92. }
  93. }
  94. foreach_ptr(fmt,
  95. "given '%s', output contains '%s'",
  96. "given '%s' output contains '%s'",
  97. "given \"%s\", output contains \"%s\"",
  98. "given \"%s\" output contains \"%s\"") {
  99. if (scan_for(file, p, fmt, input, &expect)) {
  100. *exact = false;
  101. return expect;
  102. }
  103. }
  104. foreach_ptr(fmt, "outputs '%s'", "outputs \"%s\"") {
  105. if (scan_for(file, p, fmt, &expect)) {
  106. *input = cast_const(char *, "");
  107. *exact = true;
  108. return expect;
  109. }
  110. }
  111. foreach_ptr(fmt,
  112. "given '%s', output contains '%s'",
  113. "given '%s' output contains '%s'",
  114. "given \"%s\", output contains \"%s\"",
  115. "given \"%s\" output contains \"%s\"") {
  116. if (scan_for(file, p, fmt, input, &expect)) {
  117. *exact = false;
  118. return expect;
  119. }
  120. }
  121. /* Unquoted versions... we can get this wrong! */
  122. foreach_ptr(fmt,
  123. "given %s, outputs '%s'",
  124. "given '%s', outputs %s",
  125. "given %s, outputs \"%s\"",
  126. "given \"%s\", outputs %s",
  127. "given %s, outputs %s",
  128. "given %s outputs '%s'",
  129. "given '%s' outputs %s",
  130. "given %s outputs \"%s\"",
  131. "given \"%s\" outputs %s",
  132. "given %s outputs %s") {
  133. if (scan_for(file, p, fmt, input, &expect)) {
  134. *exact = true;
  135. return expect;
  136. }
  137. }
  138. foreach_ptr(fmt,
  139. "given %s, output contains '%s'",
  140. "given '%s', output contains %s",
  141. "given %s, output contains \"%s\"",
  142. "given \"%s\", output contains %s",
  143. "given %s, output contains %s",
  144. "given %s output contains '%s'",
  145. "given '%s' output contains %s",
  146. "given %s output contains \"%s\"",
  147. "given \"%s\" output contains %s",
  148. "given %s output contains %s") {
  149. if (scan_for(file, p, fmt, input, &expect)) {
  150. *exact = false;
  151. return expect;
  152. }
  153. }
  154. foreach_ptr(fmt,
  155. "outputs '%s'",
  156. "outputs \"%s\"",
  157. "outputs %s") {
  158. if (scan_for(file, p, fmt, &expect)) {
  159. *input = cast_const(char *, "");
  160. *exact = true;
  161. return expect;
  162. }
  163. }
  164. foreach_ptr(fmt,
  165. "output contains '%s'",
  166. "output contains \"%s\"",
  167. "output contains %s") {
  168. if (scan_for(file, p, fmt, &expect)) {
  169. *input = cast_const(char *, "");
  170. *exact = false;
  171. return expect;
  172. }
  173. }
  174. }
  175. return NULL;
  176. }
  177. static char *trim(char *string)
  178. {
  179. while (strends(string, "\n"))
  180. string[strlen(string)-1] = '\0';
  181. return string;
  182. }
  183. static char *unexpected(struct ccan_file *i, const char *input,
  184. const char *expect, bool exact)
  185. {
  186. char *output, *cmd;
  187. bool ok;
  188. unsigned int default_time = default_timeout_ms;
  189. cmd = tal_fmt(i, "echo '%s' | %s %s",
  190. input, i->compiled[COMPILE_NORMAL], input);
  191. output = run_with_timeout(i, cmd, &ok, &default_time);
  192. if (!ok)
  193. return tal_fmt(i, "Exited with non-zero status\n");
  194. if (exact) {
  195. if (streq(output, expect) || streq(trim(output), expect))
  196. return NULL;
  197. } else {
  198. if (strstr(output, expect))
  199. return NULL;
  200. }
  201. return output;
  202. }
  203. static void run_examples(struct manifest *m,
  204. unsigned int *timeleft, struct score *score)
  205. {
  206. struct ccan_file *i;
  207. struct list_head *list;
  208. score->total = 0;
  209. score->pass = true;
  210. foreach_ptr(list, &m->examples, &m->mangled_examples) {
  211. list_for_each(list, i, list) {
  212. char **lines, *expect, *input, *output;
  213. unsigned int linenum = 0;
  214. bool exact;
  215. lines = get_ccan_file_lines(i);
  216. for (expect = find_expect(i, lines, &input, &exact,
  217. &linenum);
  218. expect;
  219. linenum++,
  220. expect = find_expect(i, lines, &input,
  221. &exact, &linenum)) {
  222. if (i->compiled[COMPILE_NORMAL] == NULL)
  223. continue;
  224. score->total++;
  225. output = unexpected(i, input, expect, exact);
  226. if (!output) {
  227. score->score++;
  228. continue;
  229. }
  230. score_file_error(score, i, linenum+1,
  231. "output '%s' didn't %s '%s'\n",
  232. output,
  233. exact ? "match" : "contain",
  234. expect);
  235. score->pass = false;
  236. }
  237. }
  238. }
  239. }
  240. /* FIXME: Test with reduced features, valgrind! */
  241. struct ccanlint examples_run = {
  242. .key = "examples_run",
  243. .name = "Module examples with expected output give that output",
  244. .check = run_examples,
  245. .can_run = can_run,
  246. .needs = "examples_compile"
  247. };
  248. REGISTER_TEST(examples_run);