]> git.ipfire.org Git - thirdparty/git.git/blame - t/t0060-path-utils.sh
t0060: verify that absolute_path() fails if passed the empty string
[thirdparty/git.git] / t / t0060-path-utils.sh
CommitLineData
ae299be0
DR
1#!/bin/sh
2#
3# Copyright (c) 2008 David Reiss
4#
5
6test_description='Test various path utilities'
7
8. ./test-lib.sh
9
2718e852
JS
10norm_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
2718e852
JS
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:
18rootoff=$(test-path-utils normalize_path_copy / | wc -c)
19# Account for the trailing LF:
28baf82e 20if test $rootoff = 2; then
2718e852
JS
21 rootoff= # we are on Unix
22else
23 rootoff=$(($rootoff-1))
24fi
25
0454dd93 26ancestor() {
2718e852
JS
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))
31 fi
32 test_expect_success "longest ancestor: $1 $2 => $expected" \
33 "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
34 test \"\$actual\" = '$expected'"
0454dd93
DR
35}
36
2718e852
JS
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
39case $(uname -s) in
40*MINGW*)
41 ;;
42*)
43 test_set_prereq POSIX
44 ;;
45esac
46
47norm_path "" ""
48norm_path . ""
49norm_path ./ ""
50norm_path ./. ""
51norm_path ./.. ++failed++
52norm_path ../. ++failed++
53norm_path ./../.// ++failed++
54norm_path dir/.. ""
55norm_path dir/sub/../.. ""
56norm_path dir/sub/../../.. ++failed++
57norm_path dir dir
58norm_path dir// dir/
59norm_path ./dir dir
60norm_path dir/. dir/
61norm_path dir///./ dir/
62norm_path dir//sub/.. dir/
63norm_path dir/sub/../ dir/
64norm_path dir/sub/../. dir/
65norm_path dir/s1/../s2/ dir/s2/
66norm_path d1/s1///s2/..//../s3/ d1/s3/
67norm_path d1/s1//../s2/../../d2 d2
68norm_path d1/.../d2 d1/.../d2
69norm_path d1/..././../d2 d1/d2
70
71norm_path / / POSIX
72norm_path // / POSIX
73norm_path /// / POSIX
74norm_path /. / POSIX
75norm_path /./ / POSIX
76norm_path /./.. ++failed++ POSIX
77norm_path /../. ++failed++ POSIX
78norm_path /./../.// ++failed++ POSIX
79norm_path /dir/.. / POSIX
80norm_path /dir/sub/../.. / POSIX
81norm_path /dir/sub/../../.. ++failed++ POSIX
82norm_path /dir /dir POSIX
83norm_path /dir// /dir/ POSIX
84norm_path /./dir /dir POSIX
85norm_path /dir/. /dir/ POSIX
86norm_path /dir///./ /dir/ POSIX
87norm_path /dir//sub/.. /dir/ POSIX
88norm_path /dir/sub/../ /dir/ POSIX
89norm_path //dir/sub/../. /dir/ POSIX
90norm_path /dir/s1/../s2/ /dir/s2/ POSIX
91norm_path /d1/s1///s2/..//../s3/ /d1/s3/ POSIX
92norm_path /d1/s1//../s2/../../d2 /d2 POSIX
93norm_path /d1/.../d2 /d1/.../d2 POSIX
94norm_path /d1/..././../d2 /d1/d2 POSIX
ae299be0 95
0454dd93
DR
96ancestor / "" -1
97ancestor / / -1
98ancestor /foo "" -1
99ancestor /foo : -1
100ancestor /foo ::. -1
101ancestor /foo ::..:: -1
102ancestor /foo / 0
103ancestor /foo /fo -1
104ancestor /foo /foo -1
105ancestor /foo /foo/ -1
106ancestor /foo /bar -1
107ancestor /foo /bar/ -1
108ancestor /foo /foo/bar -1
109ancestor /foo /foo:/bar/ -1
110ancestor /foo /foo/:/bar/ -1
111ancestor /foo /foo::/bar/ -1
112ancestor /foo /:/foo:/bar/ 0
113ancestor /foo /foo:/:/bar/ 0
114ancestor /foo /:/bar/:/foo 0
115ancestor /foo/bar "" -1
116ancestor /foo/bar / 0
117ancestor /foo/bar /fo -1
118ancestor /foo/bar foo -1
119ancestor /foo/bar /foo 4
120ancestor /foo/bar /foo/ 4
121ancestor /foo/bar /foo/ba -1
122ancestor /foo/bar /:/fo 0
123ancestor /foo/bar /foo:/foo/ba 4
124ancestor /foo/bar /bar -1
125ancestor /foo/bar /bar/ -1
126ancestor /foo/bar /fo: -1
127ancestor /foo/bar :/fo -1
128ancestor /foo/bar /foo:/bar/ 4
129ancestor /foo/bar /:/foo:/bar/ 4
130ancestor /foo/bar /foo:/:/bar/ 4
131ancestor /foo/bar /:/bar/:/fo 0
132ancestor /foo/bar /:/bar/ 0
2718e852
JS
133ancestor /foo/bar .:/foo/. 4
134ancestor /foo/bar .:/foo/.:.: 4
135ancestor /foo/bar /foo/./:.:/bar 4
136ancestor /foo/bar .:/bar -1
0454dd93 137
4fcc86b0
JS
138test_expect_success 'strip_path_suffix' '
139 test c:/msysgit = $(test-path-utils strip_path_suffix \
140 c:/msysgit/libexec//git-core libexec/git-core)
141'
8da650b4 142
17264bcc
MH
143test_expect_failure 'absolute path rejects the empty string' '
144 test_must_fail test-path-utils absolute_path ""
145'
146
8da650b4
MH
147test_expect_success SYMLINKS 'real path works as expected' '
148 mkdir first &&
149 ln -s ../.git first/.git &&
150 mkdir second &&
151 ln -s ../first second/other &&
152 mkdir third &&
153 dir="$(cd .git; pwd -P)" &&
154 dir2=third/../second/other/.git &&
155 test "$dir" = "$(test-path-utils real_path $dir2)" &&
156 file="$dir"/index &&
157 test "$file" = "$(test-path-utils real_path $dir2/index)" &&
158 basename=blub &&
159 test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
160 ln -s ../first/file .git/syml &&
161 sym="$(cd first; pwd -P)"/file &&
162 test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
163'
164
ae299be0 165test_done