t / helper / test-path-utils.con commit Sync with 2.16.5 (e43aab7)
   1#include "cache.h"
   2#include "string-list.h"
   3#include "utf8.h"
   4
   5/*
   6 * A "string_list_each_func_t" function that normalizes an entry from
   7 * GIT_CEILING_DIRECTORIES.  If the path is unusable for some reason,
   8 * die with an explanation.
   9 */
  10static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
  11{
  12        char *ceil = item->string;
  13
  14        if (!*ceil)
  15                die("Empty path is not supported");
  16        if (!is_absolute_path(ceil))
  17                die("Path \"%s\" is not absolute", ceil);
  18        if (normalize_path_copy(ceil, ceil) < 0)
  19                die("Path \"%s\" could not be normalized", ceil);
  20        return 1;
  21}
  22
  23static void normalize_argv_string(const char **var, const char *input)
  24{
  25        if (!strcmp(input, "<null>"))
  26                *var = NULL;
  27        else if (!strcmp(input, "<empty>"))
  28                *var = "";
  29        else
  30                *var = input;
  31
  32        if (*var && (**var == '<' || **var == '('))
  33                die("Bad value: %s\n", input);
  34}
  35
  36struct test_data {
  37        const char *from;  /* input:  transform from this ... */
  38        const char *to;    /* output: ... to this.            */
  39        const char *alternative; /* output: ... or this.      */
  40};
  41
  42/*
  43 * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
  44 * have const parameters.
  45 */
  46static char *posix_basename(char *path)
  47{
  48        return basename(path);
  49}
  50
  51static char *posix_dirname(char *path)
  52{
  53        return dirname(path);
  54}
  55
  56static int test_function(struct test_data *data, char *(*func)(char *input),
  57        const char *funcname)
  58{
  59        int failed = 0, i;
  60        char buffer[1024];
  61        char *to;
  62
  63        for (i = 0; data[i].to; i++) {
  64                if (!data[i].from)
  65                        to = func(NULL);
  66                else {
  67                        xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
  68                        to = func(buffer);
  69                }
  70                if (!strcmp(to, data[i].to))
  71                        continue;
  72                if (!data[i].alternative)
  73                        error("FAIL: %s(%s) => '%s' != '%s'\n",
  74                                funcname, data[i].from, to, data[i].to);
  75                else if (!strcmp(to, data[i].alternative))
  76                        continue;
  77                else
  78                        error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
  79                                funcname, data[i].from, to, data[i].to,
  80                                data[i].alternative);
  81                failed = 1;
  82        }
  83        return failed;
  84}
  85
  86static struct test_data basename_data[] = {
  87        /* --- POSIX type paths --- */
  88        { NULL,              "."    },
  89        { "",                "."    },
  90        { ".",               "."    },
  91        { "..",              ".."   },
  92        { "/",               "/"    },
  93        { "//",              "/", "//" },
  94        { "///",             "/", "//" },
  95        { "////",            "/", "//" },
  96        { "usr",             "usr"  },
  97        { "/usr",            "usr"  },
  98        { "/usr/",           "usr"  },
  99        { "/usr//",          "usr"  },
 100        { "/usr/lib",        "lib"  },
 101        { "usr/lib",         "lib"  },
 102        { "usr/lib///",      "lib"  },
 103
 104#if defined(__MINGW32__) || defined(_MSC_VER)
 105        /* --- win32 type paths --- */
 106        { "\\usr",           "usr"  },
 107        { "\\usr\\",         "usr"  },
 108        { "\\usr\\\\",       "usr"  },
 109        { "\\usr\\lib",      "lib"  },
 110        { "usr\\lib",        "lib"  },
 111        { "usr\\lib\\\\\\",  "lib"  },
 112        { "C:/usr",          "usr"  },
 113        { "C:/usr",          "usr"  },
 114        { "C:/usr/",         "usr"  },
 115        { "C:/usr//",        "usr"  },
 116        { "C:/usr/lib",      "lib"  },
 117        { "C:usr/lib",       "lib"  },
 118        { "C:usr/lib///",    "lib"  },
 119        { "C:",              "."    },
 120        { "C:a",             "a"    },
 121        { "C:/",             "/"    },
 122        { "C:///",           "/"    },
 123        { "\\",              "\\", "/" },
 124        { "\\\\",            "\\", "/" },
 125        { "\\\\\\",          "\\", "/" },
 126#endif
 127        { NULL,              NULL   }
 128};
 129
 130static struct test_data dirname_data[] = {
 131        /* --- POSIX type paths --- */
 132        { NULL,              "."      },
 133        { "",                "."      },
 134        { ".",               "."      },
 135        { "..",              "."      },
 136        { "/",               "/"      },
 137        { "//",              "/", "//" },
 138        { "///",             "/", "//" },
 139        { "////",            "/", "//" },
 140        { "usr",             "."      },
 141        { "/usr",            "/"      },
 142        { "/usr/",           "/"      },
 143        { "/usr//",          "/"      },
 144        { "/usr/lib",        "/usr"   },
 145        { "usr/lib",         "usr"    },
 146        { "usr/lib///",      "usr"    },
 147
 148#if defined(__MINGW32__) || defined(_MSC_VER)
 149        /* --- win32 type paths --- */
 150        { "\\",              "\\"     },
 151        { "\\\\",            "\\\\"   },
 152        { "\\usr",           "\\"     },
 153        { "\\usr\\",         "\\"     },
 154        { "\\usr\\\\",       "\\"     },
 155        { "\\usr\\lib",      "\\usr"  },
 156        { "usr\\lib",        "usr"    },
 157        { "usr\\lib\\\\\\",  "usr"    },
 158        { "C:a",             "C:."    },
 159        { "C:/",             "C:/"    },
 160        { "C:///",           "C:/"    },
 161        { "C:/usr",          "C:/"    },
 162        { "C:/usr/",         "C:/"    },
 163        { "C:/usr//",        "C:/"    },
 164        { "C:/usr/lib",      "C:/usr" },
 165        { "C:usr/lib",       "C:usr"  },
 166        { "C:usr/lib///",    "C:usr"  },
 167        { "\\\\\\",          "\\"     },
 168        { "\\\\\\\\",        "\\"     },
 169        { "C:",              "C:.", "." },
 170#endif
 171        { NULL,              NULL     }
 172};
 173
 174static int is_dotgitmodules(const char *path)
 175{
 176        return is_hfs_dotgitmodules(path) || is_ntfs_dotgitmodules(path);
 177}
 178
 179int cmd_main(int argc, const char **argv)
 180{
 181        if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
 182                char *buf = xmallocz(strlen(argv[2]));
 183                int rv = normalize_path_copy(buf, argv[2]);
 184                if (rv)
 185                        buf = "++failed++";
 186                puts(buf);
 187                return 0;
 188        }
 189
 190        if (argc >= 2 && !strcmp(argv[1], "real_path")) {
 191                while (argc > 2) {
 192                        puts(real_path(argv[2]));
 193                        argc--;
 194                        argv++;
 195                }
 196                return 0;
 197        }
 198
 199        if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
 200                while (argc > 2) {
 201                        puts(absolute_path(argv[2]));
 202                        argc--;
 203                        argv++;
 204                }
 205                return 0;
 206        }
 207
 208        if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
 209                int len;
 210                struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
 211                char *path = xstrdup(argv[2]);
 212
 213                /*
 214                 * We have to normalize the arguments because under
 215                 * Windows, bash mangles arguments that look like
 216                 * absolute POSIX paths or colon-separate lists of
 217                 * absolute POSIX paths into DOS paths (e.g.,
 218                 * "/foo:/foo/bar" might be converted to
 219                 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
 220                 * whereas longest_ancestor_length() requires paths
 221                 * that use forward slashes.
 222                 */
 223                if (normalize_path_copy(path, path))
 224                        die("Path \"%s\" could not be normalized", argv[2]);
 225                string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
 226                filter_string_list(&ceiling_dirs, 0,
 227                                   normalize_ceiling_entry, NULL);
 228                len = longest_ancestor_length(path, &ceiling_dirs);
 229                string_list_clear(&ceiling_dirs, 0);
 230                free(path);
 231                printf("%d\n", len);
 232                return 0;
 233        }
 234
 235        if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
 236                const char *prefix = argv[2];
 237                int prefix_len = strlen(prefix);
 238                int nongit_ok;
 239                setup_git_directory_gently(&nongit_ok);
 240                while (argc > 3) {
 241                        puts(prefix_path(prefix, prefix_len, argv[3]));
 242                        argc--;
 243                        argv++;
 244                }
 245                return 0;
 246        }
 247
 248        if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
 249                char *prefix = strip_path_suffix(argv[2], argv[3]);
 250                printf("%s\n", prefix ? prefix : "(null)");
 251                return 0;
 252        }
 253
 254        if (argc == 3 && !strcmp(argv[1], "print_path")) {
 255                puts(argv[2]);
 256                return 0;
 257        }
 258
 259        if (argc == 4 && !strcmp(argv[1], "relative_path")) {
 260                struct strbuf sb = STRBUF_INIT;
 261                const char *in, *prefix, *rel;
 262                normalize_argv_string(&in, argv[2]);
 263                normalize_argv_string(&prefix, argv[3]);
 264                rel = relative_path(in, prefix, &sb);
 265                if (!rel)
 266                        puts("(null)");
 267                else
 268                        puts(strlen(rel) > 0 ? rel : "(empty)");
 269                strbuf_release(&sb);
 270                return 0;
 271        }
 272
 273        if (argc == 2 && !strcmp(argv[1], "basename"))
 274                return test_function(basename_data, posix_basename, argv[1]);
 275
 276        if (argc == 2 && !strcmp(argv[1], "dirname"))
 277                return test_function(dirname_data, posix_dirname, argv[1]);
 278
 279        if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) {
 280                int res = 0, expect = 1, i;
 281                for (i = 2; i < argc; i++)
 282                        if (!strcmp("--not", argv[i]))
 283                                expect = !expect;
 284                        else if (expect != is_dotgitmodules(argv[i]))
 285                                res = error("'%s' is %s.gitmodules", argv[i],
 286                                            expect ? "not " : "");
 287                        else
 288                                fprintf(stderr, "ok: '%s' is %s.gitmodules\n",
 289                                        argv[i], expect ? "" : "not ");
 290                return !!res;
 291        }
 292
 293        fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
 294                argv[1] ? argv[1] : "(there was none)");
 295        return 1;
 296}