]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/tests/tests_pass_valgrind.c
ccanlint: tests_pass_valgrind_noleaks: handle FAIL option on tests.
[ccan] / tools / ccanlint / tests / tests_pass_valgrind.c
index 9622b300a20af1afe1023b33a980d42ccf3951fe..2a227c88743ead915aad21db6fd82f35e1bbb352 100644 (file)
@@ -18,6 +18,7 @@
 #include <ctype.h>
 
 REGISTER_TEST(tests_pass_valgrind);
+REGISTER_TEST(tests_pass_valgrind_noleaks);
 
 /* Note: we already test safe_mode in run_tests.c */
 static const char *can_run_vg(struct manifest *m)
@@ -133,6 +134,19 @@ static char *analyze_output(const char *output, char **errs)
        return leaks;
 }
 
+static const char *concat(struct score *score, char *bits[])
+{
+       unsigned int i;
+       char *ret = talloc_strdup(score, "");
+
+       for (i = 0; bits[i]; i++) {
+               if (i)
+                       ret = talloc_append_string(ret, " ");
+               ret = talloc_append_string(ret, bits[i]);
+       }
+       return ret;
+}
+
 /* FIXME: Run examples, too! */
 static void do_run_tests_vg(struct manifest *m,
                             bool keep,
@@ -145,11 +159,19 @@ static void do_run_tests_vg(struct manifest *m,
 
        /* This is slow, so we run once but grab leak info. */
        score->total = 0;
+       score->pass = true;
        foreach_ptr(list, &m->run_tests, &m->api_tests) {
                list_for_each(list, i, list) {
                        char *output, *err, *log;
                        bool pass;
+                       const char *options;
+
                        score->total++;
+                       options = concat(score,
+                                        per_file_options(&tests_pass_valgrind,
+                                                         i));
+                       if (streq(options, "FAIL"))
+                               continue;
 
                        /* FIXME: Valgrind's output sucks.  XML is unreadable by
                         * humans *and* doesn't support children reporting. */
@@ -164,8 +186,7 @@ static void do_run_tests_vg(struct manifest *m,
                                           " --leak-check=full"
                                           " --log-fd=3 %s %s"
                                           " 3> %s",
-                                          tests_pass_valgrind.options ?
-                                          tests_pass_valgrind.options : "",
+                                          options,
                                           i->compiled, log);
                        output = grab_file(i, log, NULL);
                        /* No valgrind errors?  Expect it to pass... */
@@ -181,15 +202,13 @@ static void do_run_tests_vg(struct manifest *m,
                        } else {
                                i->leak_info = analyze_output(output, &err);
                        }
-                       if (err)
+                       if (err) {
                                score_file_error(score, i, 0, "%s", err);
-                       else
+                               score->pass = false;
+                       } else
                                score->score++;
                }
        }
-
-       if (score->score == score->total)
-               score->pass = true;
 }
 
 static void do_leakcheck_vg(struct manifest *m,
@@ -199,10 +218,22 @@ static void do_leakcheck_vg(struct manifest *m,
 {
        struct ccan_file *i;
        struct list_head *list;
+       char **options;
        bool leaks = false;
 
        foreach_ptr(list, &m->run_tests, &m->api_tests) {
                list_for_each(list, i, list) {
+                       options = per_file_options(&tests_pass_valgrind_noleaks,
+                                                  i);
+                       if (options[0]) {
+                               if (streq(options[0], "FAIL")) {
+                                       leaks = true;
+                                       continue;
+                               }
+                               errx(1, "Unknown leakcheck options '%s'",
+                                    options[0]);
+                       }
+
                        if (i->leak_info) {
                                score_file_error(score, i, 0, "%s",
                                                 i->leak_info);
@@ -226,13 +257,18 @@ static void run_under_debugger_vg(struct manifest *m, struct score *score)
        struct file_error *first;
        char *command;
 
+       /* Don't ask anything if they suppressed tests. */
+       if (score->pass)
+               return;
+
        if (!ask("Should I run the first failing test under the debugger?"))
                return;
 
        first = list_top(&score->per_file_errors, struct file_error, list);
        command = talloc_asprintf(m, "valgrind --leak-check=full --db-attach=yes%s %s",
-                                 tests_pass_valgrind.options ?
-                                 tests_pass_valgrind.options : "",
+                                 concat(score,
+                                        per_file_options(&tests_pass_valgrind,
+                                                         first->file)),
                                  first->file->compiled);
        if (system(command))
                doesnt_matter();
@@ -252,7 +288,7 @@ struct ccanlint tests_pass_valgrind_noleaks = {
        .key = "tests_pass_valgrind_noleaks",
        .name = "Module's run and api tests have no memory leaks",
        .check = do_leakcheck_vg,
+       .takes_options = true,
        .needs = "tests_pass_valgrind"
 };
 
-REGISTER_TEST(tests_pass_valgrind_noleaks);