fast-export: trivial cleanup
[git/mingw.git] / t / t0060-path-utils.sh
blob4ef2345982fe0e0c175ed67edcb20b04c81b9680
1 #!/bin/sh
3 # Copyright (c) 2008 David Reiss
6 test_description='Test various path utilities'
8 . ./test-lib.sh
10 norm_path() {
11 test_expect_success $3 "normalize path: $1 => $2" \
12 "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'"
15 # On Windows, we are using MSYS's bash, which mangles the paths.
16 # Absolute paths are anchored at the MSYS installation directory,
17 # which means that the path / accounts for this many characters:
18 rootoff=$(test-path-utils normalize_path_copy / | wc -c)
19 # Account for the trailing LF:
20 if test $rootoff = 2; then
21 rootoff= # we are on Unix
22 else
23 rootoff=$(($rootoff-1))
26 ancestor() {
27 # We do some math with the expected ancestor length.
28 expected=$3
29 if test -n "$rootoff" && test "x$expected" != x-1; then
30 expected=$(($expected+$rootoff))
32 test_expect_success "longest ancestor: $1 $2 => $expected" \
33 "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
34 test \"\$actual\" = '$expected'"
37 # Absolute path tests must be skipped on Windows because due to path mangling
38 # the test program never sees a POSIX-style absolute path
39 case $(uname -s) in
40 *MINGW*)
43 test_set_prereq POSIX
45 esac
47 norm_path "" ""
48 norm_path . ""
49 norm_path ./ ""
50 norm_path ./. ""
51 norm_path ./.. ++failed++
52 norm_path ../. ++failed++
53 norm_path ./../.// ++failed++
54 norm_path dir/.. ""
55 norm_path dir/sub/../.. ""
56 norm_path dir/sub/../../.. ++failed++
57 norm_path dir dir
58 norm_path dir// dir/
59 norm_path ./dir dir
60 norm_path dir/. dir/
61 norm_path dir///./ dir/
62 norm_path dir//sub/.. dir/
63 norm_path dir/sub/../ dir/
64 norm_path dir/sub/../. dir/
65 norm_path dir/s1/../s2/ dir/s2/
66 norm_path d1/s1///s2/..//../s3/ d1/s3/
67 norm_path d1/s1//../s2/../../d2 d2
68 norm_path d1/.../d2 d1/.../d2
69 norm_path d1/..././../d2 d1/d2
71 norm_path / / POSIX
72 norm_path // / POSIX
73 norm_path /// / POSIX
74 norm_path /. / POSIX
75 norm_path /./ / POSIX
76 norm_path /./.. ++failed++ POSIX
77 norm_path /../. ++failed++ POSIX
78 norm_path /./../.// ++failed++ POSIX
79 norm_path /dir/.. / POSIX
80 norm_path /dir/sub/../.. / POSIX
81 norm_path /dir/sub/../../.. ++failed++ POSIX
82 norm_path /dir /dir POSIX
83 norm_path /dir// /dir/ POSIX
84 norm_path /./dir /dir POSIX
85 norm_path /dir/. /dir/ POSIX
86 norm_path /dir///./ /dir/ POSIX
87 norm_path /dir//sub/.. /dir/ POSIX
88 norm_path /dir/sub/../ /dir/ POSIX
89 norm_path //dir/sub/../. /dir/ POSIX
90 norm_path /dir/s1/../s2/ /dir/s2/ POSIX
91 norm_path /d1/s1///s2/..//../s3/ /d1/s3/ POSIX
92 norm_path /d1/s1//../s2/../../d2 /d2 POSIX
93 norm_path /d1/.../d2 /d1/.../d2 POSIX
94 norm_path /d1/..././../d2 /d1/d2 POSIX
96 ancestor / "" -1
97 ancestor / / -1
98 ancestor /foo "" -1
99 ancestor /foo : -1
100 ancestor /foo ::. -1
101 ancestor /foo ::..:: -1
102 ancestor /foo / 0
103 ancestor /foo /fo -1
104 ancestor /foo /foo -1
105 ancestor /foo /foo/ -1
106 ancestor /foo /bar -1
107 ancestor /foo /bar/ -1
108 ancestor /foo /foo/bar -1
109 ancestor /foo /foo:/bar/ -1
110 ancestor /foo /foo/:/bar/ -1
111 ancestor /foo /foo::/bar/ -1
112 ancestor /foo /:/foo:/bar/ 0
113 ancestor /foo /foo:/:/bar/ 0
114 ancestor /foo /:/bar/:/foo 0
115 ancestor /foo/bar "" -1
116 ancestor /foo/bar / 0
117 ancestor /foo/bar /fo -1
118 ancestor /foo/bar foo -1
119 ancestor /foo/bar /foo 4
120 ancestor /foo/bar /foo/ 4
121 ancestor /foo/bar /foo/ba -1
122 ancestor /foo/bar /:/fo 0
123 ancestor /foo/bar /foo:/foo/ba 4
124 ancestor /foo/bar /bar -1
125 ancestor /foo/bar /bar/ -1
126 ancestor /foo/bar /fo: -1
127 ancestor /foo/bar :/fo -1
128 ancestor /foo/bar /foo:/bar/ 4
129 ancestor /foo/bar /:/foo:/bar/ 4
130 ancestor /foo/bar /foo:/:/bar/ 4
131 ancestor /foo/bar /:/bar/:/fo 0
132 ancestor /foo/bar /:/bar/ 0
133 ancestor /foo/bar .:/foo/. 4
134 ancestor /foo/bar .:/foo/.:.: 4
135 ancestor /foo/bar /foo/./:.:/bar 4
136 ancestor /foo/bar .:/bar -1
138 test_expect_success 'strip_path_suffix' '
139 test c:/msysgit = $(test-path-utils strip_path_suffix \
140 c:/msysgit/libexec//git-core libexec/git-core)
143 test_expect_success 'absolute path rejects the empty string' '
144 test_must_fail test-path-utils absolute_path ""
147 test_expect_success 'real path rejects the empty string' '
148 test_must_fail test-path-utils real_path ""
151 test_expect_success POSIX 'real path works on absolute paths 1' '
152 nopath="hopefully-absent-path" &&
153 test "/" = "$(test-path-utils real_path "/")" &&
154 test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
157 test_expect_success 'real path works on absolute paths 2' '
158 nopath="hopefully-absent-path" &&
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")"
165 test_expect_success POSIX 'real path removes extra leading slashes' '
166 nopath="hopefully-absent-path" &&
167 test "/" = "$(test-path-utils real_path "///")" &&
168 test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
169 # Find an existing top-level directory for the remaining tests:
170 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
171 test "$d" = "$(test-path-utils real_path "//$d")" &&
172 test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
175 test_expect_success 'real path removes other extra slashes' '
176 nopath="hopefully-absent-path" &&
177 # Find an existing top-level directory for the remaining tests:
178 d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
179 test "$d" = "$(test-path-utils real_path "$d///")" &&
180 test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
183 test_expect_success SYMLINKS 'real path works on symlinks' '
184 mkdir first &&
185 ln -s ../.git first/.git &&
186 mkdir second &&
187 ln -s ../first second/other &&
188 mkdir third &&
189 dir="$(cd .git; pwd -P)" &&
190 dir2=third/../second/other/.git &&
191 test "$dir" = "$(test-path-utils real_path $dir2)" &&
192 file="$dir"/index &&
193 test "$file" = "$(test-path-utils real_path $dir2/index)" &&
194 basename=blub &&
195 test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
196 ln -s ../first/file .git/syml &&
197 sym="$(cd first; pwd -P)"/file &&
198 test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
201 test_done