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