8345c8a72a46bc781c503de21c832261c1d2b256
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano
   4#
   5
   6test_description='Test the very basics part #1.
   7
   8The rest of the test suite does not check the basic operation of git
   9plumbing commands to work very carefully.  Their job is to concentrate
  10on tricky features that caused bugs in the past to detect regression.
  11
  12This test runs very basic features, like registering things in cache,
  13writing tree, etc.
  14
  15Note that this test *deliberately* hard-codes many expected object
  16IDs.  When object ID computation changes, like in the previous case of
  17swapping compression and hashing order, the person who is making the
  18modification *should* take notice and update the test vectors here.
  19'
  20
  21. ./test-lib.sh
  22
  23################################################################
  24# git init has been done in an empty repository.
  25# make sure it is empty.
  26
  27test_expect_success '.git/objects should be empty after git init in an empty repo' '
  28        find .git/objects -type f -print >should-be-empty &&
  29        test_line_count = 0 should-be-empty
  30'
  31
  32# also it should have 2 subdirectories; no fan-out anymore, pack, and info.
  33# 3 is counting "objects" itself
  34test_expect_success '.git/objects should have 3 subdirectories' '
  35        find .git/objects -type d -print >full-of-directories &&
  36        test_line_count = 3 full-of-directories
  37'
  38
  39################################################################
  40# Test harness
  41test_expect_success 'success is reported like this' '
  42        :
  43'
  44
  45_run_sub_test_lib_test_common () {
  46        neg="$1" name="$2" descr="$3" # stdin is the body of the test code
  47        shift 3
  48        mkdir "$name" &&
  49        (
  50                # Pretend we're not running under a test harness, whether we
  51                # are or not. The test-lib output depends on the setting of
  52                # this variable, so we need a stable setting under which to run
  53                # the sub-test.
  54                sane_unset HARNESS_ACTIVE &&
  55                cd "$name" &&
  56                cat >"$name.sh" <<-EOF &&
  57                #!$SHELL_PATH
  58
  59                test_description='$descr (run in sub test-lib)
  60
  61                This is run in a sub test-lib so that we do not get incorrect
  62                passing metrics
  63                '
  64
  65                # Point to the t/test-lib.sh, which isn't in ../ as usual
  66                . "\$TEST_DIRECTORY"/test-lib.sh
  67                EOF
  68                cat >>"$name.sh" &&
  69                chmod +x "$name.sh" &&
  70                export TEST_DIRECTORY &&
  71                TEST_OUTPUT_DIRECTORY=$(pwd) &&
  72                export TEST_OUTPUT_DIRECTORY &&
  73                if test -z "$neg"
  74                then
  75                        ./"$name.sh" "$@" >out 2>err
  76                else
  77                        !  ./"$name.sh" "$@" >out 2>err
  78                fi
  79        )
  80}
  81
  82run_sub_test_lib_test () {
  83        _run_sub_test_lib_test_common '' "$@"
  84}
  85
  86run_sub_test_lib_test_err () {
  87        _run_sub_test_lib_test_common '!' "$@"
  88}
  89
  90check_sub_test_lib_test () {
  91        name="$1" # stdin is the expected output from the test
  92        (
  93                cd "$name" &&
  94                ! test -s err &&
  95                sed -e 's/^> //' -e 's/Z$//' >expect &&
  96                test_cmp expect out
  97        )
  98}
  99
 100check_sub_test_lib_test_err () {
 101        name="$1" # stdin is the expected output output from the test
 102        # expected error output is in descriptior 3
 103        (
 104                cd "$name" &&
 105                sed -e 's/^> //' -e 's/Z$//' >expect.out &&
 106                test_cmp expect.out out &&
 107                sed -e 's/^> //' -e 's/Z$//' <&3 >expect.err &&
 108                test_cmp expect.err err
 109        )
 110}
 111
 112test_expect_success 'pretend we have a fully passing test suite' "
 113        run_sub_test_lib_test full-pass '3 passing tests' <<-\\EOF &&
 114        for i in 1 2 3
 115        do
 116                test_expect_success \"passing test #\$i\" 'true'
 117        done
 118        test_done
 119        EOF
 120        check_sub_test_lib_test full-pass <<-\\EOF
 121        > ok 1 - passing test #1
 122        > ok 2 - passing test #2
 123        > ok 3 - passing test #3
 124        > # passed all 3 test(s)
 125        > 1..3
 126        EOF
 127"
 128
 129test_expect_success 'pretend we have a partially passing test suite' "
 130        test_must_fail run_sub_test_lib_test \
 131                partial-pass '2/3 tests passing' <<-\\EOF &&
 132        test_expect_success 'passing test #1' 'true'
 133        test_expect_success 'failing test #2' 'false'
 134        test_expect_success 'passing test #3' 'true'
 135        test_done
 136        EOF
 137        check_sub_test_lib_test partial-pass <<-\\EOF
 138        > ok 1 - passing test #1
 139        > not ok 2 - failing test #2
 140        #       false
 141        > ok 3 - passing test #3
 142        > # failed 1 among 3 test(s)
 143        > 1..3
 144        EOF
 145"
 146
 147test_expect_success 'pretend we have a known breakage' "
 148        run_sub_test_lib_test failing-todo 'A failing TODO test' <<-\\EOF &&
 149        test_expect_success 'passing test' 'true'
 150        test_expect_failure 'pretend we have a known breakage' 'false'
 151        test_done
 152        EOF
 153        check_sub_test_lib_test failing-todo <<-\\EOF
 154        > ok 1 - passing test
 155        > not ok 2 - pretend we have a known breakage # TODO known breakage
 156        > # still have 1 known breakage(s)
 157        > # passed all remaining 1 test(s)
 158        > 1..2
 159        EOF
 160"
 161
 162test_expect_success 'pretend we have fixed a known breakage' "
 163        run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
 164        test_expect_failure 'pretend we have fixed a known breakage' 'true'
 165        test_done
 166        EOF
 167        check_sub_test_lib_test passing-todo <<-\\EOF
 168        > ok 1 - pretend we have fixed a known breakage # TODO known breakage vanished
 169        > # 1 known breakage(s) vanished; please update test(s)
 170        > 1..1
 171        EOF
 172"
 173
 174test_expect_success 'pretend we have fixed one of two known breakages (run in sub test-lib)' "
 175        run_sub_test_lib_test partially-passing-todos \
 176                '2 TODO tests, one passing' <<-\\EOF &&
 177        test_expect_failure 'pretend we have a known breakage' 'false'
 178        test_expect_success 'pretend we have a passing test' 'true'
 179        test_expect_failure 'pretend we have fixed another known breakage' 'true'
 180        test_done
 181        EOF
 182        check_sub_test_lib_test partially-passing-todos <<-\\EOF
 183        > not ok 1 - pretend we have a known breakage # TODO known breakage
 184        > ok 2 - pretend we have a passing test
 185        > ok 3 - pretend we have fixed another known breakage # TODO known breakage vanished
 186        > # 1 known breakage(s) vanished; please update test(s)
 187        > # still have 1 known breakage(s)
 188        > # passed all remaining 1 test(s)
 189        > 1..3
 190        EOF
 191"
 192
 193test_expect_success 'pretend we have a pass, fail, and known breakage' "
 194        test_must_fail run_sub_test_lib_test \
 195                mixed-results1 'mixed results #1' <<-\\EOF &&
 196        test_expect_success 'passing test' 'true'
 197        test_expect_success 'failing test' 'false'
 198        test_expect_failure 'pretend we have a known breakage' 'false'
 199        test_done
 200        EOF
 201        check_sub_test_lib_test mixed-results1 <<-\\EOF
 202        > ok 1 - passing test
 203        > not ok 2 - failing test
 204        > #     false
 205        > not ok 3 - pretend we have a known breakage # TODO known breakage
 206        > # still have 1 known breakage(s)
 207        > # failed 1 among remaining 2 test(s)
 208        > 1..3
 209        EOF
 210"
 211
 212test_expect_success 'pretend we have a mix of all possible results' "
 213        test_must_fail run_sub_test_lib_test \
 214                mixed-results2 'mixed results #2' <<-\\EOF &&
 215        test_expect_success 'passing test' 'true'
 216        test_expect_success 'passing test' 'true'
 217        test_expect_success 'passing test' 'true'
 218        test_expect_success 'passing test' 'true'
 219        test_expect_success 'failing test' 'false'
 220        test_expect_success 'failing test' 'false'
 221        test_expect_success 'failing test' 'false'
 222        test_expect_failure 'pretend we have a known breakage' 'false'
 223        test_expect_failure 'pretend we have a known breakage' 'false'
 224        test_expect_failure 'pretend we have fixed a known breakage' 'true'
 225        test_done
 226        EOF
 227        check_sub_test_lib_test mixed-results2 <<-\\EOF
 228        > ok 1 - passing test
 229        > ok 2 - passing test
 230        > ok 3 - passing test
 231        > ok 4 - passing test
 232        > not ok 5 - failing test
 233        > #     false
 234        > not ok 6 - failing test
 235        > #     false
 236        > not ok 7 - failing test
 237        > #     false
 238        > not ok 8 - pretend we have a known breakage # TODO known breakage
 239        > not ok 9 - pretend we have a known breakage # TODO known breakage
 240        > ok 10 - pretend we have fixed a known breakage # TODO known breakage vanished
 241        > # 1 known breakage(s) vanished; please update test(s)
 242        > # still have 2 known breakage(s)
 243        > # failed 3 among remaining 7 test(s)
 244        > 1..10
 245        EOF
 246"
 247
 248test_expect_success 'test --verbose' '
 249        test_must_fail run_sub_test_lib_test \
 250                test-verbose "test verbose" --verbose <<-\EOF &&
 251        test_expect_success "passing test" true
 252        test_expect_success "test with output" "echo foo"
 253        test_expect_success "failing test" false
 254        test_done
 255        EOF
 256        mv test-verbose/out test-verbose/out+
 257        grep -v "^Initialized empty" test-verbose/out+ >test-verbose/out &&
 258        check_sub_test_lib_test test-verbose <<-\EOF
 259        > expecting success: true
 260        > ok 1 - passing test
 261        > Z
 262        > expecting success: echo foo
 263        > foo
 264        > ok 2 - test with output
 265        > Z
 266        > expecting success: false
 267        > not ok 3 - failing test
 268        > #     false
 269        > Z
 270        > # failed 1 among 3 test(s)
 271        > 1..3
 272        EOF
 273'
 274
 275test_expect_success 'test --verbose-only' '
 276        test_must_fail run_sub_test_lib_test \
 277                test-verbose-only-2 "test verbose-only=2" \
 278                --verbose-only=2 <<-\EOF &&
 279        test_expect_success "passing test" true
 280        test_expect_success "test with output" "echo foo"
 281        test_expect_success "failing test" false
 282        test_done
 283        EOF
 284        check_sub_test_lib_test test-verbose-only-2 <<-\EOF
 285        > ok 1 - passing test
 286        > Z
 287        > expecting success: echo foo
 288        > foo
 289        > ok 2 - test with output
 290        > Z
 291        > not ok 3 - failing test
 292        > #     false
 293        > # failed 1 among 3 test(s)
 294        > 1..3
 295        EOF
 296'
 297
 298test_expect_success 'GIT_SKIP_TESTS' "
 299        GIT_SKIP_TESTS='git.2' \
 300                run_sub_test_lib_test git-skip-tests-basic \
 301                'GIT_SKIP_TESTS' <<-\\EOF &&
 302        for i in 1 2 3
 303        do
 304                test_expect_success \"passing test #\$i\" 'true'
 305        done
 306        test_done
 307        EOF
 308        check_sub_test_lib_test git-skip-tests-basic <<-\\EOF
 309        > ok 1 - passing test #1
 310        > ok 2 # skip passing test #2 (GIT_SKIP_TESTS)
 311        > ok 3 - passing test #3
 312        > # passed all 3 test(s)
 313        > 1..3
 314        EOF
 315"
 316
 317test_expect_success 'GIT_SKIP_TESTS several tests' "
 318        GIT_SKIP_TESTS='git.2 git.5' \
 319                run_sub_test_lib_test git-skip-tests-several \
 320                'GIT_SKIP_TESTS several tests' <<-\\EOF &&
 321        for i in 1 2 3 4 5 6
 322        do
 323                test_expect_success \"passing test #\$i\" 'true'
 324        done
 325        test_done
 326        EOF
 327        check_sub_test_lib_test git-skip-tests-several <<-\\EOF
 328        > ok 1 - passing test #1
 329        > ok 2 # skip passing test #2 (GIT_SKIP_TESTS)
 330        > ok 3 - passing test #3
 331        > ok 4 - passing test #4
 332        > ok 5 # skip passing test #5 (GIT_SKIP_TESTS)
 333        > ok 6 - passing test #6
 334        > # passed all 6 test(s)
 335        > 1..6
 336        EOF
 337"
 338
 339test_expect_success 'GIT_SKIP_TESTS sh pattern' "
 340        GIT_SKIP_TESTS='git.[2-5]' \
 341                run_sub_test_lib_test git-skip-tests-sh-pattern \
 342                'GIT_SKIP_TESTS sh pattern' <<-\\EOF &&
 343        for i in 1 2 3 4 5 6
 344        do
 345                test_expect_success \"passing test #\$i\" 'true'
 346        done
 347        test_done
 348        EOF
 349        check_sub_test_lib_test git-skip-tests-sh-pattern <<-\\EOF
 350        > ok 1 - passing test #1
 351        > ok 2 # skip passing test #2 (GIT_SKIP_TESTS)
 352        > ok 3 # skip passing test #3 (GIT_SKIP_TESTS)
 353        > ok 4 # skip passing test #4 (GIT_SKIP_TESTS)
 354        > ok 5 # skip passing test #5 (GIT_SKIP_TESTS)
 355        > ok 6 - passing test #6
 356        > # passed all 6 test(s)
 357        > 1..6
 358        EOF
 359"
 360
 361test_expect_success '--run basic' "
 362        run_sub_test_lib_test run-basic \
 363                '--run basic' --run='1 3 5' <<-\\EOF &&
 364        for i in 1 2 3 4 5 6
 365        do
 366                test_expect_success \"passing test #\$i\" 'true'
 367        done
 368        test_done
 369        EOF
 370        check_sub_test_lib_test run-basic <<-\\EOF
 371        > ok 1 - passing test #1
 372        > ok 2 # skip passing test #2 (--run)
 373        > ok 3 - passing test #3
 374        > ok 4 # skip passing test #4 (--run)
 375        > ok 5 - passing test #5
 376        > ok 6 # skip passing test #6 (--run)
 377        > # passed all 6 test(s)
 378        > 1..6
 379        EOF
 380"
 381
 382test_expect_success '--run with a range' "
 383        run_sub_test_lib_test run-range \
 384                '--run with a range' --run='1-3' <<-\\EOF &&
 385        for i in 1 2 3 4 5 6
 386        do
 387                test_expect_success \"passing test #\$i\" 'true'
 388        done
 389        test_done
 390        EOF
 391        check_sub_test_lib_test run-range <<-\\EOF
 392        > ok 1 - passing test #1
 393        > ok 2 - passing test #2
 394        > ok 3 - passing test #3
 395        > ok 4 # skip passing test #4 (--run)
 396        > ok 5 # skip passing test #5 (--run)
 397        > ok 6 # skip passing test #6 (--run)
 398        > # passed all 6 test(s)
 399        > 1..6
 400        EOF
 401"
 402
 403test_expect_success '--run with two ranges' "
 404        run_sub_test_lib_test run-two-ranges \
 405                '--run with two ranges' --run='1-2 5-6' <<-\\EOF &&
 406        for i in 1 2 3 4 5 6
 407        do
 408                test_expect_success \"passing test #\$i\" 'true'
 409        done
 410        test_done
 411        EOF
 412        check_sub_test_lib_test run-two-ranges <<-\\EOF
 413        > ok 1 - passing test #1
 414        > ok 2 - passing test #2
 415        > ok 3 # skip passing test #3 (--run)
 416        > ok 4 # skip passing test #4 (--run)
 417        > ok 5 - passing test #5
 418        > ok 6 - passing test #6
 419        > # passed all 6 test(s)
 420        > 1..6
 421        EOF
 422"
 423
 424test_expect_success '--run with a left open range' "
 425        run_sub_test_lib_test run-left-open-range \
 426                '--run with a left open range' --run='-3' <<-\\EOF &&
 427        for i in 1 2 3 4 5 6
 428        do
 429                test_expect_success \"passing test #\$i\" 'true'
 430        done
 431        test_done
 432        EOF
 433        check_sub_test_lib_test run-left-open-range <<-\\EOF
 434        > ok 1 - passing test #1
 435        > ok 2 - passing test #2
 436        > ok 3 - passing test #3
 437        > ok 4 # skip passing test #4 (--run)
 438        > ok 5 # skip passing test #5 (--run)
 439        > ok 6 # skip passing test #6 (--run)
 440        > # passed all 6 test(s)
 441        > 1..6
 442        EOF
 443"
 444
 445test_expect_success '--run with a right open range' "
 446        run_sub_test_lib_test run-right-open-range \
 447                '--run with a right open range' --run='4-' <<-\\EOF &&
 448        for i in 1 2 3 4 5 6
 449        do
 450                test_expect_success \"passing test #\$i\" 'true'
 451        done
 452        test_done
 453        EOF
 454        check_sub_test_lib_test run-right-open-range <<-\\EOF
 455        > ok 1 # skip passing test #1 (--run)
 456        > ok 2 # skip passing test #2 (--run)
 457        > ok 3 # skip passing test #3 (--run)
 458        > ok 4 - passing test #4
 459        > ok 5 - passing test #5
 460        > ok 6 - passing test #6
 461        > # passed all 6 test(s)
 462        > 1..6
 463        EOF
 464"
 465
 466test_expect_success '--run with basic negation' "
 467        run_sub_test_lib_test run-basic-neg \
 468                '--run with basic negation' --run='"'!3'"' <<-\\EOF &&
 469        for i in 1 2 3 4 5 6
 470        do
 471                test_expect_success \"passing test #\$i\" 'true'
 472        done
 473        test_done
 474        EOF
 475        check_sub_test_lib_test run-basic-neg <<-\\EOF
 476        > ok 1 - passing test #1
 477        > ok 2 - passing test #2
 478        > ok 3 # skip passing test #3 (--run)
 479        > ok 4 - passing test #4
 480        > ok 5 - passing test #5
 481        > ok 6 - passing test #6
 482        > # passed all 6 test(s)
 483        > 1..6
 484        EOF
 485"
 486
 487test_expect_success '--run with two negations' "
 488        run_sub_test_lib_test run-two-neg \
 489                '--run with two negations' --run='"'!3 !6'"' <<-\\EOF &&
 490        for i in 1 2 3 4 5 6
 491        do
 492                test_expect_success \"passing test #\$i\" 'true'
 493        done
 494        test_done
 495        EOF
 496        check_sub_test_lib_test run-two-neg <<-\\EOF
 497        > ok 1 - passing test #1
 498        > ok 2 - passing test #2
 499        > ok 3 # skip passing test #3 (--run)
 500        > ok 4 - passing test #4
 501        > ok 5 - passing test #5
 502        > ok 6 # skip passing test #6 (--run)
 503        > # passed all 6 test(s)
 504        > 1..6
 505        EOF
 506"
 507
 508test_expect_success '--run a range and negation' "
 509        run_sub_test_lib_test run-range-and-neg \
 510                '--run a range and negation' --run='"'-4 !2'"' <<-\\EOF &&
 511        for i in 1 2 3 4 5 6
 512        do
 513                test_expect_success \"passing test #\$i\" 'true'
 514        done
 515        test_done
 516        EOF
 517        check_sub_test_lib_test run-range-and-neg <<-\\EOF
 518        > ok 1 - passing test #1
 519        > ok 2 # skip passing test #2 (--run)
 520        > ok 3 - passing test #3
 521        > ok 4 - passing test #4
 522        > ok 5 # skip passing test #5 (--run)
 523        > ok 6 # skip passing test #6 (--run)
 524        > # passed all 6 test(s)
 525        > 1..6
 526        EOF
 527"
 528
 529test_expect_success '--run range negation' "
 530        run_sub_test_lib_test run-range-neg \
 531                '--run range negation' --run='"'!1-3'"' <<-\\EOF &&
 532        for i in 1 2 3 4 5 6
 533        do
 534                test_expect_success \"passing test #\$i\" 'true'
 535        done
 536        test_done
 537        EOF
 538        check_sub_test_lib_test run-range-neg <<-\\EOF
 539        > ok 1 # skip passing test #1 (--run)
 540        > ok 2 # skip passing test #2 (--run)
 541        > ok 3 # skip passing test #3 (--run)
 542        > ok 4 - passing test #4
 543        > ok 5 - passing test #5
 544        > ok 6 - passing test #6
 545        > # passed all 6 test(s)
 546        > 1..6
 547        EOF
 548"
 549
 550test_expect_success '--run include, exclude and include' "
 551        run_sub_test_lib_test run-inc-neg-inc \
 552                '--run include, exclude and include' \
 553                --run='"'1-5 !1-3 2'"' <<-\\EOF &&
 554        for i in 1 2 3 4 5 6
 555        do
 556                test_expect_success \"passing test #\$i\" 'true'
 557        done
 558        test_done
 559        EOF
 560        check_sub_test_lib_test run-inc-neg-inc <<-\\EOF
 561        > ok 1 # skip passing test #1 (--run)
 562        > ok 2 - passing test #2
 563        > ok 3 # skip passing test #3 (--run)
 564        > ok 4 - passing test #4
 565        > ok 5 - passing test #5
 566        > ok 6 # skip passing test #6 (--run)
 567        > # passed all 6 test(s)
 568        > 1..6
 569        EOF
 570"
 571
 572test_expect_success '--run include, exclude and include, comma separated' "
 573        run_sub_test_lib_test run-inc-neg-inc-comma \
 574                '--run include, exclude and include, comma separated' \
 575                --run=1-5,\!1-3,2 <<-\\EOF &&
 576        for i in 1 2 3 4 5 6
 577        do
 578                test_expect_success \"passing test #\$i\" 'true'
 579        done
 580        test_done
 581        EOF
 582        check_sub_test_lib_test run-inc-neg-inc-comma <<-\\EOF
 583        > ok 1 # skip passing test #1 (--run)
 584        > ok 2 - passing test #2
 585        > ok 3 # skip passing test #3 (--run)
 586        > ok 4 - passing test #4
 587        > ok 5 - passing test #5
 588        > ok 6 # skip passing test #6 (--run)
 589        > # passed all 6 test(s)
 590        > 1..6
 591        EOF
 592"
 593
 594test_expect_success '--run exclude and include' "
 595        run_sub_test_lib_test run-neg-inc \
 596                '--run exclude and include' \
 597                --run='"'!3- 5'"' <<-\\EOF &&
 598        for i in 1 2 3 4 5 6
 599        do
 600                test_expect_success \"passing test #\$i\" 'true'
 601        done
 602        test_done
 603        EOF
 604        check_sub_test_lib_test run-neg-inc <<-\\EOF
 605        > ok 1 - passing test #1
 606        > ok 2 - passing test #2
 607        > ok 3 # skip passing test #3 (--run)
 608        > ok 4 # skip passing test #4 (--run)
 609        > ok 5 - passing test #5
 610        > ok 6 # skip passing test #6 (--run)
 611        > # passed all 6 test(s)
 612        > 1..6
 613        EOF
 614"
 615
 616test_expect_success '--run empty selectors' "
 617        run_sub_test_lib_test run-empty-sel \
 618                '--run empty selectors' \
 619                --run='1,,3,,,5' <<-\\EOF &&
 620        for i in 1 2 3 4 5 6
 621        do
 622                test_expect_success \"passing test #\$i\" 'true'
 623        done
 624        test_done
 625        EOF
 626        check_sub_test_lib_test run-empty-sel <<-\\EOF
 627        > ok 1 - passing test #1
 628        > ok 2 # skip passing test #2 (--run)
 629        > ok 3 - passing test #3
 630        > ok 4 # skip passing test #4 (--run)
 631        > ok 5 - passing test #5
 632        > ok 6 # skip passing test #6 (--run)
 633        > # passed all 6 test(s)
 634        > 1..6
 635        EOF
 636"
 637
 638test_expect_success '--run invalid range start' "
 639        run_sub_test_lib_test_err run-inv-range-start \
 640                '--run invalid range start' \
 641                --run='a-5' <<-\\EOF &&
 642        test_expect_success \"passing test #1\" 'true'
 643        test_done
 644        EOF
 645        check_sub_test_lib_test_err run-inv-range-start \
 646                <<-\\EOF_OUT 3<<-\\EOF_ERR
 647        > FATAL: Unexpected exit with code 1
 648        EOF_OUT
 649        > error: --run: invalid non-numeric in range start: 'a-5'
 650        EOF_ERR
 651"
 652
 653test_expect_success '--run invalid range end' "
 654        run_sub_test_lib_test_err run-inv-range-end \
 655                '--run invalid range end' \
 656                --run='1-z' <<-\\EOF &&
 657        test_expect_success \"passing test #1\" 'true'
 658        test_done
 659        EOF
 660        check_sub_test_lib_test_err run-inv-range-end \
 661                <<-\\EOF_OUT 3<<-\\EOF_ERR
 662        > FATAL: Unexpected exit with code 1
 663        EOF_OUT
 664        > error: --run: invalid non-numeric in range end: '1-z'
 665        EOF_ERR
 666"
 667
 668test_expect_success '--run invalid selector' "
 669        run_sub_test_lib_test_err run-inv-selector \
 670                '--run invalid selector' \
 671                --run='1?' <<-\\EOF &&
 672        test_expect_success \"passing test #1\" 'true'
 673        test_done
 674        EOF
 675        check_sub_test_lib_test_err run-inv-selector \
 676                <<-\\EOF_OUT 3<<-\\EOF_ERR
 677        > FATAL: Unexpected exit with code 1
 678        EOF_OUT
 679        > error: --run: invalid non-numeric in test selector: '1?'
 680        EOF_ERR
 681"
 682
 683
 684test_set_prereq HAVEIT
 685haveit=no
 686test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
 687        test_have_prereq HAVEIT &&
 688        haveit=yes
 689'
 690donthaveit=yes
 691test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
 692        donthaveit=no
 693'
 694if test $haveit$donthaveit != yesyes
 695then
 696        say "bug in test framework: prerequisite tags do not work reliably"
 697        exit 1
 698fi
 699
 700test_set_prereq HAVETHIS
 701haveit=no
 702test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
 703        test_have_prereq HAVEIT &&
 704        test_have_prereq HAVETHIS &&
 705        haveit=yes
 706'
 707donthaveit=yes
 708test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
 709        donthaveit=no
 710'
 711donthaveiteither=yes
 712test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
 713        donthaveiteither=no
 714'
 715if test $haveit$donthaveit$donthaveiteither != yesyesyes
 716then
 717        say "bug in test framework: multiple prerequisite tags do not work reliably"
 718        exit 1
 719fi
 720
 721test_lazy_prereq LAZY_TRUE true
 722havetrue=no
 723test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
 724        havetrue=yes
 725'
 726donthavetrue=yes
 727test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
 728        donthavetrue=no
 729'
 730
 731if test "$havetrue$donthavetrue" != yesyes
 732then
 733        say 'bug in test framework: lazy prerequisites do not work'
 734        exit 1
 735fi
 736
 737test_lazy_prereq LAZY_FALSE false
 738nothavefalse=no
 739test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
 740        nothavefalse=yes
 741'
 742havefalse=yes
 743test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
 744        havefalse=no
 745'
 746
 747if test "$nothavefalse$havefalse" != yesyes
 748then
 749        say 'bug in test framework: negative lazy prerequisites do not work'
 750        exit 1
 751fi
 752
 753clean=no
 754test_expect_success 'tests clean up after themselves' '
 755        test_when_finished clean=yes
 756'
 757
 758if test $clean != yes
 759then
 760        say "bug in test framework: basic cleanup command does not work reliably"
 761        exit 1
 762fi
 763
 764test_expect_success 'tests clean up even on failures' "
 765        test_must_fail run_sub_test_lib_test \
 766                failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
 767        test_expect_success 'tests clean up even after a failure' '
 768                touch clean-after-failure &&
 769                test_when_finished rm clean-after-failure &&
 770                (exit 1)
 771        '
 772        test_expect_success 'failure to clean up causes the test to fail' '
 773                test_when_finished \"(exit 2)\"
 774        '
 775        test_done
 776        EOF
 777        check_sub_test_lib_test failing-cleanup <<-\\EOF
 778        > not ok 1 - tests clean up even after a failure
 779        > #     Z
 780        > #     touch clean-after-failure &&
 781        > #     test_when_finished rm clean-after-failure &&
 782        > #     (exit 1)
 783        > #     Z
 784        > not ok 2 - failure to clean up causes the test to fail
 785        > #     Z
 786        > #     test_when_finished \"(exit 2)\"
 787        > #     Z
 788        > # failed 2 among 2 test(s)
 789        > 1..2
 790        EOF
 791"
 792
 793################################################################
 794# Basics of the basics
 795
 796# updating a new file without --add should fail.
 797test_expect_success 'git update-index without --add should fail adding' '
 798        test_must_fail git update-index should-be-empty
 799'
 800
 801# and with --add it should succeed, even if it is empty (it used to fail).
 802test_expect_success 'git update-index with --add should succeed' '
 803        git update-index --add should-be-empty
 804'
 805
 806test_expect_success 'writing tree out with git write-tree' '
 807        tree=$(git write-tree)
 808'
 809
 810# we know the shape and contents of the tree and know the object ID for it.
 811test_expect_success 'validate object ID of a known tree' '
 812        test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
 813    '
 814
 815# Removing paths.
 816test_expect_success 'git update-index without --remove should fail removing' '
 817        rm -f should-be-empty full-of-directories &&
 818        test_must_fail git update-index should-be-empty
 819'
 820
 821test_expect_success 'git update-index with --remove should be able to remove' '
 822        git update-index --remove should-be-empty
 823'
 824
 825# Empty tree can be written with recent write-tree.
 826test_expect_success 'git write-tree should be able to write an empty tree' '
 827        tree=$(git write-tree)
 828'
 829
 830test_expect_success 'validate object ID of a known tree' '
 831        test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
 832'
 833
 834# Various types of objects
 835
 836test_expect_success 'adding various types of objects with git update-index --add' '
 837        mkdir path2 path3 path3/subp3 &&
 838        paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
 839        (
 840                for p in $paths
 841                do
 842                        echo "hello $p" >$p || exit 1
 843                        test_ln_s_add "hello $p" ${p}sym || exit 1
 844                done
 845        ) &&
 846        find path* ! -type d -print | xargs git update-index --add
 847'
 848
 849# Show them and see that matches what we expect.
 850test_expect_success 'showing stage with git ls-files --stage' '
 851        git ls-files --stage >current
 852'
 853
 854test_expect_success 'validate git ls-files output for a known tree' '
 855        cat >expected <<-\EOF &&
 856        100644 f87290f8eb2cbbea7857214459a0739927eab154 0       path0
 857        120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0       path0sym
 858        100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0       path2/file2
 859        120000 d8ce161addc5173867a3c3c730924388daedbc38 0       path2/file2sym
 860        100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0       path3/file3
 861        120000 8599103969b43aff7e430efea79ca4636466794f 0       path3/file3sym
 862        100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0       path3/subp3/file3
 863        120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0       path3/subp3/file3sym
 864        EOF
 865        test_cmp expected current
 866'
 867
 868test_expect_success 'writing tree out with git write-tree' '
 869        tree=$(git write-tree)
 870'
 871
 872test_expect_success 'validate object ID for a known tree' '
 873        test "$tree" = 087704a96baf1c2d1c869a8b084481e121c88b5b
 874'
 875
 876test_expect_success 'showing tree with git ls-tree' '
 877    git ls-tree $tree >current
 878'
 879
 880test_expect_success 'git ls-tree output for a known tree' '
 881        cat >expected <<-\EOF &&
 882        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 883        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 884        040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 885        040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 886        EOF
 887        test_cmp expected current
 888'
 889
 890# This changed in ls-tree pathspec change -- recursive does
 891# not show tree nodes anymore.
 892test_expect_success 'showing tree with git ls-tree -r' '
 893        git ls-tree -r $tree >current
 894'
 895
 896test_expect_success 'git ls-tree -r output for a known tree' '
 897        cat >expected <<-\EOF &&
 898        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 899        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 900        100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 901        120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 902        100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 903        120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 904        100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 905        120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 906        EOF
 907        test_cmp expected current
 908'
 909
 910# But with -r -t we can have both.
 911test_expect_success 'showing tree with git ls-tree -r -t' '
 912        git ls-tree -r -t $tree >current
 913'
 914
 915test_expect_success 'git ls-tree -r output for a known tree' '
 916        cat >expected <<-\EOF &&
 917        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 918        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 919        040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 920        100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 921        120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 922        040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 923        100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 924        120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 925        040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2    path3/subp3
 926        100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 927        120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 928        EOF
 929        test_cmp expected current
 930'
 931
 932test_expect_success 'writing partial tree out with git write-tree --prefix' '
 933        ptree=$(git write-tree --prefix=path3)
 934'
 935
 936test_expect_success 'validate object ID for a known tree' '
 937        test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3
 938'
 939
 940test_expect_success 'writing partial tree out with git write-tree --prefix' '
 941        ptree=$(git write-tree --prefix=path3/subp3)
 942'
 943
 944test_expect_success 'validate object ID for a known tree' '
 945        test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2
 946'
 947
 948test_expect_success 'put invalid objects into the index' '
 949        rm -f .git/index &&
 950        cat >badobjects <<-\EOF &&
 951        100644 blob 1000000000000000000000000000000000000000    dir/file1
 952        100644 blob 2000000000000000000000000000000000000000    dir/file2
 953        100644 blob 3000000000000000000000000000000000000000    dir/file3
 954        100644 blob 4000000000000000000000000000000000000000    dir/file4
 955        100644 blob 5000000000000000000000000000000000000000    dir/file5
 956        EOF
 957        git update-index --index-info <badobjects
 958'
 959
 960test_expect_success 'writing this tree without --missing-ok' '
 961        test_must_fail git write-tree
 962'
 963
 964test_expect_success 'writing this tree with --missing-ok' '
 965        git write-tree --missing-ok
 966'
 967
 968
 969################################################################
 970test_expect_success 'git read-tree followed by write-tree should be idempotent' '
 971        rm -f .git/index
 972        git read-tree $tree &&
 973        test -f .git/index &&
 974        newtree=$(git write-tree) &&
 975        test "$newtree" = "$tree"
 976'
 977
 978test_expect_success 'validate git diff-files output for a know cache/work tree state' '
 979        cat >expected <<\EOF &&
 980:100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M      path0
 981:120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M      path0sym
 982:100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M      path2/file2
 983:120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M      path2/file2sym
 984:100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M      path3/file3
 985:120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M      path3/file3sym
 986:100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M      path3/subp3/file3
 987:120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M      path3/subp3/file3sym
 988EOF
 989        git diff-files >current &&
 990        test_cmp current expected
 991'
 992
 993test_expect_success 'git update-index --refresh should succeed' '
 994        git update-index --refresh
 995'
 996
 997test_expect_success 'no diff after checkout and git update-index --refresh' '
 998        git diff-files >current &&
 999        cmp -s current /dev/null
