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