]> git.ozlabs.org Git - ccan/blobdiff - tools/ccanlint/ccanlint.c
ccanlint: print module name properly for nested modules.
[ccan] / tools / ccanlint / ccanlint.c
index 17f7a9c640c07f77208b30b2f3752ad4cd62e018..2fdb8863cea9dc79c291518bb89c668a653e80c5 100644 (file)
@@ -32,6 +32,7 @@
 #include <ccan/foreach/foreach.h>
 #include <ccan/cast/cast.h>
 #include <ccan/tlist/tlist.h>
+#include <ccan/tal/path/path.h>
 #include <ccan/strmap/strmap.h>
 
 struct ccanlint_map {
@@ -584,13 +585,27 @@ static bool add_to_all(const char *member, struct ccanlint *c,
        return true;
 }
 
+static bool test_module(struct dgraph_node *all,
+                       const char *dir, const char *prefix, bool summary)
+{
+       struct manifest *m = get_manifest(autofree(), dir);
+       char *testlink = path_join(NULL, temp_dir(), "test");
+
+       /* Create a symlink from temp dir back to src dir's
+        * test directory. */
+       unlink(testlink);
+       if (symlink(path_join(m, dir, "test"), testlink) != 0)
+               err(1, "Creating test symlink in %s", temp_dir());
+
+       return run_tests(all, summary, m, prefix);
+}
+
 int main(int argc, char *argv[])
 {
        bool summary = false, pass = true;
        unsigned int i;
-       struct manifest *m;
        const char *prefix = "";
-       char *dir = tal_getcwd(NULL), *base_dir = dir, *testlink;
+       char *cwd = path_cwd(NULL), *dir;
        struct dgraph_node all;
        
        /* Empty graph node to which we attach everything else. */
@@ -645,51 +660,31 @@ int main(int argc, char *argv[])
        if (!targeting)
                strmap_iterate(&tests, add_to_all, &all);
 
-       /* This links back to the module's test dir. */
-       testlink = tal_fmt(NULL, "%s/test", temp_dir());
-
-       /* Defaults to pwd. */
-       if (argc == 1) {
-               i = 1;
-               goto got_dir;
-       }
-
-       for (i = 1; i < argc; i++) {
-               dir = argv[i];
-
-               if (dir[0] != '/')
-                       dir = tal_fmt(NULL, "%s/%s", base_dir, dir);
-               while (strends(dir, "/"))
-                       dir[strlen(dir)-1] = '\0';
-
-       got_dir:
-               /* We assume there's a ccan/ in there somewhere... */
-               if (i == 1) {
-                       ccan_dir = find_ccan_dir(dir);
-                       if (!ccan_dir)
-                               errx(1, "Cannot find ccan/ base directory in %s",
-                                    dir);
-                       config_header = read_config_header(ccan_dir,
-                                                          &compiler, &cflags,
-                                                          verbose > 1);
+       if (argc == 1)
+               dir = cwd;
+       else
+               dir = path_join(NULL, cwd, argv[1]);
+
+       ccan_dir = find_ccan_dir(dir);
+       if (!ccan_dir)
+               errx(1, "Cannot find ccan/ base directory in %s", dir);
+       config_header = read_config_header(ccan_dir, &compiler, &cflags,
+                                          verbose > 1);
+
+       if (argc == 1)
+               pass = test_module(&all, cwd, "", summary);
+       else {
+               for (i = 1; i < argc; i++) {
+                       dir = path_canon(NULL,
+                                        take(path_join(NULL, cwd, argv[i])));
+
+                       prefix = path_join(NULL, ccan_dir, "ccan");
+                       prefix = path_rel(NULL, take(prefix), dir);
+                       prefix = tal_strcat(NULL, take(prefix), ": ");
+
+                       pass &= test_module(&all, dir, prefix, summary);
+                       reset_tests(&all);
                }
-
-               if (dir != base_dir)
-                       prefix = tal_strcat(NULL, take(tal_basename(NULL,dir)),
-                                           ": ");
-
-               m = get_manifest(autofree(), dir);
-
-               /* Create a symlink from temp dir back to src dir's
-                * test directory. */
-               unlink(testlink);
-               if (symlink(tal_fmt(m, "%s/test", dir), testlink) != 0)
-                       err(1, "Creating test symlink in %s", temp_dir());
-
-               if (!run_tests(&all, summary, m, prefix))
-                       pass = false;
-
-               reset_tests(&all);
        }
        return pass ? 0 : 1;
 }