t / t4019-diff-wserror.shon commit Merge branch 'tc/format-patch-p' (33935dc)
   1#!/bin/sh
   2
   3test_description='diff whitespace error detection'
   4
   5. ./test-lib.sh
   6
   7test_expect_success setup '
   8
   9        git config diff.color.whitespace "blue reverse" &&
  10        >F &&
  11        git add F &&
  12        echo "         Eight SP indent" >>F &&
  13        echo "  HT and SP indent" >>F &&
  14        echo "With trailing SP " >>F &&
  15        echo "Carriage ReturnQ" | tr Q "\015" >>F &&
  16        echo "No problem" >>F &&
  17        echo >>F
  18
  19'
  20
  21blue_grep='7;34m' ;# ESC [ 7 ; 3 4 m
  22
  23printf "\033[%s" "$blue_grep" >check-grep
  24if (grep "$blue_grep" <check-grep | grep "$blue_grep") >/dev/null 2>&1
  25then
  26        grep_a=grep
  27elif (grep -a "$blue_grep" <check-grep | grep -a "$blue_grep") >/dev/null 2>&1
  28then
  29        grep_a='grep -a'
  30else
  31        grep_a=grep ;# expected to fail...
  32fi
  33rm -f check-grep
  34
  35prepare_output () {
  36        git diff --color >output
  37        $grep_a "$blue_grep" output >error
  38        $grep_a -v "$blue_grep" output >normal
  39        return 0
  40}
  41
  42test_expect_success default '
  43
  44        prepare_output &&
  45
  46        grep Eight normal >/dev/null &&
  47        grep HT error >/dev/null &&
  48        grep With error >/dev/null &&
  49        grep Return error >/dev/null &&
  50        grep No normal >/dev/null
  51
  52'
  53
  54test_expect_success 'without -trail' '
  55
  56        git config core.whitespace -trail &&
  57        prepare_output &&
  58
  59        grep Eight normal >/dev/null &&
  60        grep HT error >/dev/null &&
  61        grep With normal >/dev/null &&
  62        grep Return normal >/dev/null &&
  63        grep No normal >/dev/null
  64
  65'
  66
  67test_expect_success 'without -trail (attribute)' '
  68
  69        test_might_fail git config --unset core.whitespace &&
  70        echo "F whitespace=-trail" >.gitattributes &&
  71        prepare_output &&
  72
  73        grep Eight normal >/dev/null &&
  74        grep HT error >/dev/null &&
  75        grep With normal >/dev/null &&
  76        grep Return normal >/dev/null &&
  77        grep No normal >/dev/null
  78
  79'
  80
  81test_expect_success 'without -space' '
  82
  83        rm -f .gitattributes &&
  84        git config core.whitespace -space &&
  85        prepare_output &&
  86
  87        grep Eight normal >/dev/null &&
  88        grep HT normal >/dev/null &&
  89        grep With error >/dev/null &&
  90        grep Return error >/dev/null &&
  91        grep No normal >/dev/null
  92
  93'
  94
  95test_expect_success 'without -space (attribute)' '
  96
  97        test_might_fail git config --unset core.whitespace &&
  98        echo "F whitespace=-space" >.gitattributes &&
  99        prepare_output &&
 100
 101        grep Eight normal >/dev/null &&
 102        grep HT normal >/dev/null &&
 103        grep With error >/dev/null &&
 104        grep Return error >/dev/null &&
 105        grep No normal >/dev/null
 106
 107'
 108
 109test_expect_success 'with indent-non-tab only' '
 110
 111        rm -f .gitattributes &&
 112        git config core.whitespace indent,-trailing,-space &&
 113        prepare_output &&
 114
 115        grep Eight error >/dev/null &&
 116        grep HT normal >/dev/null &&
 117        grep With normal >/dev/null &&
 118        grep Return normal >/dev/null &&
 119        grep No normal >/dev/null
 120
 121'
 122
 123test_expect_success 'with indent-non-tab only (attribute)' '
 124
 125        test_might_fail git config --unset core.whitespace &&
 126        echo "F whitespace=indent,-trailing,-space" >.gitattributes &&
 127        prepare_output &&
 128
 129        grep Eight error >/dev/null &&
 130        grep HT normal >/dev/null &&
 131        grep With normal >/dev/null &&
 132        grep Return normal >/dev/null &&
 133        grep No normal >/dev/null
 134
 135'
 136
 137test_expect_success 'with cr-at-eol' '
 138
 139        rm -f .gitattributes &&
 140        git config core.whitespace cr-at-eol &&
 141        prepare_output &&
 142
 143        grep Eight normal >/dev/null &&
 144        grep HT error >/dev/null &&
 145        grep With error >/dev/null &&
 146        grep Return normal >/dev/null &&
 147        grep No normal >/dev/null
 148
 149'
 150
 151test_expect_success 'with cr-at-eol (attribute)' '
 152
 153        test_might_fail git config --unset core.whitespace &&
 154        echo "F whitespace=trailing,cr-at-eol" >.gitattributes &&
 155        prepare_output &&
 156
 157        grep Eight normal >/dev/null &&
 158        grep HT error >/dev/null &&
 159        grep With error >/dev/null &&
 160        grep Return normal >/dev/null &&
 161        grep No normal >/dev/null
 162
 163'
 164
 165test_expect_success 'trailing empty lines (1)' '
 166
 167        rm -f .gitattributes &&
 168        test_must_fail git diff --check >output &&
 169        grep "new blank line at" output &&
 170        grep "trailing whitespace" output
 171
 172'
 173
 174test_expect_success 'trailing empty lines (2)' '
 175
 176        echo "F -whitespace" >.gitattributes &&
 177        git diff --check >output &&
 178        ! test -s output
 179
 180'
 181
 182test_expect_success 'checkdiff shows correct line number for trailing blank lines' '
 183
 184        printf "a\nb\n" > G &&
 185        git add G &&
 186        printf "x\nx\nx\na\nb\nc\n\n" > G &&
 187        [ "$(git diff --check -- G)" = "G:7: new blank line at EOF." ]
 188
 189'
 190
 191test_expect_success 'do not color trailing cr in context' '
 192        test_might_fail git config --unset core.whitespace &&
 193        rm -f .gitattributes &&
 194        echo AAAQ | tr Q "\015" >G &&
 195        git add G &&
 196        echo BBBQ | tr Q "\015" >>G &&
 197        git diff --color G | tr "\015" Q >output &&
 198        grep "BBB.*${blue_grep}Q" output &&
 199        grep "AAA.*\[mQ" output
 200
 201'
 202
 203test_expect_success 'color new trailing blank lines' '
 204        { echo a; echo b; echo; echo; } >x &&
 205        git add x &&
 206        { echo a; echo; echo; echo; echo c; echo; echo; echo; echo; } >x &&
 207        git diff --color x >output &&
 208        cnt=$($grep_a "${blue_grep}" output | wc -l) &&
 209        test $cnt = 2
 210'
 211
 212test_done