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, unsigned int *timeleft)
36 struct list_head *list = talloc(m, struct list_head);
37 struct run_tests_result *res;
42 /* We run tests in the module directory, so any paths
43 * referenced can all be module-local. */
44 olddir = talloc_getcwd(m);
46 err(1, "Could not save cwd");
47 if (chdir(m->dir) != 0)
48 err(1, "Could not chdir to %s", m->dir);
52 list_for_each(&m->run_tests, i, list) {
53 run_tests_vg.total_score++;
54 cmdout = run_command(m, timeleft,
55 "valgrind -q %s", i->compiled);
57 res = talloc(list, struct run_tests_result);
59 res->output = talloc_steal(res, cmdout);
60 list_add_tail(list, &res->list);
64 list_for_each(&m->api_tests, i, list) {
65 run_tests_vg.total_score++;
66 cmdout = run_command(m, timeleft,
67 "valgrind -q %s", i->compiled);
69 res = talloc(list, struct run_tests_result);
71 res->output = talloc_steal(res, cmdout);
72 list_add_tail(list, &res->list);
76 if (list_empty(list)) {
81 if (chdir(olddir) != 0)
82 err(1, "Could not chdir to %s", olddir);
87 static unsigned int score_run_tests_vg(struct manifest *m, void *check_result)
89 struct list_head *list = check_result;
90 struct run_tests_result *i;
91 unsigned int score = run_tests_vg.total_score;
93 list_for_each(list, i, list)
98 static const char *describe_run_tests_vg(struct manifest *m,
101 struct list_head *list = check_result;
102 char *descrip = talloc_strdup(check_result, "Running tests under valgrind failed:\n");
103 struct run_tests_result *i;
105 list_for_each(list, i, list)
106 descrip = talloc_asprintf_append(descrip, "Running %s:\n%s",
107 i->file->name, i->output);
111 /* Gcc's warn_unused_result is fascist bullshit. */
112 #define doesnt_matter()
114 static void run_under_debugger_vg(struct manifest *m, void *check_result)
116 struct list_head *list = check_result;
117 struct run_tests_result *first;
120 if (!ask("Should I run the first failing test under the debugger?"))
123 first = list_top(list, struct run_tests_result, list);
124 command = talloc_asprintf(m, "valgrind --db-attach=yes %s",
125 first->file->compiled);
130 struct ccanlint run_tests_vg = {
132 .name = "Module's run and api tests succeed under valgrind",
133 .score = score_run_tests_vg,
134 .check = do_run_tests_vg,
135 .describe = describe_run_tests_vg,
136 .can_run = can_run_vg,
137 .handle = run_under_debugger_vg
140 REGISTER_TEST(run_tests_vg, &run_tests, NULL);