t / t0060-path-utils.shon commit Git 2.4.9 (74b6763)
   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        expected=$(test-path-utils print_path "$2")
  12        test_expect_success $3 "normalize path: $1 => $2" \
  13        "test \"\$(test-path-utils normalize_path_copy '$1')\" = '$expected'"
  14}
  15
  16relative_path() {
  17        expected=$(test-path-utils print_path "$3")
  18        test_expect_success $4 "relative path: $1 $2 => $3" \
  19        "test \"\$(test-path-utils relative_path '$1' '$2')\" = '$expected'"
  20}
  21
  22# On Windows, we are using MSYS's bash, which mangles the paths.
  23# Absolute paths are anchored at the MSYS installation directory,
  24# which means that the path / accounts for this many characters:
  25rootoff=$(test-path-utils normalize_path_copy / | wc -c)
  26# Account for the trailing LF:
  27if test $rootoff = 2; then
  28        rootoff=        # we are on Unix
  29else
  30        rootoff=$(($rootoff-1))
  31fi
  32
  33ancestor() {
  34        # We do some math with the expected ancestor length.
  35        expected=$3
  36        if test -n "$rootoff" && test "x$expected" != x-1; then
  37                expected=$(($expected+$rootoff))
  38        fi
  39        test_expect_success "longest ancestor: $1 $2 => $expected" \
  40        "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
  41         test \"\$actual\" = '$expected'"
  42}
  43
  44# Some absolute path tests should be skipped on Windows due to path mangling
  45# on POSIX-style absolute paths
  46case $(uname -s) in
  47*MINGW*)
  48        ;;
  49*)
  50        test_set_prereq POSIX
  51        ;;
  52esac
  53
  54norm_path "" ""
  55norm_path . ""
  56norm_path ./ ""
  57norm_path ./. ""
  58norm_path ./.. ++failed++
  59norm_path ../. ++failed++
  60norm_path ./../.// ++failed++
  61norm_path dir/.. ""
  62norm_path dir/sub/../.. ""
  63norm_path dir/sub/../../.. ++failed++
  64norm_path dir dir
  65norm_path dir// dir/
  66norm_path ./dir dir
  67norm_path dir/. dir/
  68norm_path dir///./ dir/
  69norm_path dir//sub/.. dir/
  70norm_path dir/sub/../ dir/
  71norm_path dir/sub/../. dir/
  72norm_path dir/s1/../s2/ dir/s2/
  73norm_path d1/s1///s2/..//../s3/ d1/s3/
  74norm_path d1/s1//../s2/../../d2 d2
  75norm_path d1/.../d2 d1/.../d2
  76norm_path d1/..././../d2 d1/d2
  77
  78norm_path / /
  79norm_path // / POSIX
  80norm_path /// / POSIX
  81norm_path /. /
  82norm_path /./ / POSIX
  83norm_path /./.. ++failed++ POSIX
  84norm_path /../. ++failed++
  85norm_path /./../.// ++failed++ POSIX
  86norm_path /dir/.. / POSIX
  87norm_path /dir/sub/../.. / POSIX
  88norm_path /dir/sub/../../.. ++failed++ POSIX
  89norm_path /dir /dir
  90norm_path /dir// /dir/
  91norm_path /./dir /dir
  92norm_path /dir/. /dir/
  93norm_path /dir///./ /dir/
  94norm_path /dir//sub/.. /dir/
  95norm_path /dir/sub/../ /dir/
  96norm_path //dir/sub/../. /dir/ POSIX
  97norm_path /dir/s1/../s2/ /dir/s2/
  98norm_path /d1/s1///s2/..//../s3/ /d1/s3/
  99norm_path /d1/s1//../s2/../../d2 /d2
 100norm_path /d1/.../d2 /d1/.../d2
 101norm_path /d1/..././../d2 /d1/d2
 102
 103ancestor / / -1
 104ancestor /foo / 0
 105ancestor /foo /fo -1
 106ancestor /foo /foo -1
 107ancestor /foo /bar -1
 108ancestor /foo /foo/bar -1
 109ancestor /foo /foo:/bar -1
 110ancestor /foo /:/foo:/bar 0
 111ancestor /foo /foo:/:/bar 0
 112ancestor /foo /:/bar:/foo 0
 113ancestor /foo/bar / 0
 114ancestor /foo/bar /fo -1
 115ancestor /foo/bar /foo 4
 116ancestor /foo/bar /foo/ba -1
 117ancestor /foo/bar /:/fo 0
 118ancestor /foo/bar /foo:/foo/ba 4
 119ancestor /foo/bar /bar -1
 120ancestor /foo/bar /fo -1
 121ancestor /foo/bar /foo:/bar 4
 122ancestor /foo/bar /:/foo:/bar 4
 123ancestor /foo/bar /foo:/:/bar 4
 124ancestor /foo/bar /:/bar:/fo 0
 125ancestor /foo/bar /:/bar 0
 126ancestor /foo/bar /foo 4
 127ancestor /foo/bar /foo:/bar 4
 128ancestor /foo/bar /bar -1
 129
 130test_expect_success 'strip_path_suffix' '
 131        test c:/msysgit = $(test-path-utils strip_path_suffix \
 132                c:/msysgit/libexec//git-core libexec/git-core)
 133'
 134
 135test_expect_success 'absolute path rejects the empty string' '
 136        test_must_fail test-path-utils absolute_path ""
 137'
 138
 139test_expect_success 'real path rejects the empty string' '
 140        test_must_fail test-path-utils real_path ""
 141'
 142
 143test_expect_success POSIX 'real path works on absolute paths 1' '
 144        nopath="hopefully-absent-path" &&
 145        test "/" = "$(test-path-utils real_path "/")" &&
 146        test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
 147'
 148
 149test_expect_success 'real path works on absolute paths 2' '
 150        nopath="hopefully-absent-path" &&
 151        # Find an existing top-level directory for the remaining tests:
 152        d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
 153        test "$d" = "$(test-path-utils real_path "$d")" &&
 154        test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")"
 155'
 156
 157test_expect_success POSIX 'real path removes extra leading slashes' '
 158        nopath="hopefully-absent-path" &&
 159        test "/" = "$(test-path-utils real_path "///")" &&
 160        test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
 161        # Find an existing top-level directory for the remaining tests:
 162        d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
 163        test "$d" = "$(test-path-utils real_path "//$d")" &&
 164        test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
 165'
 166
 167test_expect_success 'real path removes other extra slashes' '
 168        nopath="hopefully-absent-path" &&
 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")"
 173'
 174
 175test_expect_success SYMLINKS 'real path works on symlinks' '
 176        mkdir first &&
 177        ln -s ../.git first/.git &&
 178        mkdir second &&
 179        ln -s ../first second/other &&
 180        mkdir third &&
 181        dir="$(cd .git; pwd -P)" &&
 182        dir2=third/../second/other/.git &&
 183        test "$dir" = "$(test-path-utils real_path $dir2)" &&
 184        file="$dir"/index &&
 185        test "$file" = "$(test-path-utils real_path $dir2/index)" &&
 186        basename=blub &&
 187        test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
 188        ln -s ../first/file .git/syml &&
 189        sym="$(cd first; pwd -P)"/file &&
 190        test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
 191'
 192
 193test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
 194        ln -s target symlink &&
 195        test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
 196'
 197
 198test_expect_success 'prefix_path works with only absolute path to work tree' '
 199        echo "" >expected &&
 200        test-path-utils prefix_path prefix "$(pwd)" >actual &&
 201        test_cmp expected actual
 202'
 203
 204test_expect_success 'prefix_path rejects absolute path to dir with same beginning as work tree' '
 205        test_must_fail test-path-utils prefix_path prefix "$(pwd)a"
 206'
 207
 208test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having  same beginning as work tree' '
 209        git init repo &&
 210        ln -s repo repolink &&
 211        test "a" = "$(cd repo && test-path-utils prefix_path prefix "$(pwd)/../repolink/a")"
 212'
 213
 214relative_path /foo/a/b/c/       /foo/a/b/       c/
 215relative_path /foo/a/b/c/       /foo/a/b        c/
 216relative_path /foo/a//b//c/     ///foo/a/b//    c/              POSIX
 217relative_path /foo/a/b          /foo/a/b        ./
 218relative_path /foo/a/b/         /foo/a/b        ./
 219relative_path /foo/a            /foo/a/b        ../
 220relative_path /                 /foo/a/b/       ../../../
 221relative_path /foo/a/c          /foo/a/b/       ../c
 222relative_path /foo/a/c          /foo/a/b        ../c
 223relative_path /foo/x/y          /foo/a/b/       ../../x/y
 224relative_path /foo/a/b          "<empty>"       /foo/a/b
 225relative_path /foo/a/b          "<null>"        /foo/a/b
 226relative_path foo/a/b/c/        foo/a/b/        c/
 227relative_path foo/a/b/c/        foo/a/b         c/
 228relative_path foo/a/b//c        foo/a//b        c
 229relative_path foo/a/b/          foo/a/b/        ./
 230relative_path foo/a/b/          foo/a/b         ./
 231relative_path foo/a             foo/a/b         ../
 232relative_path foo/x/y           foo/a/b         ../../x/y
 233relative_path foo/a/c           foo/a/b         ../c
 234relative_path foo/a/b           /foo/x/y        foo/a/b
 235relative_path /foo/a/b          foo/x/y         /foo/a/b
 236relative_path d:/a/b            D:/a/c          ../b            MINGW
 237relative_path C:/a/b            D:/a/c          C:/a/b          MINGW
 238relative_path foo/a/b           "<empty>"       foo/a/b
 239relative_path foo/a/b           "<null>"        foo/a/b
 240relative_path "<empty>"         /foo/a/b        ./
 241relative_path "<empty>"         "<empty>"       ./
 242relative_path "<empty>"         "<null>"        ./
 243relative_path "<null>"          "<empty>"       ./
 244relative_path "<null>"          "<null>"        ./
 245relative_path "<null>"          /foo/a/b        ./
 246
 247test_done