t / t0060-path-utils.shon commit mingw: accomodate t0060-path-utils for MSYS2 (fc56c7b)
   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
  22test_git_path() {
  23        test_expect_success "git-path $1 $2 => $3" "
  24                $1 git rev-parse --git-path $2 >actual &&
  25                echo $3 >expect &&
  26                test_cmp expect actual
  27        "
  28}
  29
  30# On Windows, we are using MSYS's bash, which mangles the paths.
  31# Absolute paths are anchored at the MSYS installation directory,
  32# which means that the path / accounts for this many characters:
  33rootoff=$(test-path-utils normalize_path_copy / | wc -c)
  34# Account for the trailing LF:
  35if test $rootoff = 2; then
  36        rootoff=        # we are on Unix
  37else
  38        rootoff=$(($rootoff-1))
  39        # In MSYS2, the root directory "/" is translated into a Windows
  40        # directory *with* trailing slash. Let's test for that and adjust
  41        # our expected longest ancestor length accordingly.
  42        case "$(test-path-utils print_path /)" in
  43        */) rootslash=1;;
  44        *) rootslash=0;;
  45        esac
  46fi
  47
  48ancestor() {
  49        # We do some math with the expected ancestor length.
  50        expected=$3
  51        if test -n "$rootoff" && test "x$expected" != x-1; then
  52                expected=$(($expected-$rootslash))
  53                test $expected -lt 0 ||
  54                expected=$(($expected+$rootoff))
  55        fi
  56        test_expect_success "longest ancestor: $1 $2 => $expected" \
  57        "actual=\$(test-path-utils longest_ancestor_length '$1' '$2') &&
  58         test \"\$actual\" = '$expected'"
  59}
  60
  61# Some absolute path tests should be skipped on Windows due to path mangling
  62# on POSIX-style absolute paths
  63case $(uname -s) in
  64*MINGW*)
  65        ;;
  66*)
  67        test_set_prereq POSIX
  68        ;;
  69esac
  70
  71norm_path "" ""
  72norm_path . ""
  73norm_path ./ ""
  74norm_path ./. ""
  75norm_path ./.. ++failed++
  76norm_path ../. ++failed++
  77norm_path ./../.// ++failed++
  78norm_path dir/.. ""
  79norm_path dir/sub/../.. ""
  80norm_path dir/sub/../../.. ++failed++
  81norm_path dir dir
  82norm_path dir// dir/
  83norm_path ./dir dir
  84norm_path dir/. dir/
  85norm_path dir///./ dir/
  86norm_path dir//sub/.. dir/
  87norm_path dir/sub/../ dir/
  88norm_path dir/sub/../. dir/
  89norm_path dir/s1/../s2/ dir/s2/
  90norm_path d1/s1///s2/..//../s3/ d1/s3/
  91norm_path d1/s1//../s2/../../d2 d2
  92norm_path d1/.../d2 d1/.../d2
  93norm_path d1/..././../d2 d1/d2
  94
  95norm_path / /
  96norm_path // / POSIX
  97norm_path /// / POSIX
  98norm_path /. /
  99norm_path /./ / POSIX
 100norm_path /./.. ++failed++ POSIX
 101norm_path /../. ++failed++
 102norm_path /./../.// ++failed++ POSIX
 103norm_path /dir/.. / POSIX
 104norm_path /dir/sub/../.. / POSIX
 105norm_path /dir/sub/../../.. ++failed++ POSIX
 106norm_path /dir /dir
 107norm_path /dir// /dir/
 108norm_path /./dir /dir
 109norm_path /dir/. /dir/
 110norm_path /dir///./ /dir/
 111norm_path /dir//sub/.. /dir/
 112norm_path /dir/sub/../ /dir/
 113norm_path //dir/sub/../. /dir/ POSIX
 114norm_path /dir/s1/../s2/ /dir/s2/
 115norm_path /d1/s1///s2/..//../s3/ /d1/s3/
 116norm_path /d1/s1//../s2/../../d2 /d2
 117norm_path /d1/.../d2 /d1/.../d2
 118norm_path /d1/..././../d2 /d1/d2
 119
 120ancestor / / -1
 121ancestor /foo / 0
 122ancestor /foo /fo -1
 123ancestor /foo /foo -1
 124ancestor /foo /bar -1
 125ancestor /foo /foo/bar -1
 126ancestor /foo /foo:/bar -1
 127ancestor /foo /:/foo:/bar 0
 128ancestor /foo /foo:/:/bar 0
 129ancestor /foo /:/bar:/foo 0
 130ancestor /foo/bar / 0
 131ancestor /foo/bar /fo -1
 132ancestor /foo/bar /foo 4
 133ancestor /foo/bar /foo/ba -1
 134ancestor /foo/bar /:/fo 0
 135ancestor /foo/bar /foo:/foo/ba 4
 136ancestor /foo/bar /bar -1
 137ancestor /foo/bar /fo -1
 138ancestor /foo/bar /foo:/bar 4
 139ancestor /foo/bar /:/foo:/bar 4
 140ancestor /foo/bar /foo:/:/bar 4
 141ancestor /foo/bar /:/bar:/fo 0
 142ancestor /foo/bar /:/bar 0
 143ancestor /foo/bar /foo 4
 144ancestor /foo/bar /foo:/bar 4
 145ancestor /foo/bar /bar -1
 146
 147test_expect_success 'strip_path_suffix' '
 148        test c:/msysgit = $(test-path-utils strip_path_suffix \
 149                c:/msysgit/libexec//git-core libexec/git-core)
 150'
 151
 152test_expect_success 'absolute path rejects the empty string' '
 153        test_must_fail test-path-utils absolute_path ""
 154'
 155
 156test_expect_success 'real path rejects the empty string' '
 157        test_must_fail test-path-utils real_path ""
 158'
 159
 160test_expect_success POSIX 'real path works on absolute paths 1' '
 161        nopath="hopefully-absent-path" &&
 162        test "/" = "$(test-path-utils real_path "/")" &&
 163        test "/$nopath" = "$(test-path-utils real_path "/$nopath")"
 164'
 165
 166test_expect_success 'real path works on absolute paths 2' '
 167        nopath="hopefully-absent-path" &&
 168        # Find an existing top-level directory for the remaining tests:
 169        d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
 170        test "$d" = "$(test-path-utils real_path "$d")" &&
 171        test "$d/$nopath" = "$(test-path-utils real_path "$d/$nopath")"
 172'
 173
 174test_expect_success POSIX 'real path removes extra leading slashes' '
 175        nopath="hopefully-absent-path" &&
 176        test "/" = "$(test-path-utils real_path "///")" &&
 177        test "/$nopath" = "$(test-path-utils real_path "///$nopath")" &&
 178        # Find an existing top-level directory for the remaining tests:
 179        d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
 180        test "$d" = "$(test-path-utils real_path "//$d")" &&
 181        test "$d/$nopath" = "$(test-path-utils real_path "//$d/$nopath")"
 182'
 183
 184test_expect_success 'real path removes other extra slashes' '
 185        nopath="hopefully-absent-path" &&
 186        # Find an existing top-level directory for the remaining tests:
 187        d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
 188        test "$d" = "$(test-path-utils real_path "$d///")" &&
 189        test "$d/$nopath" = "$(test-path-utils real_path "$d///$nopath")"
 190'
 191
 192test_expect_success SYMLINKS 'real path works on symlinks' '
 193        mkdir first &&
 194        ln -s ../.git first/.git &&
 195        mkdir second &&
 196        ln -s ../first second/other &&
 197        mkdir third &&
 198        dir="$(cd .git; pwd -P)" &&
 199        dir2=third/../second/other/.git &&
 200        test "$dir" = "$(test-path-utils real_path $dir2)" &&
 201        file="$dir"/index &&
 202        test "$file" = "$(test-path-utils real_path $dir2/index)" &&
 203        basename=blub &&
 204        test "$dir/$basename" = "$(cd .git && test-path-utils real_path "$basename")" &&
 205        ln -s ../first/file .git/syml &&
 206        sym="$(cd first; pwd -P)"/file &&
 207        test "$sym" = "$(test-path-utils real_path "$dir2/syml")"
 208'
 209
 210test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
 211        ln -s target symlink &&
 212        test "$(test-path-utils prefix_path prefix "$(pwd)/symlink")" = "symlink"
 213'
 214
 215test_expect_success 'prefix_path works with only absolute path to work tree' '
 216        echo "" >expected &&
 217        test-path-utils prefix_path prefix "$(pwd)" >actual &&
 218        test_cmp expected actual
 219'
 220
 221test_expect_success 'prefix_path rejects absolute path to dir with same beginning as work tree' '
 222        test_must_fail test-path-utils prefix_path prefix "$(pwd)a"
 223'
 224
 225test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having  same beginning as work tree' '
 226        git init repo &&
 227        ln -s repo repolink &&
 228        test "a" = "$(cd repo && test-path-utils prefix_path prefix "$(pwd)/../repolink/a")"
 229'
 230
 231relative_path /foo/a/b/c/       /foo/a/b/       c/
 232relative_path /foo/a/b/c/       /foo/a/b        c/
 233relative_path /foo/a//b//c/     ///foo/a/b//    c/              POSIX
 234relative_path /foo/a/b          /foo/a/b        ./
 235relative_path /foo/a/b/         /foo/a/b        ./
 236relative_path /foo/a            /foo/a/b        ../
 237relative_path /                 /foo/a/b/       ../../../
 238relative_path /foo/a/c          /foo/a/b/       ../c
 239relative_path /foo/a/c          /foo/a/b        ../c
 240relative_path /foo/x/y          /foo/a/b/       ../../x/y
 241relative_path /foo/a/b          "<empty>"       /foo/a/b
 242relative_path /foo/a/b          "<null>"        /foo/a/b
 243relative_path foo/a/b/c/        foo/a/b/        c/
 244relative_path foo/a/b/c/        foo/a/b         c/
 245relative_path foo/a/b//c        foo/a//b        c
 246relative_path foo/a/b/          foo/a/b/        ./
 247relative_path foo/a/b/          foo/a/b         ./
 248relative_path foo/a             foo/a/b         ../
 249relative_path foo/x/y           foo/a/b         ../../x/y
 250relative_path foo/a/c           foo/a/b         ../c
 251relative_path foo/a/b           /foo/x/y        foo/a/b
 252relative_path /foo/a/b          foo/x/y         /foo/a/b
 253relative_path d:/a/b            D:/a/c          ../b            MINGW
 254relative_path C:/a/b            D:/a/c          C:/a/b          MINGW
 255relative_path foo/a/b           "<empty>"       foo/a/b
 256relative_path foo/a/b           "<null>"        foo/a/b
 257relative_path "<empty>"         /foo/a/b        ./
 258relative_path "<empty>"         "<empty>"       ./
 259relative_path "<empty>"         "<null>"        ./
 260relative_path "<null>"          "<empty>"       ./
 261relative_path "<null>"          "<null>"        ./
 262relative_path "<null>"          /foo/a/b        ./
 263
 264test_git_path A=B                info/grafts .git/info/grafts
 265test_git_path GIT_GRAFT_FILE=foo info/grafts foo
 266test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
 267test_git_path GIT_INDEX_FILE=foo index foo
 268test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
 269test_git_path GIT_INDEX_FILE=foo index2 .git/index2
 270test_expect_success 'setup fake objects directory foo' 'mkdir foo'
 271test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
 272test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
 273test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
 274test_expect_success 'setup common repository' 'git --git-dir=bar init'
 275test_git_path GIT_COMMON_DIR=bar index                    .git/index
 276test_git_path GIT_COMMON_DIR=bar HEAD                     .git/HEAD
 277test_git_path GIT_COMMON_DIR=bar logs/HEAD                .git/logs/HEAD
 278test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo     .git/logs/refs/bisect/foo
 279test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo      bar/logs/refs/bisec/foo
 280test_git_path GIT_COMMON_DIR=bar logs/refs/bisec          bar/logs/refs/bisec
 281test_git_path GIT_COMMON_DIR=bar logs/refs/bisectfoo      bar/logs/refs/bisectfoo
 282test_git_path GIT_COMMON_DIR=bar objects                  bar/objects
 283test_git_path GIT_COMMON_DIR=bar objects/bar              bar/objects/bar
 284test_git_path GIT_COMMON_DIR=bar info/exclude             bar/info/exclude
 285test_git_path GIT_COMMON_DIR=bar info/grafts              bar/info/grafts
 286test_git_path GIT_COMMON_DIR=bar info/sparse-checkout     .git/info/sparse-checkout
 287test_git_path GIT_COMMON_DIR=bar info//sparse-checkout    .git/info//sparse-checkout
 288test_git_path GIT_COMMON_DIR=bar remotes/bar              bar/remotes/bar
 289test_git_path GIT_COMMON_DIR=bar branches/bar             bar/branches/bar
 290test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master   bar/logs/refs/heads/master
 291test_git_path GIT_COMMON_DIR=bar refs/heads/master        bar/refs/heads/master
 292test_git_path GIT_COMMON_DIR=bar refs/bisect/foo          .git/refs/bisect/foo
 293test_git_path GIT_COMMON_DIR=bar hooks/me                 bar/hooks/me
 294test_git_path GIT_COMMON_DIR=bar config                   bar/config
 295test_git_path GIT_COMMON_DIR=bar packed-refs              bar/packed-refs
 296test_git_path GIT_COMMON_DIR=bar shallow                  bar/shallow
 297
 298test_done