t / t0000-basic.shon commit git-send-email: skip RFC2047 quoting for ASCII subjects (5637d85)
   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
  48test_expect_success 'pretend we have fixed a known breakage (run in sub test-lib)' "
  49        mkdir passing-todo &&
  50        (cd passing-todo &&
  51        cat >passing-todo.sh <<-EOF &&
  52        #!$SHELL_PATH
  53
  54        test_description='A passing TODO test
  55
  56        This is run in a sub test-lib so that we do not get incorrect
  57        passing metrics
  58        '
  59
  60        # Point to the t/test-lib.sh, which isn't in ../ as usual
  61        TEST_DIRECTORY=\"$TEST_DIRECTORY\"
  62        . \"\$TEST_DIRECTORY\"/test-lib.sh
  63
  64        test_expect_failure 'pretend we have fixed a known breakage' '
  65                :
  66        '
  67
  68        test_done
  69        EOF
  70        chmod +x passing-todo.sh &&
  71        ./passing-todo.sh >out 2>err &&
  72        ! test -s err &&
  73        sed -e 's/^> //' >expect <<-\\EOF &&
  74        > ok 1 - pretend we have fixed a known breakage # TODO known breakage
  75        > # fixed 1 known breakage(s)
  76        > # passed all 1 test(s)
  77        > 1..1
  78        EOF
  79        test_cmp expect out)
  80"
  81test_set_prereq HAVEIT
  82haveit=no
  83test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
  84        test_have_prereq HAVEIT &&
  85        haveit=yes
  86'
  87donthaveit=yes
  88test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
  89        donthaveit=no
  90'
  91if test $haveit$donthaveit != yesyes
  92then
  93        say "bug in test framework: prerequisite tags do not work reliably"
  94        exit 1
  95fi
  96
  97test_set_prereq HAVETHIS
  98haveit=no
  99test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
 100        test_have_prereq HAVEIT &&
 101        test_have_prereq HAVETHIS &&
 102        haveit=yes
 103'
 104donthaveit=yes
 105test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
 106        donthaveit=no
 107'
 108donthaveiteither=yes
 109test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
 110        donthaveiteither=no
 111'
 112if test $haveit$donthaveit$donthaveiteither != yesyesyes
 113then
 114        say "bug in test framework: multiple prerequisite tags do not work reliably"
 115        exit 1
 116fi
 117
 118clean=no
 119test_expect_success 'tests clean up after themselves' '
 120        test_when_finished clean=yes
 121'
 122
 123if test $clean != yes
 124then
 125        say "bug in test framework: basic cleanup command does not work reliably"
 126        exit 1
 127fi
 128
 129test_expect_success 'tests clean up even on failures' "
 130        mkdir failing-cleanup &&
 131        (
 132        cd failing-cleanup &&
 133
 134        cat >failing-cleanup.sh <<-EOF &&
 135        #!$SHELL_PATH
 136
 137        test_description='Failing tests with cleanup commands'
 138
 139        # Point to the t/test-lib.sh, which isn't in ../ as usual
 140        TEST_DIRECTORY=\"$TEST_DIRECTORY\"
 141        . \"\$TEST_DIRECTORY\"/test-lib.sh
 142
 143        test_expect_success 'tests clean up even after a failure' '
 144                touch clean-after-failure &&
 145                test_when_finished rm clean-after-failure &&
 146                (exit 1)
 147        '
 148        test_expect_success 'failure to clean up causes the test to fail' '
 149                test_when_finished \"(exit 2)\"
 150        '
 151        test_done
 152
 153        EOF
 154
 155        chmod +x failing-cleanup.sh &&
 156        test_must_fail ./failing-cleanup.sh >out 2>err &&
 157        ! test -s err &&
 158        ! test -f \"trash directory.failing-cleanup/clean-after-failure\" &&
 159        sed -e 's/Z$//' -e 's/^> //' >expect <<-\\EOF &&
 160        > not ok - 1 tests clean up even after a failure
 161        > #     Z
 162        > #     touch clean-after-failure &&
 163        > #     test_when_finished rm clean-after-failure &&
 164        > #     (exit 1)
 165        > #     Z
 166        > not ok - 2 failure to clean up causes the test to fail
 167        > #     Z
 168        > #     test_when_finished \"(exit 2)\"
 169        > #     Z
 170        > # failed 2 among 2 test(s)
 171        > 1..2
 172        EOF
 173        test_cmp expect out
 174        )
 175"
 176
 177################################################################
 178# Basics of the basics
 179
 180# updating a new file without --add should fail.
 181test_expect_success 'git update-index without --add should fail adding' '
 182        test_must_fail git update-index should-be-empty
 183'
 184
 185# and with --add it should succeed, even if it is empty (it used to fail).
 186test_expect_success 'git update-index with --add should succeed' '
 187        git update-index --add should-be-empty
 188'
 189
 190test_expect_success 'writing tree out with git write-tree' '
 191        tree=$(git write-tree)
 192'
 193
 194# we know the shape and contents of the tree and know the object ID for it.
 195test_expect_success 'validate object ID of a known tree' '
 196        test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
 197    '
 198
 199# Removing paths.
 200test_expect_success 'git update-index without --remove should fail removing' '
 201        rm -f should-be-empty full-of-directories &&
 202        test_must_fail git update-index should-be-empty
 203'
 204
 205test_expect_success 'git update-index with --remove should be able to remove' '
 206        git update-index --remove should-be-empty
 207'
 208
 209# Empty tree can be written with recent write-tree.
 210test_expect_success 'git write-tree should be able to write an empty tree' '
 211        tree=$(git write-tree)
 212'
 213
 214test_expect_success 'validate object ID of a known tree' '
 215        test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
 216'
 217
 218# Various types of objects
 219
 220# Some filesystems do not support symblic links; on such systems
 221# some expected values are different
 222if test_have_prereq SYMLINKS
 223then
 224        expectfilter=cat
 225        expectedtree=087704a96baf1c2d1c869a8b084481e121c88b5b
 226        expectedptree1=21ae8269cacbe57ae09138dcc3a2887f904d02b3
 227        expectedptree2=3c5e5399f3a333eddecce7a9b9465b63f65f51e2
 228else
 229        expectfilter='grep -v sym'
 230        expectedtree=8e18edf7d7edcf4371a3ac6ae5f07c2641db7c46
 231        expectedptree1=cfb8591b2f65de8b8cc1020cd7d9e67e7793b325
 232        expectedptree2=ce580448f0148b985a513b693fdf7d802cacb44f
 233fi
 234
 235
 236test_expect_success 'adding various types of objects with git update-index --add' '
 237        mkdir path2 path3 path3/subp3 &&
 238        paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
 239        (
 240                for p in $paths
 241                do
 242                        echo "hello $p" >$p || exit 1
 243                        if test_have_prereq SYMLINKS
 244                        then
 245                                ln -s "hello $p" ${p}sym || exit 1
 246                        fi
 247                done
 248        ) &&
 249        find path* ! -type d -print | xargs git update-index --add
 250'
 251
 252# Show them and see that matches what we expect.
 253test_expect_success 'showing stage with git ls-files --stage' '
 254        git ls-files --stage >current
 255'
 256
 257test_expect_success 'validate git ls-files output for a known tree' '
 258        $expectfilter >expected <<-\EOF &&
 259        100644 f87290f8eb2cbbea7857214459a0739927eab154 0       path0
 260        120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0       path0sym
 261        100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0       path2/file2
 262        120000 d8ce161addc5173867a3c3c730924388daedbc38 0       path2/file2sym
 263        100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0       path3/file3
 264        120000 8599103969b43aff7e430efea79ca4636466794f 0       path3/file3sym
 265        100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0       path3/subp3/file3
 266        120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0       path3/subp3/file3sym
 267        EOF
 268        test_cmp expected current
 269'
 270
 271test_expect_success 'writing tree out with git write-tree' '
 272        tree=$(git write-tree)
 273'
 274
 275test_expect_success 'validate object ID for a known tree' '
 276        test "$tree" = "$expectedtree"
 277'
 278
 279test_expect_success 'showing tree with git ls-tree' '
 280    git ls-tree $tree >current
 281'
 282
 283test_expect_success SYMLINKS 'git ls-tree output for a known tree' '
 284        cat >expected <<-\EOF &&
 285        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 286        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 287        040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 288        040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 289        EOF
 290        test_cmp expected current
 291'
 292
 293# This changed in ls-tree pathspec change -- recursive does
 294# not show tree nodes anymore.
 295test_expect_success 'showing tree with git ls-tree -r' '
 296        git ls-tree -r $tree >current
 297'
 298
 299test_expect_success 'git ls-tree -r output for a known tree' '
 300        $expectfilter >expected <<-\EOF &&
 301        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 302        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 303        100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 304        120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 305        100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 306        120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 307        100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 308        120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 309        EOF
 310        test_cmp expected current
 311'
 312
 313# But with -r -t we can have both.
 314test_expect_success 'showing tree with git ls-tree -r -t' '
 315        git ls-tree -r -t $tree >current
 316'
 317
 318test_expect_success SYMLINKS 'git ls-tree -r output for a known tree' '
 319        cat >expected <<-\EOF &&
 320        100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 321        120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 322        040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 323        100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 324        120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 325        040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 326        100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 327        120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 328        040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2    path3/subp3
 329        100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 330        120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 331        EOF
 332        test_cmp expected current
 333'
 334
 335test_expect_success 'writing partial tree out with git write-tree --prefix' '
 336        ptree=$(git write-tree --prefix=path3)
 337'
 338
 339test_expect_success 'validate object ID for a known tree' '
 340        test "$ptree" = "$expectedptree1"
 341'
 342
 343test_expect_success 'writing partial tree out with git write-tree --prefix' '
 344        ptree=$(git write-tree --prefix=path3/subp3)
 345'
 346
 347test_expect_success 'validate object ID for a known tree' '
 348        test "$ptree" = "$expectedptree2"
 349'
 350
 351test_expect_success 'put invalid objects into the index' '
 352        rm -f .git/index &&
 353        cat >badobjects <<-\EOF &&
 354        100644 blob 1000000000000000000000000000000000000000    dir/file1
 355        100644 blob 2000000000000000000000000000000000000000    dir/file2
 356        100644 blob 3000000000000000000000000000000000000000    dir/file3
 357        100644 blob 4000000000000000000000000000000000000000    dir/file4
 358        100644 blob 5000000000000000000000000000000000000000    dir/file5
 359        EOF
 360        git update-index --index-info <badobjects
 361'
 362
 363test_expect_success 'writing this tree without --missing-ok' '
 364        test_must_fail git write-tree
 365'
 366
 367test_expect_success 'writing this tree with --missing-ok' '
 368        git write-tree --missing-ok
 369'
 370
 371
 372################################################################
 373test_expect_success 'git read-tree followed by write-tree should be idempotent' '
 374        rm -f .git/index
 375        git read-tree $tree &&
 376        test -f .git/index &&
 377        newtree=$(git write-tree) &&
 378        test "$newtree" = "$tree"
 379'
 380
 381test_expect_success 'validate git diff-files output for a know cache/work tree state' '
 382        $expectfilter >expected <<\EOF &&
 383:100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M      path0
 384:120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M      path0sym
 385:100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M      path2/file2
 386:120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M      path2/file2sym
 387:100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M      path3/file3
 388:120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M      path3/file3sym
 389:100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M      path3/subp3/file3
 390:120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M      path3/subp3/file3sym
 391EOF
 392        git diff-files >current &&
 393        test_cmp current expected
 394'
 395
 396test_expect_success 'git update-index --refresh should succeed' '
 397        git update-index --refresh
 398'
 399
 400test_expect_success 'no diff after checkout and git update-index --refresh' '
 401        git diff-files >current &&
 402        cmp -s current /dev/null
 403'
 404
 405################################################################
 406P=$expectedtree
 407
 408test_expect_success 'git commit-tree records the correct tree in a commit' '
 409        commit0=$(echo NO | git commit-tree $P) &&
 410        tree=$(git show --pretty=raw $commit0 |
 411                 sed -n -e "s/^tree //p" -e "/^author /q") &&
 412        test "z$tree" = "z$P"
 413'
 414
 415test_expect_success 'git commit-tree records the correct parent in a commit' '
 416        commit1=$(echo NO | git commit-tree $P -p $commit0) &&
 417        parent=$(git show --pretty=raw $commit1 |
 418                sed -n -e "s/^parent //p" -e "/^author /q") &&
 419        test "z$commit0" = "z$parent"
 420'
 421
 422test_expect_success 'git commit-tree omits duplicated parent in a commit' '
 423        commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
 424             parent=$(git show --pretty=raw $commit2 |
 425                sed -n -e "s/^parent //p" -e "/^author /q" |
 426                sort -u) &&
 427        test "z$commit0" = "z$parent" &&
 428        numparent=$(git show --pretty=raw $commit2 |
 429                sed -n -e "s/^parent //p" -e "/^author /q" |
 430                wc -l) &&
 431        test $numparent = 1
 432'
 433
 434test_expect_success 'update-index D/F conflict' '
 435        mv path0 tmp &&
 436        mv path2 path0 &&
 437        mv tmp path2 &&
 438        git update-index --add --replace path2 path0/file2 &&
 439        numpath0=$(git ls-files path0 | wc -l) &&
 440        test $numpath0 = 1
 441'
 442
 443test_expect_success 'very long name in the index handled sanely' '
 444
 445        a=a && # 1
 446        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
 447        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
 448        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
 449        a=${a}q &&
 450
 451        >path4 &&
 452        git update-index --add path4 &&
 453        (
 454                git ls-files -s path4 |
 455                sed -e "s/      .*/     /" |
 456                tr -d "\012"
 457                echo "$a"
 458        ) | git update-index --index-info &&
 459        len=$(git ls-files "a*" | wc -c) &&
 460        test $len = 4098
 461'
 462
 463test_done