1000'
1001
1002################################################################
1003P=087704a96baf1c2d1c869a8b084481e121c88b5b
1004
1005test_expect_success 'git commit-tree records the correct tree in a commit' '
1006        commit0=$(echo NO | git commit-tree $P) &&
1007        tree=$(git show --pretty=raw $commit0 |
1008                 sed -n -e "s/^tree //p" -e "/^author /q") &&
1009        test "z$tree" = "z$P"
1010'
1011
1012test_expect_success 'git commit-tree records the correct parent in a commit' '
1013        commit1=$(echo NO | git commit-tree $P -p $commit0) &&
1014        parent=$(git show --pretty=raw $commit1 |
1015                sed -n -e "s/^parent //p" -e "/^author /q") &&
1016        test "z$commit0" = "z$parent"
1017'
1018
1019test_expect_success 'git commit-tree omits duplicated parent in a commit' '
1020        commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
1021             parent=$(git show --pretty=raw $commit2 |
1022                sed -n -e "s/^parent //p" -e "/^author /q" |
1023                sort -u) &&
1024        test "z$commit0" = "z$parent" &&
1025        numparent=$(git show --pretty=raw $commit2 |
1026                sed -n -e "s/^parent //p" -e "/^author /q" |
1027                wc -l) &&
1028        test $numparent = 1
1029'
1030
1031test_expect_success 'update-index D/F conflict' '
1032        mv path0 tmp &&
1033        mv path2 path0 &&
1034        mv tmp path2 &&
1035        git update-index --add --replace path2 path0/file2 &&
1036        numpath0=$(git ls-files path0 | wc -l) &&
1037        test $numpath0 = 1
1038'
1039
1040test_expect_success 'very long name in the index handled sanely' '
1041
1042        a=a && # 1
1043        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
1044        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
1045        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
1046        a=${a}q &&
1047
1048        >path4 &&
1049        git update-index --add path4 &&
1050        (
1051                git ls-files -s path4 |
1052                sed -e "s/      .*/     /" |
1053                tr -d "\012"
1054                echo "$a"
1055        ) | git update-index --index-info &&
1056        len=$(git ls-files "a*" | wc -c) &&
1057        test $len = 4098
1058'
1059
1060test_done