t / t3070-wildmatch.shon commit Merge branch 'jt/t5551-test-chunked' (d5df41c)
   1#!/bin/sh
   2
   3test_description='wildmatch tests'
   4
   5. ./test-lib.sh
   6
   7should_create_test_file() {
   8        file=$1
   9
  10        case $file in
  11        # `touch .` will succeed but obviously not do what we intend
  12        # here.
  13        ".")
  14                return 1
  15                ;;
  16        # We cannot create a file with an empty filename.
  17        "")
  18                return 1
  19                ;;
  20        # The tests that are testing that e.g. foo//bar is matched by
  21        # foo/*/bar can't be tested on filesystems since there's no
  22        # way we're getting a double slash.
  23        *//*)
  24                return 1
  25                ;;
  26        # When testing the difference between foo/bar and foo/bar/ we
  27        # can't test the latter.
  28        */)
  29                return 1
  30                ;;
  31        # On Windows, \ in paths is silently converted to /, which
  32        # would result in the "touch" below working, but the test
  33        # itself failing. See 6fd1106aa4 ("t3700: Skip a test with
  34        # backslashes in pathspec", 2009-03-13) for prior art and
  35        # details.
  36        *\\*)
  37                if ! test_have_prereq BSLASHPSPEC
  38                then
  39                        return 1
  40                fi
  41                # NOTE: The ;;& bash extension is not portable, so
  42                # this test needs to be at the end of the pattern
  43                # list.
  44                #
  45                # If we want to add more conditional returns we either
  46                # need a new case statement, or turn this whole thing
  47                # into a series of "if" tests.
  48                ;;
  49        esac
  50
  51
  52        # On Windows proper (i.e. not Cygwin) many file names which
  53        # under Cygwin would be emulated don't work.
  54        if test_have_prereq MINGW
  55        then
  56                case $file in
  57                " ")
  58                        # Files called " " are forbidden on Windows
  59                        return 1
  60                        ;;
  61                *\<*|*\>*|*:*|*\"*|*\|*|*\?*|*\**)
  62                        # Files with various special characters aren't
  63                        # allowed on Windows. Sourced from
  64                        # https://stackoverflow.com/a/31976060
  65                        return 1
  66                        ;;
  67                esac
  68        fi
  69
  70        return 0
  71}
  72
  73match_with_function() {
  74        text=$1
  75        pattern=$2
  76        match_expect=$3
  77        match_function=$4
  78
  79        if test "$match_expect" = 1
  80        then
  81                test_expect_success "$match_function: match '$text' '$pattern'" "
  82                        test-tool wildmatch $match_function '$text' '$pattern'
  83                "
  84        elif test "$match_expect" = 0
  85        then
  86                test_expect_success "$match_function: no match '$text' '$pattern'" "
  87                        test_must_fail test-tool wildmatch $match_function '$text' '$pattern'
  88                "
  89        else
  90                test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
  91        fi
  92
  93}
  94
  95match_with_ls_files() {
  96        text=$1
  97        pattern=$2
  98        match_expect=$3
  99        match_function=$4
 100        ls_files_args=$5
 101
 102        match_stdout_stderr_cmp="
 103                tr -d '\0' <actual.raw >actual &&
 104                test_must_be_empty actual.err &&
 105                test_cmp expect actual"
 106
 107        if test "$match_expect" = 'E'
 108        then
 109                if test -e .git/created_test_file
 110                then
 111                        test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match dies on '$pattern' '$text'" "
 112                                printf '%s' '$text' >expect &&
 113                                test_must_fail git$ls_files_args ls-files -z -- '$pattern'
 114                        "
 115                else
 116                        test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
 117                fi
 118        elif test "$match_expect" = 1
 119        then
 120                if test -e .git/created_test_file
 121                then
 122                        test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match '$pattern' '$text'" "
 123                                printf '%s' '$text' >expect &&
 124                                git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
 125                                $match_stdout_stderr_cmp
 126                        "
 127                else
 128                        test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
 129                fi
 130        elif test "$match_expect" = 0
 131        then
 132                if test -e .git/created_test_file
 133                then
 134                        test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): no match '$pattern' '$text'" "
 135                                >expect &&
 136                                git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
 137                                $match_stdout_stderr_cmp
 138                        "
 139                else
 140                        test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): no match skip '$pattern' '$text'" 'false'
 141                fi
 142        else
 143                test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
 144        fi
 145}
 146
 147match() {
 148        if test "$#" = 6
 149        then
 150                # When test-tool wildmatch and git ls-files produce the same
 151                # result.
 152                match_glob=$1
 153                match_file_glob=$match_glob
 154                match_iglob=$2
 155                match_file_iglob=$match_iglob
 156                match_pathmatch=$3
 157                match_file_pathmatch=$match_pathmatch
 158                match_pathmatchi=$4
 159                match_file_pathmatchi=$match_pathmatchi
 160                text=$5
 161                pattern=$6
 162        elif test "$#" = 10
 163        then
 164                match_glob=$1
 165                match_iglob=$2
 166                match_pathmatch=$3
 167                match_pathmatchi=$4
 168                match_file_glob=$5
 169                match_file_iglob=$6
 170                match_file_pathmatch=$7
 171                match_file_pathmatchi=$8
 172                text=$9
 173                pattern=${10}
 174        fi
 175
 176        test_expect_success EXPENSIVE_ON_WINDOWS 'cleanup after previous file test' '
 177                if test -e .git/created_test_file
 178                then
 179                        git reset &&
 180                        git clean -df
 181                fi
 182        '
 183
 184        printf '%s' "$text" >.git/expected_test_file
 185
 186        test_expect_success EXPENSIVE_ON_WINDOWS "setup match file test for $text" '
 187                file=$(cat .git/expected_test_file) &&
 188                if should_create_test_file "$file"
 189                then
 190                        dirs=${file%/*}
 191                        if test "$file" != "$dirs"
 192                        then
 193                                mkdir -p -- "$dirs" &&
 194                                touch -- "./$text"
 195                        else
 196                                touch -- "./$file"
 197                        fi &&
 198                        git add -A &&
 199                        printf "%s" "$file" >.git/created_test_file
 200                elif test -e .git/created_test_file
 201                then
 202                        rm .git/created_test_file
 203                fi
 204        '
 205
 206        # $1: Case sensitive glob match: test-tool wildmatch & ls-files
 207        match_with_function "$text" "$pattern" $match_glob "wildmatch"
 208        match_with_ls_files "$text" "$pattern" $match_file_glob "wildmatch" " --glob-pathspecs"
 209
 210        # $2: Case insensitive glob match: test-tool wildmatch & ls-files
 211        match_with_function "$text" "$pattern" $match_iglob "iwildmatch"
 212        match_with_ls_files "$text" "$pattern" $match_file_iglob "iwildmatch" " --glob-pathspecs --icase-pathspecs"
 213
 214        # $3: Case sensitive path match: test-tool wildmatch & ls-files
 215        match_with_function "$text" "$pattern" $match_pathmatch "pathmatch"
 216        match_with_ls_files "$text" "$pattern" $match_file_pathmatch "pathmatch" ""
 217
 218        # $4: Case insensitive path match: test-tool wildmatch & ls-files
 219        match_with_function "$text" "$pattern" $match_pathmatchi "ipathmatch"
 220        match_with_ls_files "$text" "$pattern" $match_file_pathmatchi "ipathmatch" " --icase-pathspecs"
 221}
 222
 223# Basic wildmatch features
 224match 1 1 1 1 foo foo
 225match 0 0 0 0 foo bar
 226match 1 1 1 1 '' ""
 227match 1 1 1 1 foo '???'
 228match 0 0 0 0 foo '??'
 229match 1 1 1 1 foo '*'
 230match 1 1 1 1 foo 'f*'
 231match 0 0 0 0 foo '*f'
 232match 1 1 1 1 foo '*foo*'
 233match 1 1 1 1 foobar '*ob*a*r*'
 234match 1 1 1 1 aaaaaaabababab '*ab'
 235match 1 1 1 1 'foo*' 'foo\*'
 236match 0 0 0 0 foobar 'foo\*bar'
 237match 1 1 1 1 'f\oo' 'f\\oo'
 238match 1 1 1 1 ball '*[al]?'
 239match 0 0 0 0 ten '[ten]'
 240match 1 1 1 1 ten '**[!te]'
 241match 0 0 0 0 ten '**[!ten]'
 242match 1 1 1 1 ten 't[a-g]n'
 243match 0 0 0 0 ten 't[!a-g]n'
 244match 1 1 1 1 ton 't[!a-g]n'
 245match 1 1 1 1 ton 't[^a-g]n'
 246match 1 1 1 1 'a]b' 'a[]]b'
 247match 1 1 1 1 a-b 'a[]-]b'
 248match 1 1 1 1 'a]b' 'a[]-]b'
 249match 0 0 0 0 aab 'a[]-]b'
 250match 1 1 1 1 aab 'a[]a-]b'
 251match 1 1 1 1 ']' ']'
 252
 253# Extended slash-matching features
 254match 0 0 1 1 'foo/baz/bar' 'foo*bar'
 255match 0 0 1 1 'foo/baz/bar' 'foo**bar'
 256match 1 1 1 1 'foobazbar' 'foo**bar'
 257match 1 1 1 1 'foo/baz/bar' 'foo/**/bar'
 258match 1 1 0 0 'foo/baz/bar' 'foo/**/**/bar'
 259match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/bar'
 260match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/**/bar'
 261match 1 1 0 0 'foo/bar' 'foo/**/bar'
 262match 1 1 0 0 'foo/bar' 'foo/**/**/bar'
 263match 0 0 1 1 'foo/bar' 'foo?bar'
 264match 0 0 1 1 'foo/bar' 'foo[/]bar'
 265match 0 0 1 1 'foo/bar' 'foo[^a-z]bar'
 266match 0 0 1 1 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
 267match 1 1 1 1 'foo-bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
 268match 1 1 0 0 'foo' '**/foo'
 269match 1 1 1 1 'XXX/foo' '**/foo'
 270match 1 1 1 1 'bar/baz/foo' '**/foo'
 271match 0 0 1 1 'bar/baz/foo' '*/foo'
 272match 0 0 1 1 'foo/bar/baz' '**/bar*'
 273match 1 1 1 1 'deep/foo/bar/baz' '**/bar/*'
 274match 0 0 1 1 'deep/foo/bar/baz/' '**/bar/*'
 275match 1 1 1 1 'deep/foo/bar/baz/' '**/bar/**'
 276match 0 0 0 0 'deep/foo/bar' '**/bar/*'
 277match 1 1 1 1 'deep/foo/bar/' '**/bar/**'
 278match 0 0 1 1 'foo/bar/baz' '**/bar**'
 279match 1 1 1 1 'foo/bar/baz/x' '*/bar/**'
 280match 0 0 1 1 'deep/foo/bar/baz/x' '*/bar/**'
 281match 1 1 1 1 'deep/foo/bar/baz/x' '**/bar/*/*'
 282
 283# Various additional tests
 284match 0 0 0 0 'acrt' 'a[c-c]st'
 285match 1 1 1 1 'acrt' 'a[c-c]rt'
 286match 0 0 0 0 ']' '[!]-]'
 287match 1 1 1 1 'a' '[!]-]'
 288match 0 0 0 0 '' '\'
 289match 0 0 0 0 \
 290      1 1 1 1 '\' '\'
 291match 0 0 0 0 'XXX/\' '*/\'
 292match 1 1 1 1 'XXX/\' '*/\\'
 293match 1 1 1 1 'foo' 'foo'
 294match 1 1 1 1 '@foo' '@foo'
 295match 0 0 0 0 'foo' '@foo'
 296match 1 1 1 1 '[ab]' '\[ab]'
 297match 1 1 1 1 '[ab]' '[[]ab]'
 298match 1 1 1 1 '[ab]' '[[:]ab]'
 299match 0 0 0 0 '[ab]' '[[::]ab]'
 300match 1 1 1 1 '[ab]' '[[:digit]ab]'
 301match 1 1 1 1 '[ab]' '[\[:]ab]'
 302match 1 1 1 1 '?a?b' '\??\?b'
 303match 1 1 1 1 'abc' '\a\b\c'
 304match 0 0 0 0 \
 305      E E E E 'foo' ''
 306match 1 1 1 1 'foo/bar/baz/to' '**/t[o]'
 307
 308# Character class tests
 309match 1 1 1 1 'a1B' '[[:alpha:]][[:digit:]][[:upper:]]'
 310match 0 1 0 1 'a' '[[:digit:][:upper:][:space:]]'
 311match 1 1 1 1 'A' '[[:digit:][:upper:][:space:]]'
 312match 1 1 1 1 '1' '[[:digit:][:upper:][:space:]]'
 313match 0 0 0 0 '1' '[[:digit:][:upper:][:spaci:]]'
 314match 1 1 1 1 ' ' '[[:digit:][:upper:][:space:]]'
 315match 0 0 0 0 '.' '[[:digit:][:upper:][:space:]]'
 316match 1 1 1 1 '.' '[[:digit:][:punct:][:space:]]'
 317match 1 1 1 1 '5' '[[:xdigit:]]'
 318match 1 1 1 1 'f' '[[:xdigit:]]'
 319match 1 1 1 1 'D' '[[:xdigit:]]'
 320match 1 1 1 1 '_' '[[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:xdigit:]]'
 321match 1 1 1 1 '.' '[^[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:lower:][:space:][:upper:][:xdigit:]]'
 322match 1 1 1 1 '5' '[a-c[:digit:]x-z]'
 323match 1 1 1 1 'b' '[a-c[:digit:]x-z]'
 324match 1 1 1 1 'y' '[a-c[:digit:]x-z]'
 325match 0 0 0 0 'q' '[a-c[:digit:]x-z]'
 326
 327# Additional tests, including some malformed wildmatch patterns
 328match 1 1 1 1 ']' '[\\-^]'
 329match 0 0 0 0 '[' '[\\-^]'
 330match 1 1 1 1 '-' '[\-_]'
 331match 1 1 1 1 ']' '[\]]'
 332match 0 0 0 0 '\]' '[\]]'
 333match 0 0 0 0 '\' '[\]]'
 334match 0 0 0 0 'ab' 'a[]b'
 335match 0 0 0 0 \
 336      1 1 1 1 'a[]b' 'a[]b'
 337match 0 0 0 0 \
 338      1 1 1 1 'ab[' 'ab['
 339match 0 0 0 0 'ab' '[!'
 340match 0 0 0 0 'ab' '[-'
 341match 1 1 1 1 '-' '[-]'
 342match 0 0 0 0 '-' '[a-'
 343match 0 0 0 0 '-' '[!a-'
 344match 1 1 1 1 '-' '[--A]'
 345match 1 1 1 1 '5' '[--A]'
 346match 1 1 1 1 ' ' '[ --]'
 347match 1 1 1 1 '$' '[ --]'
 348match 1 1 1 1 '-' '[ --]'
 349match 0 0 0 0 '0' '[ --]'
 350match 1 1 1 1 '-' '[---]'
 351match 1 1 1 1 '-' '[------]'
 352match 0 0 0 0 'j' '[a-e-n]'
 353match 1 1 1 1 '-' '[a-e-n]'
 354match 1 1 1 1 'a' '[!------]'
 355match 0 0 0 0 '[' '[]-a]'
 356match 1 1 1 1 '^' '[]-a]'
 357match 0 0 0 0 '^' '[!]-a]'
 358match 1 1 1 1 '[' '[!]-a]'
 359match 1 1 1 1 '^' '[a^bc]'
 360match 1 1 1 1 '-b]' '[a-]b]'
 361match 0 0 0 0 '\' '[\]'
 362match 1 1 1 1 '\' '[\\]'
 363match 0 0 0 0 '\' '[!\\]'
 364match 1 1 1 1 'G' '[A-\\]'
 365match 0 0 0 0 'aaabbb' 'b*a'
 366match 0 0 0 0 'aabcaa' '*ba*'
 367match 1 1 1 1 ',' '[,]'
 368match 1 1 1 1 ',' '[\\,]'
 369match 1 1 1 1 '\' '[\\,]'
 370match 1 1 1 1 '-' '[,-.]'
 371match 0 0 0 0 '+' '[,-.]'
 372match 0 0 0 0 '-.]' '[,-.]'
 373match 1 1 1 1 '2' '[\1-\3]'
 374match 1 1 1 1 '3' '[\1-\3]'
 375match 0 0 0 0 '4' '[\1-\3]'
 376match 1 1 1 1 '\' '[[-\]]'
 377match 1 1 1 1 '[' '[[-\]]'
 378match 1 1 1 1 ']' '[[-\]]'
 379match 0 0 0 0 '-' '[[-\]]'
 380
 381# Test recursion
 382match 1 1 1 1 '-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
 383match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
 384match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
 385match 1 1 1 1 'XXX/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
 386match 0 0 0 0 'XXX/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
 387match 1 1 1 1 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt' '**/*a*b*g*n*t'
 388match 0 0 0 0 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txtz' '**/*a*b*g*n*t'
 389match 0 0 0 0 foo '*/*/*'
 390match 0 0 0 0 foo/bar '*/*/*'
 391match 1 1 1 1 foo/bba/arr '*/*/*'
 392match 0 0 1 1 foo/bb/aa/rr '*/*/*'
 393match 1 1 1 1 foo/bb/aa/rr '**/**/**'
 394match 1 1 1 1 abcXdefXghi '*X*i'
 395match 0 0 1 1 ab/cXd/efXg/hi '*X*i'
 396match 1 1 1 1 ab/cXd/efXg/hi '*/*X*/*/*i'
 397match 1 1 1 1 ab/cXd/efXg/hi '**/*X*/**/*i'
 398
 399# Extra pathmatch tests
 400match 0 0 0 0 foo fo
 401match 1 1 1 1 foo/bar foo/bar
 402match 1 1 1 1 foo/bar 'foo/*'
 403match 0 0 1 1 foo/bba/arr 'foo/*'
 404match 1 1 1 1 foo/bba/arr 'foo/**'
 405match 0 0 1 1 foo/bba/arr 'foo*'
 406match 0 0 1 1 \
 407      1 1 1 1 foo/bba/arr 'foo**'
 408match 0 0 1 1 foo/bba/arr 'foo/*arr'
 409match 0 0 1 1 foo/bba/arr 'foo/**arr'
 410match 0 0 0 0 foo/bba/arr 'foo/*z'
 411match 0 0 0 0 foo/bba/arr 'foo/**z'
 412match 0 0 1 1 foo/bar 'foo?bar'
 413match 0 0 1 1 foo/bar 'foo[/]bar'
 414match 0 0 1 1 foo/bar 'foo[^a-z]bar'
 415match 0 0 1 1 ab/cXd/efXg/hi '*Xg*i'
 416
 417# Extra case-sensitivity tests
 418match 0 1 0 1 'a' '[A-Z]'
 419match 1 1 1 1 'A' '[A-Z]'
 420match 0 1 0 1 'A' '[a-z]'
 421match 1 1 1 1 'a' '[a-z]'
 422match 0 1 0 1 'a' '[[:upper:]]'
 423match 1 1 1 1 'A' '[[:upper:]]'
 424match 0 1 0 1 'A' '[[:lower:]]'
 425match 1 1 1 1 'a' '[[:lower:]]'
 426match 0 1 0 1 'A' '[B-Za]'
 427match 1 1 1 1 'a' '[B-Za]'
 428match 0 1 0 1 'A' '[B-a]'
 429match 1 1 1 1 'a' '[B-a]'
 430match 0 1 0 1 'z' '[Z-y]'
 431match 1 1 1 1 'Z' '[Z-y]'
 432
 433test_done