1 #include <tools/ccanlint/ccanlint.h>
2 #include <tools/tools.h>
3 #include <ccan/talloc/talloc.h>
4 #include <ccan/str/str.h>
17 /* Note: we already test safe_mode in run_tests.c */
18 static const char *can_run_vg(struct manifest *m)
20 unsigned int timeleft = default_timeout_ms;
21 char *output = run_command(m, &timeleft, "valgrind -q true");
24 return talloc_asprintf(m, "No valgrind support: %s", output);
28 struct run_tests_result {
29 struct list_node list;
30 struct ccan_file *file;
34 static void *do_run_tests_vg(struct manifest *m,
36 unsigned int *timeleft)
38 struct list_head *list = talloc(m, struct list_head);
39 struct run_tests_result *res;
44 /* We run tests in the module directory, so any paths
45 * referenced can all be module-local. */
46 olddir = talloc_getcwd(m);
48 err(1, "Could not save cwd");
49 if (chdir(m->dir) != 0)
50 err(1, "Could not chdir to %s", m->dir);
54 list_for_each(&m->run_tests, i, list) {
55 run_tests_vg.total_score++;
56 cmdout = run_command(m, timeleft,
57 "valgrind -q %s", i->compiled);
59 res = talloc(list, struct run_tests_result);
61 res->output = talloc_steal(res, cmdout);
62 list_add_tail(list, &res->list);
66 list_for_each(&m->api_tests, i, list) {
67 run_tests_vg.total_score++;
68 cmdout = run_command(m, timeleft,
69 "valgrind -q %s", i->compiled);
71 res = talloc(list, struct run_tests_result);
73 res->output = talloc_steal(res, cmdout);
74 list_add_tail(list, &res->list);
78 if (list_empty(list)) {
83 if (chdir(olddir) != 0)
84 err(1, "Could not chdir to %s", olddir);
89 static unsigned int score_run_tests_vg(struct manifest *m, void *check_result)
91 struct list_head *list = check_result;
92 struct run_tests_result *i;
93 unsigned int score = run_tests_vg.total_score;
95 list_for_each(list, i, list)
100 static const char *describe_run_tests_vg(struct manifest *m,
103 struct list_head *list = check_result;
104 char *descrip = talloc_strdup(check_result, "Running tests under valgrind failed:\n");
105 struct run_tests_result *i;
107 list_for_each(list, i, list)
108 descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
109 i->file->name, i->output);
113 /* Gcc's warn_unused_result is fascist bullshit. */
114 #define doesnt_matter()
116 static void run_under_debugger_vg(struct manifest *m, void *check_result)
118 struct list_head *list = check_result;
119 struct run_tests_result *first;
122 if (!ask("Should I run the first failing test under the debugger?"))
125 first = list_top(list, struct run_tests_result, list);
126 command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
127 first->file->compiled);
132 struct ccanlint run_tests_vg = {
133 .key = "valgrind-tests",
134 .name = "Module's run and api tests succeed under valgrind",
135 .score = score_run_tests_vg,
136 .check = do_run_tests_vg,
137 .describe = describe_run_tests_vg,
138 .can_run = can_run_vg,
139 .handle = run_under_debugger_vg
142 REGISTER_TEST(run_tests_vg, &run_tests, NULL);