t / t4034-diff-words.shon commit Merge branch 'cn/maint-rev-list-doc' into maint (0e20414)
   1#!/bin/sh
   2
   3test_description='word diff colors'
   4
   5. ./test-lib.sh
   6
   7cat >pre.simple <<-\EOF
   8        h(4)
   9
  10        a = b + c
  11EOF
  12cat >post.simple <<-\EOF
  13        h(4),hh[44]
  14
  15        a = b + c
  16
  17        aa = a
  18
  19        aeff = aeff * ( aaa )
  20EOF
  21cat >expect.letter-runs-are-words <<-\EOF
  22        <BOLD>diff --git a/pre b/post<RESET>
  23        <BOLD>index 330b04f..5ed8eff 100644<RESET>
  24        <BOLD>--- a/pre<RESET>
  25        <BOLD>+++ b/post<RESET>
  26        <CYAN>@@ -1,3 +1,7 @@<RESET>
  27        h(4),<GREEN>hh<RESET>[44]
  28
  29        a = b + c<RESET>
  30
  31        <GREEN>aa = a<RESET>
  32
  33        <GREEN>aeff = aeff * ( aaa<RESET> )
  34EOF
  35cat >expect.non-whitespace-is-word <<-\EOF
  36        <BOLD>diff --git a/pre b/post<RESET>
  37        <BOLD>index 330b04f..5ed8eff 100644<RESET>
  38        <BOLD>--- a/pre<RESET>
  39        <BOLD>+++ b/post<RESET>
  40        <CYAN>@@ -1,3 +1,7 @@<RESET>
  41        h(4)<GREEN>,hh[44]<RESET>
  42
  43        a = b + c<RESET>
  44
  45        <GREEN>aa = a<RESET>
  46
  47        <GREEN>aeff = aeff * ( aaa )<RESET>
  48EOF
  49
  50word_diff () {
  51        test_must_fail git diff --no-index "$@" pre post >output &&
  52        test_decode_color <output >output.decrypted &&
  53        test_cmp expect output.decrypted
  54}
  55
  56test_language_driver () {
  57        lang=$1
  58        test_expect_success "diff driver '$lang'" '
  59                cp "$TEST_DIRECTORY/t4034/'"$lang"'/pre" \
  60                        "$TEST_DIRECTORY/t4034/'"$lang"'/post" \
  61                        "$TEST_DIRECTORY/t4034/'"$lang"'/expect" . &&
  62                echo "* diff='"$lang"'" >.gitattributes &&
  63                word_diff --color-words
  64        '
  65}
  66
  67test_expect_success setup '
  68        git config diff.color.old red &&
  69        git config diff.color.new green &&
  70        git config diff.color.func magenta
  71'
  72
  73test_expect_success 'set up pre and post with runs of whitespace' '
  74        cp pre.simple pre &&
  75        cp post.simple post
  76'
  77
  78test_expect_success 'word diff with runs of whitespace' '
  79        cat >expect <<-\EOF &&
  80                <BOLD>diff --git a/pre b/post<RESET>
  81                <BOLD>index 330b04f..5ed8eff 100644<RESET>
  82                <BOLD>--- a/pre<RESET>
  83                <BOLD>+++ b/post<RESET>
  84                <CYAN>@@ -1,3 +1,7 @@<RESET>
  85                <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
  86
  87                a = b + c<RESET>
  88
  89                <GREEN>aa = a<RESET>
  90
  91                <GREEN>aeff = aeff * ( aaa )<RESET>
  92        EOF
  93        word_diff --color-words &&
  94        word_diff --word-diff=color &&
  95        word_diff --color --word-diff=color
  96'
  97
  98test_expect_success '--word-diff=porcelain' '
  99        sed 's/#.*$//' >expect <<-\EOF &&
 100                diff --git a/pre b/post
 101                index 330b04f..5ed8eff 100644
 102                --- a/pre
 103                +++ b/post
 104                @@ -1,3 +1,7 @@
 105                -h(4)
 106                +h(4),hh[44]
 107                ~
 108                 # significant space
 109                ~
 110                 a = b + c
 111                ~
 112                ~
 113                +aa = a
 114                ~
 115                ~
 116                +aeff = aeff * ( aaa )
 117                ~
 118        EOF
 119        word_diff --word-diff=porcelain
 120'
 121
 122test_expect_success '--word-diff=plain' '
 123        cat >expect <<-\EOF &&
 124                diff --git a/pre b/post
 125                index 330b04f..5ed8eff 100644
 126                --- a/pre
 127                +++ b/post
 128                @@ -1,3 +1,7 @@
 129                [-h(4)-]{+h(4),hh[44]+}
 130
 131                a = b + c
 132
 133                {+aa = a+}
 134
 135                {+aeff = aeff * ( aaa )+}
 136        EOF
 137        word_diff --word-diff=plain &&
 138        word_diff --word-diff=plain --no-color
 139'
 140
 141test_expect_success '--word-diff=plain --color' '
 142        cat >expect <<-\EOF &&
 143                <BOLD>diff --git a/pre b/post<RESET>
 144                <BOLD>index 330b04f..5ed8eff 100644<RESET>
 145                <BOLD>--- a/pre<RESET>
 146                <BOLD>+++ b/post<RESET>
 147                <CYAN>@@ -1,3 +1,7 @@<RESET>
 148                <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
 149
 150                a = b + c<RESET>
 151
 152                <GREEN>{+aa = a+}<RESET>
 153
 154                <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
 155        EOF
 156        word_diff --word-diff=plain --color
 157'
 158
 159test_expect_success 'word diff without context' '
 160        cat >expect <<-\EOF &&
 161                <BOLD>diff --git a/pre b/post<RESET>
 162                <BOLD>index 330b04f..5ed8eff 100644<RESET>
 163                <BOLD>--- a/pre<RESET>
 164                <BOLD>+++ b/post<RESET>
 165                <CYAN>@@ -1 +1 @@<RESET>
 166                <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
 167                <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
 168
 169                <GREEN>aa = a<RESET>
 170
 171                <GREEN>aeff = aeff * ( aaa )<RESET>
 172        EOF
 173        word_diff --color-words --unified=0
 174'
 175
 176test_expect_success 'word diff with a regular expression' '
 177        cp expect.letter-runs-are-words expect &&
 178        word_diff --color-words="[a-z]+"
 179'
 180
 181test_expect_success 'set up a diff driver' '
 182        git config diff.testdriver.wordRegex "[^[:space:]]" &&
 183        cat <<-\EOF >.gitattributes
 184                pre diff=testdriver
 185                post diff=testdriver
 186        EOF
 187'
 188
 189test_expect_success 'option overrides .gitattributes' '
 190        cp expect.letter-runs-are-words expect &&
 191        word_diff --color-words="[a-z]+"
 192'
 193
 194test_expect_success 'use regex supplied by driver' '
 195        cp expect.non-whitespace-is-word expect &&
 196        word_diff --color-words
 197'
 198
 199test_expect_success 'set up diff.wordRegex option' '
 200        git config diff.wordRegex "[[:alnum:]]+"
 201'
 202
 203test_expect_success 'command-line overrides config' '
 204        cp expect.letter-runs-are-words expect &&
 205        word_diff --color-words="[a-z]+"
 206'
 207
 208test_expect_success 'command-line overrides config: --word-diff-regex' '
 209        cat >expect <<-\EOF &&
 210                <BOLD>diff --git a/pre b/post<RESET>
 211                <BOLD>index 330b04f..5ed8eff 100644<RESET>
 212                <BOLD>--- a/pre<RESET>
 213                <BOLD>+++ b/post<RESET>
 214                <CYAN>@@ -1,3 +1,7 @@<RESET>
 215                h(4),<GREEN>{+hh+}<RESET>[44]
 216
 217                a = b + c<RESET>
 218
 219                <GREEN>{+aa = a+}<RESET>
 220
 221                <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
 222        EOF
 223        word_diff --color --word-diff-regex="[a-z]+"
 224'
 225
 226test_expect_success '.gitattributes override config' '
 227        cp expect.non-whitespace-is-word expect &&
 228        word_diff --color-words
 229'
 230
 231test_expect_success 'setup: remove diff driver regex' '
 232        test_might_fail git config --unset diff.testdriver.wordRegex
 233'
 234
 235test_expect_success 'use configured regex' '
 236        cat >expect <<-\EOF &&
 237                <BOLD>diff --git a/pre b/post<RESET>
 238                <BOLD>index 330b04f..5ed8eff 100644<RESET>
 239                <BOLD>--- a/pre<RESET>
 240                <BOLD>+++ b/post<RESET>
 241                <CYAN>@@ -1,3 +1,7 @@<RESET>
 242                h(4),<GREEN>hh[44<RESET>]
 243
 244                a = b + c<RESET>
 245
 246                <GREEN>aa = a<RESET>
 247
 248                <GREEN>aeff = aeff * ( aaa<RESET> )
 249        EOF
 250        word_diff --color-words
 251'
 252
 253test_expect_success 'test parsing words for newline' '
 254        echo "aaa (aaa)" >pre &&
 255        echo "aaa (aaa) aaa" >post &&
 256        cat >expect <<-\EOF &&
 257                <BOLD>diff --git a/pre b/post<RESET>
 258                <BOLD>index c29453b..be22f37 100644<RESET>
 259                <BOLD>--- a/pre<RESET>
 260                <BOLD>+++ b/post<RESET>
 261                <CYAN>@@ -1 +1 @@<RESET>
 262                aaa (aaa) <GREEN>aaa<RESET>
 263        EOF
 264        word_diff --color-words="a+"
 265'
 266
 267test_expect_success 'test when words are only removed at the end' '
 268        echo "(:" >pre &&
 269        echo "(" >post &&
 270        cat >expect <<-\EOF &&
 271                <BOLD>diff --git a/pre b/post<RESET>
 272                <BOLD>index 289cb9d..2d06f37 100644<RESET>
 273                <BOLD>--- a/pre<RESET>
 274                <BOLD>+++ b/post<RESET>
 275                <CYAN>@@ -1 +1 @@<RESET>
 276                (<RED>:<RESET>
 277        EOF
 278        word_diff --color-words=.
 279'
 280
 281test_expect_success '--word-diff=none' '
 282        echo "(:" >pre &&
 283        echo "(" >post &&
 284        cat >expect <<-\EOF &&
 285                diff --git a/pre b/post
 286                index 289cb9d..2d06f37 100644
 287                --- a/pre
 288                +++ b/post
 289                @@ -1 +1 @@
 290                -(:
 291                +(
 292        EOF
 293        word_diff --word-diff=plain --word-diff=none
 294'
 295
 296test_language_driver bibtex
 297test_language_driver cpp
 298test_language_driver csharp
 299test_language_driver fortran
 300test_language_driver html
 301test_language_driver java
 302test_language_driver matlab
 303test_language_driver objc
 304test_language_driver pascal
 305test_language_driver perl
 306test_language_driver php
 307test_language_driver python
 308test_language_driver ruby
 309test_language_driver tex
 310
 311test_expect_success 'word-diff with diff.sbe' '
 312        cat >expect <<-\EOF &&
 313        diff --git a/pre b/post
 314        index a1a53b5..bc8fe6d 100644
 315        --- a/pre
 316        +++ b/post
 317        @@ -1,3 +1,3 @@
 318        a
 319
 320        [-b-]{+c+}
 321        EOF
 322        cat >pre <<-\EOF &&
 323        a
 324
 325        b
 326        EOF
 327        cat >post <<-\EOF &&
 328        a
 329
 330        c
 331        EOF
 332        test_when_finished "git config --unset diff.suppress-blank-empty" &&
 333        git config diff.suppress-blank-empty true &&
 334        word_diff --word-diff=plain
 335'
 336
 337test_expect_success 'word-diff with no newline at EOF' '
 338        cat >expect <<-\EOF &&
 339        diff --git a/pre b/post
 340        index 7bf316e..3dd0303 100644
 341        --- a/pre
 342        +++ b/post
 343        @@ -1 +1 @@
 344        a a [-a-]{+ab+} a a
 345        EOF
 346        printf "%s" "a a a a a" >pre &&
 347        printf "%s" "a a ab a a" >post &&
 348        word_diff --word-diff=plain
 349'
 350
 351test_done