t / t0000-basic.shon commit tests: paint unexpectedly fixed known breakages in bold red (b73d9a2)
   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################################################################
  22# It appears that people try to run tests without building...
  23
  24../git >/dev/null
  25if test $? != 1
  26then
  27        echo >&2 'You do not seem to have built git yet.'
  28        exit 1
  29fi
  30
  31. ./test-lib.sh
  32
  33################################################################
  34# git init has been done in an empty repository.
  35# make sure it is empty.
  36
  37test_expect_success '.git/objects should be empty after git init in an empty repo' '
  38        find .git/objects -type f -print >should-be-empty &&
  39        test_line_count = 0 should-be-empty
  40'
  41
  42# also it should have 2 subdirectories; no fan-out anymore, pack, and info.
  43# 3 is counting "objects" itself
  44test_expect_success '.git/objects should have 3 subdirectories' '
  45        find .git/objects -type d -print >full-of-directories &&
  46        test_line_count = 3 full-of-directories
  47'
  48
  49################################################################
  50# Test harness
  51test_expect_success 'success is reported like this' '
  52        :
  53'
  54test_expect_failure 'pretend we have a known breakage' '
  55        false
  56'
  57
  58run_sub_test_lib_test () {
  59        name="$1" descr="$2" # stdin is the body of the test code
  60        mkdir "$name" &&
  61        (
  62                cd "$name" &&
  63                cat >"$name.sh" <<-EOF &&
  64                #!$SHELL_PATH
  65
  66                test_description='$descr (run in sub test-lib)
  67
  68                This is run in a sub test-lib so that we do not get incorrect
  69                passing metrics
  70                '
  71
  72                # Point to the t/test-lib.sh, which isn't in ../ as usual
  73                . "\$TEST_DIRECTORY"/test-lib.sh
  74                EOF
  75                cat >>"$name.sh" &&
  76                chmod +x "$name.sh" &&
  77                export TEST_DIRECTORY &&
  78                ./"$name.sh" >out 2>err
  79        )
  80}
  81
  82check_sub_test_lib_test () {
  83        name="$1" # stdin is the expected output from the test
  84        (
  85                cd "$name" &&
  86                ! test -s err &&
  87                sed -e 's/^> //' -e 's/Z$//' >expect &&
  88                test_cmp expect out
  89        )
  90}
  91
  92test_expect_success 'pretend we have a fully passing test suite' "
  93        run_sub_test_lib_test full-pass '3 passing tests' <<-\\EOF &&
  94        for i in 1 2 3
  95        do
  96                test_expect_success \"passing test #\$i\" 'true'
  97        done
  98        test_done
  99        EOF
 100        check_sub_test_lib_test full-pass <<-\\EOF
 101        > ok 1 - passing test #1
 102        > ok 2 - passing test #2
 103        > ok 3 - passing test #3
 104        > # passed all 3 test(s)
 105        > 1..3
 106        EOF
 107"
 108
 109test_expect_success 'pretend we have a partially passing test suite' "
 110        test_must_fail run_sub_test_lib_test \
 111                partial-pass '2/3 tests passing' <<-\\EOF &&
 112        test_expect_success 'passing test #1' 'true'
 113        test_expect_success 'failing test #2' 'false'
 114        test_expect_success 'passing test #3' 'true'
 115        test_done
 116        EOF
 117        check_sub_test_lib_test partial-pass <<-\\EOF
 118        > ok 1 - passing test #1
 119        > not ok 2 - failing test #2
 120        #       false
 121        > ok 3 - passing test #3
 122        > # failed 1 among 3 test(s)
 123        > 1..3
 124        EOF
 125"
 126
 127test_expect_success 'pretend we have a known breakage' "
 128        run_sub_test_lib_test failing-todo 'A failing TODO test' <<-\\EOF &&
 129        test_expect_success 'passing test' 'true'
 130        test_expect_failure 'pretend we have a known breakage' 'false'
 131        test_done
 132        EOF
 133        check_sub_test_lib_test failing-todo <<-\\EOF
 134        > ok 1 - passing test
 135        > not ok 2 - pretend we have a known breakage # TODO known breakage
 136        > # still have 1 known breakage(s)
 137        > # passed all remaining 1 test(s)
 138        > 1..2
 139        EOF
 140"
 141
 142test_expect_success 'pretend we have fixed a known breakage' "
 143        run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
 144        test_expect_failure 'pretend we have fixed a known breakage' 'true'
 145        test_done
 146        EOF
 147        check_sub_test_lib_test passing-todo <<-\\EOF
 148        > ok 1 - pretend we have fixed a known breakage # TODO known breakage vanished
 149        > # 1 known breakage(s) vanished; please update test(s)
 150        > 1..1
 151        EOF
 152"
 153
 154test_expect_success 'pretend we have fixed one of two known breakages (run in sub test-lib)' "
 155        run_sub_test_lib_test partially-passing-todos \
 156                '2 TODO tests, one passing' <<-\\EOF &&
 157        test_expect_failure 'pretend we have a known breakage' 'false'
 158        test_expect_success 'pretend we have a passing test' 'true'
 159        test_expect_failure 'pretend we have fixed another known breakage' 'true'
 160        test_done
 161        EOF
 162        check_sub_test_lib_test partially-passing-todos <<-\\EOF
 163        > not ok 1 - pretend we have a known breakage # TODO known breakage
 164        > ok 2 - pretend we have a passing test
 165        > ok 3 - pretend we have fixed another known breakage # TODO known breakage vanished
 166        > # 1 known breakage(s) vanished; please update test(s)
 167        > # still have 1 known breakage(s)
 168        > # passed all remaining 1 test(s)
 169        > 1..3
 170        EOF
 171"
 172
 173test_expect_success 'pretend we have a pass, fail, and known breakage' "
 174        test_must_fail run_sub_test_lib_test \
 175                mixed-results1 'mixed results #1' <<-\\EOF &&
 176        test_expect_success 'passing test' 'true'
 177        test_expect_success 'failing test' 'false'
 178        test_expect_failure 'pretend we have a known breakage' 'false'
 179        test_done
 180        EOF
 181        check_sub_test_lib_test mixed-results1 <<-\\EOF
 182        > ok 1 - passing test
 183        > not ok 2 - failing test
 184        > #     false
 185        > not ok 3 - pretend we have a known breakage # TODO known breakage
 186        > # still have 1 known breakage(s)
 187        > # failed 1 among remaining 2 test(s)
 188        > 1..3
 189        EOF
 190"
 191
 192test_expect_success 'pretend we have a mix of all possible results' "
 193        test_must_fail run_sub_test_lib_test \
 194                mixed-results2 'mixed results #2' <<-\\EOF &&
 195        test_expect_success 'passing test' 'true'
 196        test_expect_success 'passing test' 'true'
 197        test_expect_success 'passing test' 'true'
 198        test_expect_success 'passing test' 'true'
 199        test_expect_success 'failing test' 'false'
 200        test_expect_success 'failing test' 'false'
 201        test_expect_success 'failing test' 'false'
 202        test_expect_failure 'pretend we have a known breakage' 'false'
 203        test_expect_failure 'pretend we have a known breakage' 'false'
 204        test_expect_failure 'pretend we have fixed a known breakage' 'true'
 205        test_done
 206        EOF
 207        check_sub_test_lib_test mixed-results2 <<-\\EOF
 208        > ok 1 - passing test
 209        > ok 2 - passing test
 210        > ok 3 - passing test
 211        > ok 4 - passing test
 212        > not ok 5 - failing test
 213        > #     false
 214        > not ok 6 - failing test
 215        > #     false
 216        > not ok 7 - failing test
 217        > #     false
 218        > not ok 8 - pretend we have a known breakage # TODO known breakage
 219        > not ok 9 - pretend we have a known breakage # TODO known breakage
 220        > ok 10 - pretend we have fixed a known breakage # TODO known breakage vanished
 221        > # 1 known breakage(s) vanished; please update test(s)
 222        > # still have 2 known breakage(s)
 223        > # failed 3 among remaining 7 test(s)
 224        > 1..10
 225        EOF
 226"
 227
 228test_set_prereq HAVEIT
 229haveit=no
 230test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
 231        test_have_prereq HAVEIT &&
 232        haveit=yes
 233'
 234donthaveit=yes
 235test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
 236        donthaveit=no
 237'
 238if test $haveit$donthaveit != yesyes
 239then
 240        say "bug in test framework: prerequisite tags do not work reliably"
 241        exit 1
 242fi
 243
 244test_set_prereq HAVETHIS
 245haveit=no
 246test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
 247        test_have_prereq HAVEIT &&
 248        test_have_prereq HAVETHIS &&
 249        haveit=yes
 250'
 251donthaveit=yes
 252test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
 253        donthaveit=no
 254'
 255donthaveiteither=yes
 256test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
 257        donthaveiteither=no
 258'
 259if test $haveit$donthaveit$donthaveiteither != yesyesyes
 260then
 261        say "bug in test framework: multiple prerequisite tags do not work reliably"
 262        exit 1
 263fi
 264
 265clean=no
 266test_expect_success 'tests clean up after themselves' '
 267        test_when_finished clean=yes
 268'
 269
 270if test $clean != yes
 271then
 272        say "bug in test framework: basic cleanup command does not work reliably"
 273        exit 1
 274fi
 275
 276test_expect_success 'tests clean up even on failures' "
 277        test_must_fail run_sub_test_lib_test \
 278                failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
 279        test_expect_success 'tests clean up even after a failure' '
 280                touch clean-after-failure &&
 281                test_when_finished rm clean-after-failure &&
 282                (exit 1)
 283        '
 284        test_expect_success 'failure to clean up causes the test to fail' '
 285                test_when_finished \"(exit 2)\"
 286        '
 287        test_done
 288        EOF
 289        check_sub_test_lib_test failing-cleanup <<-\\EOF
 290        > not ok 1 - tests clean up even after a failure
 291        > #     Z
 292        > #     touch clean-after-failure &&
 293        > #     test_when_finished rm clean-after-failure &&
 294        > #     (exit 1)
 295        > #     Z
 296        > not ok 2 - failure to clean up causes the test to fail
 297        > #     Z
 298        > #     test_when_finished \"(exit 2)\"
 299        > #     Z
 300        > # failed 2 among 2 test(s)
 301        > 1..2
 302        EOF
 303"
 304
 305################################################################
 306# Basics of the basics
 307
 308# updating a new file without --add should fail.
 309test_expect_success 'git update-index without --add should fail adding' '
 310        test_must_fail git update-index should-be-empty
 311'
 312
 313# and with --add it should succeed, even if it is empty (it used to fail).
 314test_expect_success 'git update-index with --add should succeed' '
 315        git update-index --add should-be-empty
 316'
 317
 318test_expect_success 'writing tree out with git write-tree' '
 319        tree=$(git write-tree)
 320'
 321
 322# we know the shape and contents of the tree and know the object ID for it.
 323test_expect_success 'validate object ID of a known tree' '
 324        test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
 325    '
 326
 327# Removing paths.
 328test_expect_success 'git update-index without --remove should fail removing' '
 329        rm -f should-be-empty full-of-directories &&
 330        test_must_fail git update-index should-be-empty
 331'
 332
 333test_expect_success 'git update-index with --remove should be able to remove' '
 334        git update-index --remove should-be-empty
 335'
 336
 337# Empty tree can be written with recent write-tree.
 338test_expect_success 'git write-tree should be able to write an empty tree' '
 339        tree=$(git write-tree)
 340'
 341
 342test_expect_success 'validate object ID of a known tree' '
 343        test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
 344'
 345
 346# Various types of objects
 347
 348# Some filesystems do not support symblic links; on such systems
 349# some expected values are different
 350if test_have_prereq SYMLINKS
 351then
 352        expectfilter=cat
 353        expectedtree=087704a96baf1c2d1c869a8b084481e121c88b5b
 354        expectedptree1=21ae8269cacbe57ae09138dcc3a2887f904d02b3
 355        expectedptree2=3c5e5399f3a333eddecce7a9b9465b63f65f51e2
 356else
 357        expectfilter='grep -v sym'
 358        expectedtree=8e18edf7d7edcf4371a3ac6ae5f07c2641db7c46
 359        expectedptree1=cfb8591b2f65de8b8cc1020cd7d9e67e7793b325
 360        expectedptree2=ce580448f0148b985a513b693fdf7d802cacb44f
 361fi
 362
 363
 364test_expect_success 'adding various types of objects with git update-index --add' '
 365        mkdir path2 path3 path3/subp3 &&
 366        paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
 367        (
 368                for p in $paths
 369                do
 370                        echo "hello $p" >$p || exit 1
 371                        if test_have_prereq SYMLINKS
 372                        then
 373                                ln -s "hello $p" ${p}sym || exit 1
 374                        fi
 375                done
 376        ) &&
 377        find path* ! -type d -print | xargs git update-index --add
 378'
 379
 380# Show them and see that matches what we expect.
 381test_expect_success 'showing stage with git ls-files --stage' '
 382        git ls-files --stage >current
 383'
 384
 385test_expect_success 'validate git ls-files output for a known tree' '
 386        $expectfilter >expected <<-\EOF &&
 387        100644 f87290f8eb2cbbea7857214459a0739927eab154 0       path0
 388        120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0       path0sym
 389        100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0       path2/file2
 390        120000 d8ce161addc5173867a3c3c730924388daedbc38 0       path2/file2sym
 391        100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0       path3/file3
 392        120000 8599103969b43aff7e430efea79ca4636466794f 0       path3/file3sym
 393        100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0       path3/subp3/file3
 394        120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0       path3/subp3/file3sym
 395        EOF
 396        test_cmp expected current
 397'
 398
 399test_expect_success 'writing tree out with git write-tree' '
 400        tree=$(git write-tree)
 401'
 402
 403test_expect_success 'validate object ID for a known tree' '
 404        test "$tree" = "$expectedtree"
 405'
 406
 407test_expect_success 'showing tree with git ls-tree' '
 408    git ls-tree $tree >current
 409'
 410
 411test_expect_success SYMLINKS 'git ls-tree output for a known tree' '
 412        cat >expected <<-\EOF &&
 413        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 414        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 415        040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 416        040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 417        EOF
 418        test_cmp expected current
 419'
 420
 421# This changed in ls-tree pathspec change -- recursive does
 422# not show tree nodes anymore.
 423test_expect_success 'showing tree with git ls-tree -r' '
 424        git ls-tree -r $tree >current
 425'
 426
 427test_expect_success 'git ls-tree -r output for a known tree' '
 428        $expectfilter >expected <<-\EOF &&
 429        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 430        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 431        100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 432        120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 433        100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 434        120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 435        100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 436        120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 437        EOF
 438        test_cmp expected current
 439'
 440
 441# But with -r -t we can have both.
 442test_expect_success 'showing tree with git ls-tree -r -t' '
 443        git ls-tree -r -t $tree >current
 444'
 445
 446test_expect_success SYMLINKS 'git ls-tree -r output for a known tree' '
 447        cat >expected <<-\EOF &&
 448        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 449        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 450        040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 451        100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 452        120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 453        040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 454        100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 455        120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 456        040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2    path3/subp3
 457        100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 458        120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 459        EOF
 460        test_cmp expected current
 461'
 462
 463test_expect_success 'writing partial tree out with git write-tree --prefix' '
 464        ptree=$(git write-tree --prefix=path3)
 465'
 466
 467test_expect_success 'validate object ID for a known tree' '
 468        test "$ptree" = "$expectedptree1"
 469'
 470
 471test_expect_success 'writing partial tree out with git write-tree --prefix' '
 472        ptree=$(git write-tree --prefix=path3/subp3)
 473'
 474
 475test_expect_success 'validate object ID for a known tree' '
 476        test "$ptree" = "$expectedptree2"
 477'
 478
 479test_expect_success 'put invalid objects into the index' '
 480        rm -f .git/index &&
 481        cat >badobjects <<-\EOF &&
 482        100644 blob 1000000000000000000000000000000000000000    dir/file1
 483        100644 blob 2000000000000000000000000000000000000000    dir/file2
 484        100644 blob 3000000000000000000000000000000000000000    dir/file3
 485        100644 blob 4000000000000000000000000000000000000000    dir/file4
 486        100644 blob 5000000000000000000000000000000000000000    dir/file5
 487        EOF
 488        git update-index --index-info <badobjects
 489'
 490
 491test_expect_success 'writing this tree without --missing-ok' '
 492        test_must_fail git write-tree
 493'
 494
 495test_expect_success 'writing this tree with --missing-ok' '
 496        git write-tree --missing-ok
 497'
 498
 499
 500################################################################
 501test_expect_success 'git read-tree followed by write-tree should be idempotent' '
 502        rm -f .git/index
 503        git read-tree $tree &&
 504        test -f .git/index &&
 505        newtree=$(git write-tree) &&
 506        test "$newtree" = "$tree"
 507'
 508
 509test_expect_success 'validate git diff-files output for a know cache/work tree state' '
 510        $expectfilter >expected <<\EOF &&
 511:100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M      path0
 512:120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M      path0sym
 513:100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M      path2/file2
 514:120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M      path2/file2sym
 515:100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M      path3/file3
 516:120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M      path3/file3sym
 517:100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M      path3/subp3/file3
 518:120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M      path3/subp3/file3sym
 519EOF
 520        git diff-files >current &&
 521        test_cmp current expected
 522'
 523
 524test_expect_success 'git update-index --refresh should succeed' '
 525        git update-index --refresh
 526'
 527
 528test_expect_success 'no diff after checkout and git update-index --refresh' '
 529        git diff-files >current &&
 530        cmp -s current /dev/null
 531'
 532
 533################################################################
 534P=$expectedtree
 535
 536test_expect_success 'git commit-tree records the correct tree in a commit' '
 537        commit0=$(echo NO | git commit-tree $P) &&
 538        tree=$(git show --pretty=raw $commit0 |
 539                 sed -n -e "s/^tree //p" -e "/^author /q") &&
 540        test "z$tree" = "z$P"
 541'
 542
 543test_expect_success 'git commit-tree records the correct parent in a commit' '
 544        commit1=$(echo NO | git commit-tree $P -p $commit0) &&
 545        parent=$(git show --pretty=raw $commit1 |
 546                sed -n -e "s/^parent //p" -e "/^author /q") &&
 547        test "z$commit0" = "z$parent"
 548'
 549
 550test_expect_success 'git commit-tree omits duplicated parent in a commit' '
 551        commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
 552             parent=$(git show --pretty=raw $commit2 |
 553                sed -n -e "s/^parent //p" -e "/^author /q" |
 554                sort -u) &&
 555        test "z$commit0" = "z$parent" &&
 556        numparent=$(git show --pretty=raw $commit2 |
 557                sed -n -e "s/^parent //p" -e "/^author /q" |
 558                wc -l) &&
 559        test $numparent = 1
 560'
 561
 562test_expect_success 'update-index D/F conflict' '
 563        mv path0 tmp &&
 564        mv path2 path0 &&
 565        mv tmp path2 &&
 566        git update-index --add --replace path2 path0/file2 &&
 567        numpath0=$(git ls-files path0 | wc -l) &&
 568        test $numpath0 = 1
 569'
 570
 571test_expect_success 'very long name in the index handled sanely' '
 572
 573        a=a && # 1
 574        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
 575        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
 576        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
 577        a=${a}q &&
 578
 579        >path4 &&
 580        git update-index --add path4 &&
 581        (
 582                git ls-files -s path4 |
 583                sed -e "s/      .*/     /" |
 584                tr -d "\012"
 585                echo "$a"
 586        ) | git update-index --index-info &&
 587        len=$(git ls-files "a*" | wc -c) &&
 588        test $len = 4098
 589'
 590
 591test_done