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