t / t0060-path-utils.shon commit Merge branch 'maint' (01449e3)
   1#!/bin/sh
   2#
   3# Copyright (c) 2008 David Reiss
   4#
   5
   6test_description='Test various path utilities'
   7
   8. ./test-lib.sh
   9
  10norm_path() {
  11        test_expect_success $3 "normalize path: $1 => $2" \
  12        "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$2'"
  13}
  14
  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:
  20if test $rootoff = 2; then
  21        rootoff=        # we are on Unix
  22else
  23        rootoff=$(($rootoff-1))
  24fi
  25
  26ancestor() {
  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'"
  35}
  36
  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
  95
  96ancestor / / -1
  97ancestor /foo / 0
  98ancestor /foo /fo -1
  99ancestor /foo /foo -1
 100ancestor /foo /bar -1
 101ancestor /foo /foo/bar -1
 102ancestor /foo /foo:/bar -1
 103ancestor /foo /:/foo:/bar 0
 104ancestor /foo /foo:/:/bar 0
 105ancestor /foo /:/bar:/foo 0
 106ancestor /foo/bar / 0
 107ancestor /foo/bar /fo -1
 108ancestor /foo/bar /foo 4
 109ancestor /foo/bar /foo/ba -1
 110ancestor /foo/bar /:/fo 0
 111ancestor /foo/bar /foo:/foo/ba 4
 112ancestor /foo/bar /bar -1
 113ancestor /foo/bar /fo -1
 114ancestor /foo/bar /foo:/bar 4
 115ancestor /foo/bar /:/foo:/bar 4
 116ancestor /foo/bar /foo:/:/bar 4
 117ancestor /foo/bar /:/bar:/fo 0
 118ancestor /foo/bar /:/bar 0
 119ancestor /foo/bar /foo 4
 120ancestor /foo/bar /foo:/bar 4
 121ancestor /foo/bar /bar -1
 122
 123test_expect_success 'strip_path_suffix' '
 124        test c:/msysgit = $(test-path-utils strip_path_suffix \
 125                c:/msysgit/libexec//git-core libexec/git-core)
 126'
 127
 128test_expect_success 'absolute path rejects the empty string' '
 129        test_must_fail test-path-utils absolute_path ""
 130'
 131
 132test_expect_success 'real path rejects the empty string' '
 133        test_must_fail test-path-utils real_path ""
 134'
 135
 136test_expect_success POSIX 'real path works on absolute paths 1' '
 137        nopath="hopefully-absent-path" &&
 138        test "/" = "$(test-path-utils real_path "/")" &&
 139        test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
 140'
 141
 142test_expect_success 'real path works on absolute paths 2' '
 143        nopath="hopefully-absent-path" &&
 144        # Find an existing top-level directory for the remaining tests:
 145        d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
 146        test "$d" = "$(test-path-utils real_path "$d")" &&
 147        test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")"
 148'
 149
 150test_expect_success POSIX 'real path removes extra leading slashes' '
 151        nopath="hopefully-absent-path" &&
 152        test "/" = "$(test-path-utils real_path "///")" &&
 153        test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
 154        # Find an existing top-level directory for the remaining tests:
 155        d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
 156        test "$d" = "$(test-path-utils real_path "//$d")" &&
 157        test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
 158'
 159
 160test_expect_success 'real path removes other extra slashes' '
 161        nopath="hopefully-absent-path" &&
 162        # Find an existing top-level directory for the remaining tests:
 163        d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
 164        test "$d" = "$(test-path-utils real_path "$d///")" &&
 165        test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
 166'
 167
 168test_expect_success SYMLINKS 'real path works on symlinks' '
 169        mkdir first &&
 170        ln -s ../.git first/.git &&
 171        mkdir second &&
 172        ln -s ../first second/other &&
 173        mkdir third &&
 174        dir="$(cd .git; pwd -P)" &&
 175        dir2=third/../second/other/.git &&
 176        test "$dir" = "$(test-path-utils real_path $dir2)" &&
 177        file="$dir"/index &&
 178        test "$file" = "$(test-path-utils real_path $dir2/index)" &&
 179        basename=blub &&
 180        test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
 181        ln -s ../first/file .git/syml &&
 182        sym="$(cd first; pwd -P)"/file &&
 183        test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
 184'
 185
 186test_done