384b0aee47ba1606435be6e48f8a49c4db881598
   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
 149        > # fixed 1 known breakage(s)
 150        > # passed all 1 test(s)
 151        > 1..1
 152        EOF
 153"
 154
 155test_expect_success 'pretend we have a pass, fail, and known breakage' "
 156        test_must_fail run_sub_test_lib_test \
 157                mixed-results1 'mixed results #1' <<-\\EOF &&
 158        test_expect_success 'passing test' 'true'
 159        test_expect_success 'failing test' 'false'
 160        test_expect_failure 'pretend we have a known breakage' 'false'
 161        test_done
 162        EOF
 163        check_sub_test_lib_test mixed-results1 <<-\\EOF
 164        > ok 1 - passing test
 165        > not ok 2 - failing test
 166        > #     false
 167        > not ok 3 - pretend we have a known breakage # TODO known breakage
 168        > # still have 1 known breakage(s)
 169        > # failed 1 among remaining 2 test(s)
 170        > 1..3
 171        EOF
 172"
 173
 174test_expect_success 'pretend we have a mix of all possible results' "
 175        test_must_fail run_sub_test_lib_test \
 176                mixed-results2 'mixed results #2' <<-\\EOF &&
 177        test_expect_success 'passing test' 'true'
 178        test_expect_success 'passing test' 'true'
 179        test_expect_success 'passing test' 'true'
 180        test_expect_success 'passing test' 'true'
 181        test_expect_success 'failing test' 'false'
 182        test_expect_success 'failing test' 'false'
 183        test_expect_success 'failing test' 'false'
 184        test_expect_failure 'pretend we have a known breakage' 'false'
 185        test_expect_failure 'pretend we have a known breakage' 'false'
 186        test_expect_failure 'pretend we have fixed a known breakage' 'true'
 187        test_done
 188        EOF
 189        check_sub_test_lib_test mixed-results2 <<-\\EOF
 190        > ok 1 - passing test
 191        > ok 2 - passing test
 192        > ok 3 - passing test
 193        > ok 4 - passing test
 194        > not ok 5 - failing test
 195        > #     false
 196        > not ok 6 - failing test
 197        > #     false
 198        > not ok 7 - failing test
 199        > #     false
 200        > not ok 8 - pretend we have a known breakage # TODO known breakage
 201        > not ok 9 - pretend we have a known breakage # TODO known breakage
 202        > ok 10 - pretend we have fixed a known breakage # TODO known breakage
 203        > # fixed 1 known breakage(s)
 204        > # still have 2 known breakage(s)
 205        > # failed 3 among remaining 8 test(s)
 206        > 1..10
 207        EOF
 208"
 209
 210test_set_prereq HAVEIT
 211haveit=no
 212test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
 213        test_have_prereq HAVEIT &&
 214        haveit=yes
 215'
 216donthaveit=yes
 217test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
 218        donthaveit=no
 219'
 220if test $haveit$donthaveit != yesyes
 221then
 222        say "bug in test framework: prerequisite tags do not work reliably"
 223        exit 1
 224fi
 225
 226test_set_prereq HAVETHIS
 227haveit=no
 228test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
 229        test_have_prereq HAVEIT &&
 230        test_have_prereq HAVETHIS &&
 231        haveit=yes
 232'
 233donthaveit=yes
 234test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
 235        donthaveit=no
 236'
 237donthaveiteither=yes
 238test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
 239        donthaveiteither=no
 240'
 241if test $haveit$donthaveit$donthaveiteither != yesyesyes
 242then
 243        say "bug in test framework: multiple prerequisite tags do not work reliably"
 244        exit 1
 245fi
 246
 247clean=no
 248test_expect_success 'tests clean up after themselves' '
 249        test_when_finished clean=yes
 250'
 251
 252if test $clean != yes
 253then
 254        say "bug in test framework: basic cleanup command does not work reliably"
 255        exit 1
 256fi
 257
 258test_expect_success 'tests clean up even on failures' "
 259        test_must_fail run_sub_test_lib_test \
 260                failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
 261        test_expect_success 'tests clean up even after a failure' '
 262                touch clean-after-failure &&
 263                test_when_finished rm clean-after-failure &&
 264                (exit 1)
 265        '
 266        test_expect_success 'failure to clean up causes the test to fail' '
 267                test_when_finished \"(exit 2)\"
 268        '
 269        test_done
 270        EOF
 271        check_sub_test_lib_test failing-cleanup <<-\\EOF
 272        > not ok 1 - tests clean up even after a failure
 273        > #     Z
 274        > #     touch clean-after-failure &&
 275        > #     test_when_finished rm clean-after-failure &&
 276        > #     (exit 1)
 277        > #     Z
 278        > not ok 2 - failure to clean up causes the test to fail
 279        > #     Z
 280        > #     test_when_finished \"(exit 2)\"
 281        > #     Z
 282        > # failed 2 among 2 test(s)
 283        > 1..2
 284        EOF
 285"
 286
 287################################################################
 288# Basics of the basics
 289
 290# updating a new file without --add should fail.
 291test_expect_success 'git update-index without --add should fail adding' '
 292        test_must_fail git update-index should-be-empty
 293'
 294
 295# and with --add it should succeed, even if it is empty (it used to fail).
 296test_expect_success 'git update-index with --add should succeed' '
 297        git update-index --add should-be-empty
 298'
 299
 300test_expect_success 'writing tree out with git write-tree' '
 301        tree=$(git write-tree)
 302'
 303
 304# we know the shape and contents of the tree and know the object ID for it.
 305test_expect_success 'validate object ID of a known tree' '
 306        test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
 307    '
 308
 309# Removing paths.
 310test_expect_success 'git update-index without --remove should fail removing' '
 311        rm -f should-be-empty full-of-directories &&
 312        test_must_fail git update-index should-be-empty
 313'
 314
 315test_expect_success 'git update-index with --remove should be able to remove' '
 316        git update-index --remove should-be-empty
 317'
 318
 319# Empty tree can be written with recent write-tree.
 320test_expect_success 'git write-tree should be able to write an empty tree' '
 321        tree=$(git write-tree)
 322'
 323
 324test_expect_success 'validate object ID of a known tree' '
 325        test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
 326'
 327
 328# Various types of objects
 329
 330# Some filesystems do not support symblic links; on such systems
 331# some expected values are different
 332if test_have_prereq SYMLINKS
 333then
 334        expectfilter=cat
 335        expectedtree=087704a96baf1c2d1c869a8b084481e121c88b5b
 336        expectedptree1=21ae8269cacbe57ae09138dcc3a2887f904d02b3
 337        expectedptree2=3c5e5399f3a333eddecce7a9b9465b63f65f51e2
 338else
 339        expectfilter='grep -v sym'
 340        expectedtree=8e18edf7d7edcf4371a3ac6ae5f07c2641db7c46
 341        expectedptree1=cfb8591b2f65de8b8cc1020cd7d9e67e7793b325
 342        expectedptree2=ce580448f0148b985a513b693fdf7d802cacb44f
 343fi
 344
 345
 346test_expect_success 'adding various types of objects with git update-index --add' '
 347        mkdir path2 path3 path3/subp3 &&
 348        paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
 349        (
 350                for p in $paths
 351                do
 352                        echo "hello $p" >$p || exit 1
 353                        if test_have_prereq SYMLINKS
 354                        then
 355                                ln -s "hello $p" ${p}sym || exit 1
 356                        fi
 357                done
 358        ) &&
 359        find path* ! -type d -print | xargs git update-index --add
 360'
 361
 362# Show them and see that matches what we expect.
 363test_expect_success 'showing stage with git ls-files --stage' '
 364        git ls-files --stage >current
 365'
 366
 367test_expect_success 'validate git ls-files output for a known tree' '
 368        $expectfilter >expected <<-\EOF &&
 369        100644 f87290f8eb2cbbea7857214459a0739927eab154 0       path0
 370        120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0       path0sym
 371        100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0       path2/file2
 372        120000 d8ce161addc5173867a3c3c730924388daedbc38 0       path2/file2sym
 373        100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0       path3/file3
 374        120000 8599103969b43aff7e430efea79ca4636466794f 0       path3/file3sym
 375        100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0       path3/subp3/file3
 376        120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0       path3/subp3/file3sym
 377        EOF
 378        test_cmp expected current
 379'
 380
 381test_expect_success 'writing tree out with git write-tree' '
 382        tree=$(git write-tree)
 383'
 384
 385test_expect_success 'validate object ID for a known tree' '
 386        test "$tree" = "$expectedtree"
 387'
 388
 389test_expect_success 'showing tree with git ls-tree' '
 390    git ls-tree $tree >current
 391'
 392
 393test_expect_success SYMLINKS 'git ls-tree output for a known tree' '
 394        cat >expected <<-\EOF &&
 395        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 396        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 397        040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 398        040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 399        EOF
 400        test_cmp expected current
 401'
 402
 403# This changed in ls-tree pathspec change -- recursive does
 404# not show tree nodes anymore.
 405test_expect_success 'showing tree with git ls-tree -r' '
 406        git ls-tree -r $tree >current
 407'
 408
 409test_expect_success 'git ls-tree -r output for a known tree' '
 410        $expectfilter >expected <<-\EOF &&
 411        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 412        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 413        100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 414        120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 415        100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 416        120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 417        100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 418        120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 419        EOF
 420        test_cmp expected current
 421'
 422
 423# But with -r -t we can have both.
 424test_expect_success 'showing tree with git ls-tree -r -t' '
 425        git ls-tree -r -t $tree >current
 426'
 427
 428test_expect_success SYMLINKS 'git ls-tree -r output for a known tree' '
 429        cat >expected <<-\EOF &&
 430        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 431        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 432        040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 433        100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 434        120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 435        040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 436        100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 437        120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 438        040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2    path3/subp3
 439        100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 440        120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 441        EOF
 442        test_cmp expected current
 443'
 444
 445test_expect_success 'writing partial tree out with git write-tree --prefix' '
 446        ptree=$(git write-tree --prefix=path3)
 447'
 448
 449test_expect_success 'validate object ID for a known tree' '
 450        test "$ptree" = "$expectedptree1"
 451'
 452
 453test_expect_success 'writing partial tree out with git write-tree --prefix' '
 454        ptree=$(git write-tree --prefix=path3/subp3)
 455'
 456
 457test_expect_success 'validate object ID for a known tree' '
 458        test "$ptree" = "$expectedptree2"
 459'
 460
 461test_expect_success 'put invalid objects into the index' '
 462        rm -f .git/index &&
 463        cat >badobjects <<-\EOF &&
 464        100644 blob 1000000000000000000000000000000000000000    dir/file1
 465        100644 blob 2000000000000000000000000000000000000000    dir/file2
 466        100644 blob 3000000000000000000000000000000000000000    dir/file3
 467        100644 blob 4000000000000000000000000000000000000000    dir/file4
 468        100644 blob 5000000000000000000000000000000000000000    dir/file5
 469        EOF
 470        git update-index --index-info <badobjects
 471'
 472
 473test_expect_success 'writing this tree without --missing-ok' '
 474        test_must_fail git write-tree
 475'
 476
 477test_expect_success 'writing this tree with --missing-ok' '
 478        git write-tree --missing-ok
 479'
 480
 481
 482################################################################
 483test_expect_success 'git read-tree followed by write-tree should be idempotent' '
 484        rm -f .git/index
 485        git read-tree $tree &&
 486        test -f .git/index &&
 487        newtree=$(git write-tree) &&
 488        test "$newtree" = "$tree"
 489'
 490
 491test_expect_success 'validate git diff-files output for a know cache/work tree state' '
 492        $expectfilter >expected <<\EOF &&
 493:100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M      path0
 494:120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M      path0sym
 495:100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M      path2/file2
 496:120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M      path2/file2sym
 497:100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M      path3/file3
 498:120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M      path3/file3sym
 499:100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M      path3/subp3/file3
 500:120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M      path3/subp3/file3sym
 501EOF
 502        git diff-files >current &&
 503        test_cmp current expected
 504'
 505
 506test_expect_success 'git update-index --refresh should succeed' '
 507        git update-index --refresh
 508'
 509
 510test_expect_success 'no diff after checkout and git update-index --refresh' '
 511        git diff-files >current &&
 512        cmp -s current /dev/null
 513'
 514
 515################################################################
 516P=$expectedtree
 517
 518test_expect_success 'git commit-tree records the correct tree in a commit' '
 519        commit0=$(echo NO | git commit-tree $P) &&
 520        tree=$(git show --pretty=raw $commit0 |
 521                 sed -n -e "s/^tree //p" -e "/^author /q") &&
 522        test "z$tree" = "z$P"
 523'
 524
 525test_expect_success 'git commit-tree records the correct parent in a commit' '
 526        commit1=$(echo NO | git commit-tree $P -p $commit0) &&
 527        parent=$(git show --pretty=raw $commit1 |
 528                sed -n -e "s/^parent //p" -e "/^author /q") &&
 529        test "z$commit0" = "z$parent"
 530'
 531
 532test_expect_success 'git commit-tree omits duplicated parent in a commit' '
 533        commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
 534             parent=$(git show --pretty=raw $commit2 |
 535                sed -n -e "s/^parent //p" -e "/^author /q" |
 536                sort -u) &&
 537        test "z$commit0" = "z$parent" &&
 538        numparent=$(git show --pretty=raw $commit2 |
 539                sed -n -e "s/^parent //p" -e "/^author /q" |
 540                wc -l) &&
 541        test $numparent = 1
 542'
 543
 544test_expect_success 'update-index D/F conflict' '
 545        mv path0 tmp &&
 546        mv path2 path0 &&
 547        mv tmp path2 &&
 548        git update-index --add --replace path2 path0/file2 &&
 549        numpath0=$(git ls-files path0 | wc -l) &&
 550        test $numpath0 = 1
 551'
 552
 553test_expect_success 'very long name in the index handled sanely' '
 554
 555        a=a && # 1
 556        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
 557        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
 558        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
 559        a=${a}q &&
 560
 561        >path4 &&
 562        git update-index --add path4 &&
 563        (
 564                git ls-files -s path4 |
 565                sed -e "s/      .*/     /" |
 566                tr -d "\012"
 567                echo "$a"
 568        ) | git update-index --index-info &&
 569        len=$(git ls-files "a*" | wc -c) &&
 570        test $len = 4098
 571'
 572
 573test_done