test: add test cases for relative_path

Add subcommand "relative_path" in test-path-utils, and add test cases in t0060. Johannes tested an earlier version of this patch on Windows, and found that some relative_path tests should be skipped on Windows. This is because the bash on Windows rewrites arguments of regular Windows programs, such as git and the test helpers, if the arguments look like absolute POSIX paths. As a consequence, the actual tests performed are not what the tests scripts expect. The tests that need *not* be skipped are those where the two paths passed to 'test-path-utils relative_path' have the same prefix and the result is expected to be a relative path. This is because the rewriting changes "/a/b" to "D:/Src/MSysGit/a/b", and when both inputs are extended the same way, this just cancels out in the relative path computation. Signed-off-by: Jiang Xin <worldhello.net@gmail.com> Helped-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jiang Xin committed Jun 25, 2013 at 23:53 UTC 203439b2840c4c384060df2fa192994e4b6740ed
2 files changed +62
t/t0060-path-utils.sh
+37
@@ -12,6 +12,11 @@ norm_path() {
12 "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'"
13 }
14
15 +relative_path() {
16 + test_expect_success $4 "relative path: $1 $2 => $3" \
17 + "test \"\$(test-path-utils relative_path '$1' '$2')\" = '$3'"
18 +}
19 +
20 # On Windows, we are using MSYS's bash, which mangles the paths.
21 # Absolute paths are anchored at the MSYS installation directory,
22 # which means that the path / accounts for this many characters:
@@ -183,4 +188,36 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
188 test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
189 '
190
191 +relative_path /a/b/c/ /a/b/ c/
192 +relative_path /a/b/c/ /a/b c/
193 +relative_path /a//b//c/ //a/b// c/ POSIX
194 +relative_path /a/b /a/b .
195 +relative_path /a/b/ /a/b .
196 +relative_path /a /a/b /a POSIX
197 +relative_path / /a/b/ / POSIX
198 +relative_path /a/c /a/b/ /a/c POSIX
199 +relative_path /a/c /a/b /a/c POSIX
200 +relative_path /x/y /a/b/ /x/y POSIX
201 +relative_path /a/b "<empty>" /a/b POSIX
202 +relative_path /a/b "<null>" /a/b POSIX
203 +relative_path a/b/c/ a/b/ c/
204 +relative_path a/b/c/ a/b c/
205 +relative_path a/b//c a//b c
206 +relative_path a/b/ a/b/ .
207 +relative_path a/b/ a/b .
208 +relative_path a a/b a # TODO: should be: ..
209 +relative_path x/y a/b x/y # TODO: should be: ../../x/y
210 +relative_path a/c a/b a/c # TODO: should be: ../c
211 +relative_path a/b "<empty>" a/b
212 +relative_path a/b "<null>" a/b
213 +relative_path "<empty>" /a/b "(empty)"
214 +relative_path "<empty>" "<empty>" "(empty)"
215 +relative_path "<empty>" "<null>" "(empty)"
216 +relative_path "<null>" "<empty>" "(null)"
217 +relative_path "<null>" "<null>" "(null)"
218 +
219 +test_expect_failure 'relative path: <null> /a/b => segfault' '
220 + test-path-utils relative_path "<null>" "/a/b"
221 +'
222 +
223 test_done
test-path-utils.c
+25
@@ -28,6 +28,19 @@ static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
28 return 1;
29 }
30
31 +static void normalize_argv_string(const char **var, const char *input)
32 +{
33 + if (!strcmp(input, "<null>"))
34 + *var = NULL;
35 + else if (!strcmp(input, "<empty>"))
36 + *var = "";
37 + else
38 + *var = input;
39 +
40 + if (*var && (**var == '<' || **var == '('))
41 + die("Bad value: %s\n", input);
42 +}
43 +
44 int main(int argc, char **argv)
45 {
46 if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
@@ -103,6 +116,18 @@ int main(int argc, char **argv)
116 return 0;
117 }
118
119 + if (argc == 4 && !strcmp(argv[1], "relative_path")) {
120 + const char *in, *prefix, *rel;
121 + normalize_argv_string(&in, argv[2]);
122 + normalize_argv_string(&prefix, argv[3]);
123 + rel = relative_path(in, prefix);
124 + if (!rel)
125 + puts("(null)");
126 + else
127 + puts(strlen(rel) > 0 ? rel : "(empty)");
128 + return 0;
129 + }
130 +
131 fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
132 argv[1] ? argv[1] : "(there was none)");
133 return 1;