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