Skip to content

Commit 203439b

Browse files
jiangxingitster
authored andcommitted
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 <[email protected]> Helped-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0c2b1cf commit 203439b

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

t/t0060-path-utils.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ norm_path() {
1212
"test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'"
1313
}
1414

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+
1520
# On Windows, we are using MSYS's bash, which mangles the paths.
1621
# Absolute paths are anchored at the MSYS installation directory,
1722
# which means that the path / accounts for this many characters:
@@ -183,4 +188,36 @@ test_expect_success SYMLINKS 'real path works on symlinks' '
183188
test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
184189
'
185190

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+
186223
test_done

test-path-utils.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
2828
return 1;
2929
}
3030

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+
3144
int main(int argc, char **argv)
3245
{
3346
if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
@@ -103,6 +116,18 @@ int main(int argc, char **argv)
103116
return 0;
104117
}
105118

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+
106131
fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
107132
argv[1] ? argv[1] : "(there was none)");
108133
return 1;

0 commit comments

Comments
 (0)