is_{hfs,ntfs}_dotgitmodules: add tests
[git.git] / t / helper / test-path-utils.c
blob77517a43edf6cd1549a0267352a27b226e1ea6a5
1 #include "cache.h"
2 #include "string-list.h"
3 #include "utf8.h"
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 */
10 static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
12 char *ceil = item->string;
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;
23 static void normalize_argv_string(const char **var, const char *input)
25 if (!strcmp(input, "<null>"))
26 *var = NULL;
27 else if (!strcmp(input, "<empty>"))
28 *var = "";
29 else
30 *var = input;
32 if (*var && (**var == '<' || **var == '('))
33 die("Bad value: %s\n", input);
36 struct test_data {
37 const char *from; /* input: transform from this ... */
38 const char *to; /* output: ... to this. */
39 const char *alternative; /* output: ... or this. */
42 static int test_function(struct test_data *data, char *(*func)(char *input),
43 const char *funcname)
45 int failed = 0, i;
46 char buffer[1024];
47 char *to;
49 for (i = 0; data[i].to; i++) {
50 if (!data[i].from)
51 to = func(NULL);
52 else {
53 xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
54 to = func(buffer);
56 if (!strcmp(to, data[i].to))
57 continue;
58 if (!data[i].alternative)
59 error("FAIL: %s(%s) => '%s' != '%s'\n",
60 funcname, data[i].from, to, data[i].to);
61 else if (!strcmp(to, data[i].alternative))
62 continue;
63 else
64 error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
65 funcname, data[i].from, to, data[i].to,
66 data[i].alternative);
67 failed = 1;
69 return failed;
72 static struct test_data basename_data[] = {
73 /* --- POSIX type paths --- */
74 { NULL, "." },
75 { "", "." },
76 { ".", "." },
77 { "..", ".." },
78 { "/", "/" },
79 { "//", "/", "//" },
80 { "///", "/", "//" },
81 { "////", "/", "//" },
82 { "usr", "usr" },
83 { "/usr", "usr" },
84 { "/usr/", "usr" },
85 { "/usr//", "usr" },
86 { "/usr/lib", "lib" },
87 { "usr/lib", "lib" },
88 { "usr/lib///", "lib" },
90 #if defined(__MINGW32__) || defined(_MSC_VER)
91 /* --- win32 type paths --- */
92 { "\\usr", "usr" },
93 { "\\usr\\", "usr" },
94 { "\\usr\\\\", "usr" },
95 { "\\usr\\lib", "lib" },
96 { "usr\\lib", "lib" },
97 { "usr\\lib\\\\\\", "lib" },
98 { "C:/usr", "usr" },
99 { "C:/usr", "usr" },
100 { "C:/usr/", "usr" },
101 { "C:/usr//", "usr" },
102 { "C:/usr/lib", "lib" },
103 { "C:usr/lib", "lib" },
104 { "C:usr/lib///", "lib" },
105 { "C:", "." },
106 { "C:a", "a" },
107 { "C:/", "/" },
108 { "C:///", "/" },
109 { "\\", "\\", "/" },
110 { "\\\\", "\\", "/" },
111 { "\\\\\\", "\\", "/" },
112 #endif
113 { NULL, NULL }
116 static struct test_data dirname_data[] = {
117 /* --- POSIX type paths --- */
118 { NULL, "." },
119 { "", "." },
120 { ".", "." },
121 { "..", "." },
122 { "/", "/" },
123 { "//", "/", "//" },
124 { "///", "/", "//" },
125 { "////", "/", "//" },
126 { "usr", "." },
127 { "/usr", "/" },
128 { "/usr/", "/" },
129 { "/usr//", "/" },
130 { "/usr/lib", "/usr" },
131 { "usr/lib", "usr" },
132 { "usr/lib///", "usr" },
134 #if defined(__MINGW32__) || defined(_MSC_VER)
135 /* --- win32 type paths --- */
136 { "\\", "\\" },
137 { "\\\\", "\\\\" },
138 { "\\usr", "\\" },
139 { "\\usr\\", "\\" },
140 { "\\usr\\\\", "\\" },
141 { "\\usr\\lib", "\\usr" },
142 { "usr\\lib", "usr" },
143 { "usr\\lib\\\\\\", "usr" },
144 { "C:a", "C:." },
145 { "C:/", "C:/" },
146 { "C:///", "C:/" },
147 { "C:/usr", "C:/" },
148 { "C:/usr/", "C:/" },
149 { "C:/usr//", "C:/" },
150 { "C:/usr/lib", "C:/usr" },
151 { "C:usr/lib", "C:usr" },
152 { "C:usr/lib///", "C:usr" },
153 { "\\\\\\", "\\" },
154 { "\\\\\\\\", "\\" },
155 { "C:", "C:.", "." },
156 #endif
157 { NULL, NULL }
160 static int is_dotgitmodules(const char *path)
162 return is_hfs_dotgitmodules(path) || is_ntfs_dotgitmodules(path);
165 int cmd_main(int argc, const char **argv)
167 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
168 char *buf = xmallocz(strlen(argv[2]));
169 int rv = normalize_path_copy(buf, argv[2]);
170 if (rv)
171 buf = "++failed++";
172 puts(buf);
173 return 0;
176 if (argc >= 2 && !strcmp(argv[1], "real_path")) {
177 while (argc > 2) {
178 puts(real_path(argv[2]));
179 argc--;
180 argv++;
182 return 0;
185 if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
186 while (argc > 2) {
187 puts(absolute_path(argv[2]));
188 argc--;
189 argv++;
191 return 0;
194 if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
195 int len;
196 struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
197 char *path = xstrdup(argv[2]);
200 * We have to normalize the arguments because under
201 * Windows, bash mangles arguments that look like
202 * absolute POSIX paths or colon-separate lists of
203 * absolute POSIX paths into DOS paths (e.g.,
204 * "/foo:/foo/bar" might be converted to
205 * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
206 * whereas longest_ancestor_length() requires paths
207 * that use forward slashes.
209 if (normalize_path_copy(path, path))
210 die("Path \"%s\" could not be normalized", argv[2]);
211 string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
212 filter_string_list(&ceiling_dirs, 0,
213 normalize_ceiling_entry, NULL);
214 len = longest_ancestor_length(path, &ceiling_dirs);
215 string_list_clear(&ceiling_dirs, 0);
216 free(path);
217 printf("%d\n", len);
218 return 0;
221 if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
222 const char *prefix = argv[2];
223 int prefix_len = strlen(prefix);
224 int nongit_ok;
225 setup_git_directory_gently(&nongit_ok);
226 while (argc > 3) {
227 puts(prefix_path(prefix, prefix_len, argv[3]));
228 argc--;
229 argv++;
231 return 0;
234 if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
235 char *prefix = strip_path_suffix(argv[2], argv[3]);
236 printf("%s\n", prefix ? prefix : "(null)");
237 return 0;
240 if (argc == 3 && !strcmp(argv[1], "print_path")) {
241 puts(argv[2]);
242 return 0;
245 if (argc == 4 && !strcmp(argv[1], "relative_path")) {
246 struct strbuf sb = STRBUF_INIT;
247 const char *in, *prefix, *rel;
248 normalize_argv_string(&in, argv[2]);
249 normalize_argv_string(&prefix, argv[3]);
250 rel = relative_path(in, prefix, &sb);
251 if (!rel)
252 puts("(null)");
253 else
254 puts(strlen(rel) > 0 ? rel : "(empty)");
255 strbuf_release(&sb);
256 return 0;
259 if (argc == 2 && !strcmp(argv[1], "basename"))
260 return test_function(basename_data, basename, argv[1]);
262 if (argc == 2 && !strcmp(argv[1], "dirname"))
263 return test_function(dirname_data, dirname, argv[1]);
265 if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) {
266 int res = 0, expect = 1, i;
267 for (i = 2; i < argc; i++)
268 if (!strcmp("--not", argv[i]))
269 expect = !expect;
270 else if (expect != is_dotgitmodules(argv[i]))
271 res = error("'%s' is %s.gitmodules", argv[i],
272 expect ? "not " : "");
273 else
274 fprintf(stderr, "ok: '%s' is %s.gitmodules\n",
275 argv[i], expect ? "" : "not ");
276 return !!res;
279 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
280 argv[1] ? argv[1] : "(there was none)");
281 return 1;