]>
Commit | Line | Data |
---|---|---|
ae299be0 DR |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2008 David Reiss | |
4 | # | |
5 | ||
6 | test_description='Test various path utilities' | |
7 | ||
8 | . ./test-lib.sh | |
9 | ||
2718e852 JS |
10 | norm_path() { |
11 | test_expect_success $3 "normalize path: $1 => $2" \ | |
f42302b4 | 12 | "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'" |
ae299be0 DR |
13 | } |
14 | ||
203439b2 JX |
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 | ||
2718e852 JS |
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: | |
23 | rootoff=$(test-path-utils normalize_path_copy / | wc -c) | |
24 | # Account for the trailing LF: | |
28baf82e | 25 | if test $rootoff = 2; then |
2718e852 JS |
26 | rootoff= # we are on Unix |
27 | else | |
28 | rootoff=$(($rootoff-1)) | |
29 | fi | |
30 | ||
0454dd93 | 31 | ancestor() { |
2718e852 JS |
32 | # We do some math with the expected ancestor length. |
33 | expected=$3 | |
34 | if test -n "$rootoff" && test "x$expected" != x-1; then | |
35 | expected=$(($expected+$rootoff)) | |
36 | fi | |
37 | test_expect_success "longest ancestor: $1 $2 => $expected" \ | |
38 | "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') && | |
39 | test \"\$actual\" = '$expected'" | |
0454dd93 DR |
40 | } |
41 | ||
2718e852 JS |
42 | # Absolute path tests must be skipped on Windows because due to path mangling |
43 | # the test program never sees a POSIX-style absolute path | |
44 | case $(uname -s) in | |
45 | *MINGW*) | |
46 | ;; | |
47 | *) | |
48 | test_set_prereq POSIX | |
49 | ;; | |
50 | esac | |
51 | ||
52 | norm_path "" "" | |
53 | norm_path . "" | |
54 | norm_path ./ "" | |
55 | norm_path ./. "" | |
56 | norm_path ./.. ++failed++ | |
57 | norm_path ../. ++failed++ | |
58 | norm_path ./../.// ++failed++ | |
59 | norm_path dir/.. "" | |
60 | norm_path dir/sub/../.. "" | |
61 | norm_path dir/sub/../../.. ++failed++ | |
62 | norm_path dir dir | |
63 | norm_path dir// dir/ | |
64 | norm_path ./dir dir | |
65 | norm_path dir/. dir/ | |
66 | norm_path dir///./ dir/ | |
67 | norm_path dir//sub/.. dir/ | |
68 | norm_path dir/sub/../ dir/ | |
69 | norm_path dir/sub/../. dir/ | |
70 | norm_path dir/s1/../s2/ dir/s2/ | |
71 | norm_path d1/s1///s2/..//../s3/ d1/s3/ | |
72 | norm_path d1/s1//../s2/../../d2 d2 | |
73 | norm_path d1/.../d2 d1/.../d2 | |
74 | norm_path d1/..././../d2 d1/d2 | |
75 | ||
76 | norm_path / / POSIX | |
77 | norm_path // / POSIX | |
78 | norm_path /// / POSIX | |
79 | norm_path /. / POSIX | |
80 | norm_path /./ / POSIX | |
81 | norm_path /./.. ++failed++ POSIX | |
82 | norm_path /../. ++failed++ POSIX | |
83 | norm_path /./../.// ++failed++ POSIX | |
84 | norm_path /dir/.. / POSIX | |
85 | norm_path /dir/sub/../.. / POSIX | |
86 | norm_path /dir/sub/../../.. ++failed++ POSIX | |
87 | norm_path /dir /dir POSIX | |
88 | norm_path /dir// /dir/ POSIX | |
89 | norm_path /./dir /dir POSIX | |
90 | norm_path /dir/. /dir/ POSIX | |
91 | norm_path /dir///./ /dir/ POSIX | |
92 | norm_path /dir//sub/.. /dir/ POSIX | |
93 | norm_path /dir/sub/../ /dir/ POSIX | |
94 | norm_path //dir/sub/../. /dir/ POSIX | |
95 | norm_path /dir/s1/../s2/ /dir/s2/ POSIX | |
96 | norm_path /d1/s1///s2/..//../s3/ /d1/s3/ POSIX | |
97 | norm_path /d1/s1//../s2/../../d2 /d2 POSIX | |
98 | norm_path /d1/.../d2 /d1/.../d2 POSIX | |
99 | norm_path /d1/..././../d2 /d1/d2 POSIX | |
ae299be0 | 100 | |
0454dd93 | 101 | ancestor / / -1 |
0454dd93 DR |
102 | ancestor /foo / 0 |
103 | ancestor /foo /fo -1 | |
104 | ancestor /foo /foo -1 | |
0454dd93 | 105 | ancestor /foo /bar -1 |
0454dd93 | 106 | ancestor /foo /foo/bar -1 |
9e2326c7 MH |
107 | ancestor /foo /foo:/bar -1 |
108 | ancestor /foo /:/foo:/bar 0 | |
109 | ancestor /foo /foo:/:/bar 0 | |
110 | ancestor /foo /:/bar:/foo 0 | |
0454dd93 DR |
111 | ancestor /foo/bar / 0 |
112 | ancestor /foo/bar /fo -1 | |
0454dd93 | 113 | ancestor /foo/bar /foo 4 |
0454dd93 DR |
114 | ancestor /foo/bar /foo/ba -1 |
115 | ancestor /foo/bar /:/fo 0 | |
116 | ancestor /foo/bar /foo:/foo/ba 4 | |
117 | ancestor /foo/bar /bar -1 | |
9e2326c7 MH |
118 | ancestor /foo/bar /fo -1 |
119 | ancestor /foo/bar /foo:/bar 4 | |
120 | ancestor /foo/bar /:/foo:/bar 4 | |
121 | ancestor /foo/bar /foo:/:/bar 4 | |
122 | ancestor /foo/bar /:/bar:/fo 0 | |
123 | ancestor /foo/bar /:/bar 0 | |
124 | ancestor /foo/bar /foo 4 | |
125 | ancestor /foo/bar /foo:/bar 4 | |
126 | ancestor /foo/bar /bar -1 | |
0454dd93 | 127 | |
4fcc86b0 JS |
128 | test_expect_success 'strip_path_suffix' ' |
129 | test c:/msysgit = $(test-path-utils strip_path_suffix \ | |
130 | c:/msysgit/libexec//git-core libexec/git-core) | |
131 | ' | |
8da650b4 | 132 | |
a0601dc1 | 133 | test_expect_success 'absolute path rejects the empty string' ' |
17264bcc MH |
134 | test_must_fail test-path-utils absolute_path "" |
135 | ' | |
136 | ||
3efe5d1d | 137 | test_expect_success 'real path rejects the empty string' ' |
a5c45218 MH |
138 | test_must_fail test-path-utils real_path "" |
139 | ' | |
140 | ||
bacca785 | 141 | test_expect_success POSIX 'real path works on absolute paths 1' ' |
7bcf48da MH |
142 | nopath="hopefully-absent-path" && |
143 | test "/" = "$(test-path-utils real_path "/")" && | |
bacca785 JS |
144 | test "/$nopath" = "$(test-path-utils real_path "/$nopath")" |
145 | ' | |
146 | ||
147 | test_expect_success 'real path works on absolute paths 2' ' | |
148 | nopath="hopefully-absent-path" && | |
7bcf48da MH |
149 | # Find an existing top-level directory for the remaining tests: |
150 | d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") && | |
151 | test "$d" = "$(test-path-utils real_path "$d")" && | |
152 | test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")" | |
153 | ' | |
154 | ||
379a03ad MH |
155 | test_expect_success POSIX 'real path removes extra leading slashes' ' |
156 | nopath="hopefully-absent-path" && | |
157 | test "/" = "$(test-path-utils real_path "///")" && | |
158 | test "/$nopath" = "$(test-path-utils real_path "///$nopath")" && | |
159 | # Find an existing top-level directory for the remaining tests: | |
160 | d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") && | |
161 | test "$d" = "$(test-path-utils real_path "//$d")" && | |
162 | test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")" | |
163 | ' | |
164 | ||
165 | test_expect_success 'real path removes other extra slashes' ' | |
166 | nopath="hopefully-absent-path" && | |
167 | # Find an existing top-level directory for the remaining tests: | |
168 | d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") && | |
169 | test "$d" = "$(test-path-utils real_path "$d///")" && | |
170 | test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")" | |
171 | ' | |
172 | ||
7bcf48da | 173 | test_expect_success SYMLINKS 'real path works on symlinks' ' |
8da650b4 MH |
174 | mkdir first && |
175 | ln -s ../.git first/.git && | |
176 | mkdir second && | |
177 | ln -s ../first second/other && | |
178 | mkdir third && | |
179 | dir="$(cd .git; pwd -P)" && | |
180 | dir2=third/../second/other/.git && | |
181 | test "$dir" = "$(test-path-utils real_path $dir2)" && | |
182 | file="$dir"/index && | |
183 | test "$file" = "$(test-path-utils real_path $dir2/index)" && | |
184 | basename=blub && | |
185 | test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" && | |
186 | ln -s ../first/file .git/syml && | |
187 | sym="$(cd first; pwd -P)"/file && | |
188 | test "$sym" = "$(test-path-utils real_path "$dir2/syml")" | |
189 | ' | |
190 | ||
203439b2 JX |
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 | |
e02ca72f JX |
194 | relative_path /a/b /a/b ./ |
195 | relative_path /a/b/ /a/b ./ | |
196 | relative_path /a /a/b ../ | |
197 | relative_path / /a/b/ ../../ | |
198 | relative_path /a/c /a/b/ ../c | |
199 | relative_path /a/c /a/b ../c | |
200 | relative_path /x/y /a/b/ ../../x/y | |
203439b2 JX |
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 | |
e02ca72f JX |
206 | relative_path a/b/ a/b/ ./ |
207 | relative_path a/b/ a/b ./ | |
208 | relative_path a a/b ../ | |
209 | relative_path x/y a/b ../../x/y | |
210 | relative_path a/c a/b ../c | |
203439b2 JX |
211 | relative_path a/b "<empty>" a/b |
212 | relative_path a/b "<null>" a/b | |
e02ca72f JX |
213 | relative_path "<empty>" /a/b ./ |
214 | relative_path "<empty>" "<empty>" ./ | |
215 | relative_path "<empty>" "<null>" ./ | |
216 | relative_path "<null>" "<empty>" ./ | |
217 | relative_path "<null>" "<null>" ./ | |
218 | relative_path "<null>" /a/b ./ | |
203439b2 | 219 | |
ae299be0 | 220 | test_done |