c0a45630be7204fde75632a5abea8f9aeda9f13d
   1#!/bin/sh
   2
   3test_description=gitattributes
   4
   5. ./test-lib.sh
   6
   7attr_check () {
   8
   9        path="$1"
  10        expect="$2"
  11
  12        git check-attr test -- "$path" >actual 2>err &&
  13        echo "$path: test: $2" >expect &&
  14        test_cmp expect actual &&
  15        test_line_count = 0 err
  16
  17}
  18
  19
  20test_expect_success 'setup' '
  21
  22        mkdir -p a/b/d a/c b &&
  23        (
  24                echo "[attr]notest !test"
  25                echo "f test=f"
  26                echo "a/i test=a/i"
  27                echo "onoff test -test"
  28                echo "offon -test test"
  29                echo "no notest"
  30        ) >.gitattributes &&
  31        (
  32                echo "g test=a/g" &&
  33                echo "b/g test=a/b/g"
  34        ) >a/.gitattributes &&
  35        (
  36                echo "h test=a/b/h" &&
  37                echo "d/* test=a/b/d/*"
  38                echo "d/yes notest"
  39        ) >a/b/.gitattributes &&
  40        (
  41                echo "global test=global"
  42        ) >"$HOME"/global-gitattributes &&
  43        cat <<EOF >expect-all
  44f: test: f
  45a/f: test: f
  46a/c/f: test: f
  47a/g: test: a/g
  48a/b/g: test: a/b/g
  49b/g: test: unspecified
  50a/b/h: test: a/b/h
  51a/b/d/g: test: a/b/d/*
  52onoff: test: unset
  53offon: test: set
  54no: notest: set
  55no: test: unspecified
  56a/b/d/no: notest: set
  57a/b/d/no: test: a/b/d/*
  58a/b/d/yes: notest: set
  59a/b/d/yes: test: unspecified
  60EOF
  61
  62'
  63
  64test_expect_success 'command line checks' '
  65
  66        test_must_fail git check-attr &&
  67        test_must_fail git check-attr -- &&
  68        test_must_fail git check-attr test &&
  69        test_must_fail git check-attr test -- &&
  70        test_must_fail git check-attr -- f &&
  71        echo "f" | test_must_fail git check-attr --stdin &&
  72        echo "f" | test_must_fail git check-attr --stdin -- f &&
  73        echo "f" | test_must_fail git check-attr --stdin test -- f &&
  74        test_must_fail git check-attr "" -- f
  75
  76'
  77
  78test_expect_success 'attribute test' '
  79
  80        attr_check f f &&
  81        attr_check a/f f &&
  82        attr_check a/c/f f &&
  83        attr_check a/g a/g &&
  84        attr_check a/b/g a/b/g &&
  85        attr_check b/g unspecified &&
  86        attr_check a/b/h a/b/h &&
  87        attr_check a/b/d/g "a/b/d/*" &&
  88        attr_check onoff unset &&
  89        attr_check offon set &&
  90        attr_check no unspecified &&
  91        attr_check a/b/d/no "a/b/d/*" &&
  92        attr_check a/b/d/yes unspecified
  93
  94'
  95
  96test_expect_success 'unnormalized paths' '
  97
  98        attr_check ./f f &&
  99        attr_check ./a/g a/g &&
 100        attr_check a/./g a/g &&
 101        attr_check a/c/../b/g a/b/g
 102
 103'
 104
 105test_expect_success 'relative paths' '
 106
 107        (cd a && attr_check ../f f) &&
 108        (cd a && attr_check f f) &&
 109        (cd a && attr_check i a/i) &&
 110        (cd a && attr_check g a/g) &&
 111        (cd a && attr_check b/g a/b/g) &&
 112        (cd b && attr_check ../a/f f) &&
 113        (cd b && attr_check ../a/g a/g) &&
 114        (cd b && attr_check ../a/b/g a/b/g)
 115
 116'
 117
 118test_expect_success 'core.attributesfile' '
 119        attr_check global unspecified &&
 120        git config core.attributesfile "$HOME/global-gitattributes" &&
 121        attr_check global global &&
 122        git config core.attributesfile "~/global-gitattributes" &&
 123        attr_check global global &&
 124        echo "global test=precedence" >> .gitattributes &&
 125        attr_check global precedence
 126'
 127
 128test_expect_success 'attribute test: read paths from stdin' '
 129
 130        grep -v notest < expect-all > expect &&
 131        sed -e "s/:.*//" < expect | git check-attr --stdin test > actual &&
 132        test_cmp expect actual
 133'
 134
 135test_expect_success 'attribute test: --all option' '
 136
 137        grep -v unspecified <expect-all | sort >specified-all &&
 138        sed -e "s/:.*//" <expect-all | uniq >stdin-all &&
 139        git check-attr --stdin --all <stdin-all | sort >actual &&
 140        test_cmp specified-all actual
 141'
 142
 143test_expect_success 'attribute test: --cached option' '
 144
 145        : >empty &&
 146        git check-attr --cached --stdin --all <stdin-all | sort >actual &&
 147        test_cmp empty actual &&
 148        git add .gitattributes a/.gitattributes a/b/.gitattributes &&
 149        git check-attr --cached --stdin --all <stdin-all | sort >actual &&
 150        test_cmp specified-all actual
 151'
 152
 153test_expect_success 'root subdir attribute test' '
 154
 155        attr_check a/i a/i &&
 156        attr_check subdir/a/i unspecified
 157
 158'
 159
 160test_expect_success 'setup bare' '
 161
 162        git clone --bare . bare.git &&
 163        cd bare.git
 164
 165'
 166
 167test_expect_success 'bare repository: check that .gitattribute is ignored' '
 168
 169        (
 170                echo "f test=f"
 171                echo "a/i test=a/i"
 172        ) >.gitattributes &&
 173        attr_check f unspecified &&
 174        attr_check a/f unspecified &&
 175        attr_check a/c/f unspecified &&
 176        attr_check a/i unspecified &&
 177        attr_check subdir/a/i unspecified
 178
 179'
 180
 181test_expect_success 'bare repository: check that --cached honors index' '
 182        GIT_INDEX_FILE=../.git/index \
 183        git check-attr --cached --stdin --all <../stdin-all |
 184        sort >actual &&
 185        test_cmp ../specified-all actual
 186'
 187
 188test_expect_success 'bare repository: test info/attributes' '
 189
 190        (
 191                echo "f test=f"
 192                echo "a/i test=a/i"
 193        ) >info/attributes &&
 194        attr_check f f &&
 195        attr_check a/f f &&
 196        attr_check a/c/f f &&
 197        attr_check a/i a/i &&
 198        attr_check subdir/a/i unspecified
 199
 200'
 201
 202test_done