t / t0000-basic.shon commit test-lib: Add support for multiple test prerequisites (93a5724)
   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
  37find .git/objects -type f -print >should-be-empty
  38test_expect_success \
  39    '.git/objects should be empty after git init in an empty repo.' \
  40    'cmp -s /dev/null should-be-empty'
  41
  42# also it should have 2 subdirectories; no fan-out anymore, pack, and info.
  43# 3 is counting "objects" itself
  44find .git/objects -type d -print >full-of-directories
  45test_expect_success \
  46    '.git/objects should have 3 subdirectories.' \
  47    'test $(wc -l < full-of-directories) = 3'
  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'
  57test_expect_failure 'pretend we have fixed a known breakage' '
  58    :
  59'
  60test_set_prereq HAVEIT
  61haveit=no
  62test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
  63    test_have_prereq HAVEIT &&
  64    haveit=yes
  65'
  66donthaveit=yes
  67test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
  68    donthaveit=no
  69'
  70if test $haveit$donthaveit != yesyes
  71then
  72        say "bug in test framework: prerequisite tags do not work reliably"
  73        exit 1
  74fi
  75
  76test_set_prereq HAVETHIS
  77haveit=no
  78test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
  79    test_have_prereq HAVEIT &&
  80    test_have_prereq HAVETHIS &&
  81    haveit=yes
  82'
  83donthaveit=yes
  84test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
  85    donthaveit=no
  86'
  87if test $haveit$donthaveit != yesyes
  88then
  89        say "bug in test framework: multiple prerequisite tags do not work reliably"
  90        exit 1
  91fi
  92
  93clean=no
  94test_expect_success 'tests clean up after themselves' '
  95    test_when_finished clean=yes
  96'
  97
  98cleaner=no
  99test_expect_code 1 'tests clean up even after a failure' '
 100    test_when_finished cleaner=yes &&
 101    (exit 1)
 102'
 103
 104if test $clean$cleaner != yesyes
 105then
 106        say "bug in test framework: cleanup commands do not work reliably"
 107        exit 1
 108fi
 109
 110test_expect_code 2 'failure to clean up causes the test to fail' '
 111    test_when_finished "(exit 2)"
 112'
 113
 114################################################################
 115# Basics of the basics
 116
 117# updating a new file without --add should fail.
 118test_expect_success 'git update-index without --add should fail adding.' '
 119    test_must_fail git update-index should-be-empty
 120'
 121
 122# and with --add it should succeed, even if it is empty (it used to fail).
 123test_expect_success \
 124    'git update-index with --add should succeed.' \
 125    'git update-index --add should-be-empty'
 126
 127test_expect_success \
 128    'writing tree out with git write-tree' \
 129    'tree=$(git write-tree)'
 130
 131# we know the shape and contents of the tree and know the object ID for it.
 132test_expect_success \
 133    'validate object ID of a known tree.' \
 134    'test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a'
 135
 136# Removing paths.
 137rm -f should-be-empty full-of-directories
 138test_expect_success 'git update-index without --remove should fail removing.' '
 139    test_must_fail git update-index should-be-empty
 140'
 141
 142test_expect_success \
 143    'git update-index with --remove should be able to remove.' \
 144    'git update-index --remove should-be-empty'
 145
 146# Empty tree can be written with recent write-tree.
 147test_expect_success \
 148    'git write-tree should be able to write an empty tree.' \
 149    'tree=$(git write-tree)'
 150
 151test_expect_success \
 152    'validate object ID of a known tree.' \
 153    'test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904'
 154
 155# Various types of objects
 156# Some filesystems do not support symblic links; on such systems
 157# some expected values are different
 158mkdir path2 path3 path3/subp3
 159paths='path0 path2/file2 path3/file3 path3/subp3/file3'
 160for p in $paths
 161do
 162    echo "hello $p" >$p
 163done
 164if test_have_prereq SYMLINKS
 165then
 166        for p in $paths
 167        do
 168                ln -s "hello $p" ${p}sym
 169        done
 170        expectfilter=cat
 171        expectedtree=087704a96baf1c2d1c869a8b084481e121c88b5b
 172        expectedptree1=21ae8269cacbe57ae09138dcc3a2887f904d02b3
 173        expectedptree2=3c5e5399f3a333eddecce7a9b9465b63f65f51e2
 174else
 175        expectfilter='grep -v sym'
 176        expectedtree=8e18edf7d7edcf4371a3ac6ae5f07c2641db7c46
 177        expectedptree1=cfb8591b2f65de8b8cc1020cd7d9e67e7793b325
 178        expectedptree2=ce580448f0148b985a513b693fdf7d802cacb44f
 179fi
 180
 181test_expect_success \
 182    'adding various types of objects with git update-index --add.' \
 183    'find path* ! -type d -print | xargs git update-index --add'
 184
 185# Show them and see that matches what we expect.
 186test_expect_success \
 187    'showing stage with git ls-files --stage' \
 188    'git ls-files --stage >current'
 189
 190$expectfilter >expected <<\EOF
 191100644 f87290f8eb2cbbea7857214459a0739927eab154 0       path0
 192120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0       path0sym
 193100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0       path2/file2
 194120000 d8ce161addc5173867a3c3c730924388daedbc38 0       path2/file2sym
 195100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0       path3/file3
 196120000 8599103969b43aff7e430efea79ca4636466794f 0       path3/file3sym
 197100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0       path3/subp3/file3
 198120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0       path3/subp3/file3sym
 199EOF
 200test_expect_success \
 201    'validate git ls-files output for a known tree.' \
 202    'test_cmp expected current'
 203
 204test_expect_success \
 205    'writing tree out with git write-tree.' \
 206    'tree=$(git write-tree)'
 207test_expect_success \
 208    'validate object ID for a known tree.' \
 209    'test "$tree" = "$expectedtree"'
 210
 211test_expect_success \
 212    'showing tree with git ls-tree' \
 213    'git ls-tree $tree >current'
 214cat >expected <<\EOF
 215100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 216120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 217040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 218040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 219EOF
 220test_expect_success SYMLINKS \
 221    'git ls-tree output for a known tree.' \
 222    'test_cmp expected current'
 223
 224# This changed in ls-tree pathspec change -- recursive does
 225# not show tree nodes anymore.
 226test_expect_success \
 227    'showing tree with git ls-tree -r' \
 228    'git ls-tree -r $tree >current'
 229$expectfilter >expected <<\EOF
 230100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 231120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 232100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 233120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 234100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 235120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 236100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 237120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 238EOF
 239test_expect_success \
 240    'git ls-tree -r output for a known tree.' \
 241    'test_cmp expected current'
 242
 243# But with -r -t we can have both.
 244test_expect_success \
 245    'showing tree with git ls-tree -r -t' \
 246    'git ls-tree -r -t $tree >current'
 247cat >expected <<\EOF
 248100644 blob f87290f8eb2cbbea7857214459a0739927eab154    path0
 249120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01    path0sym
 250040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe    path2
 251100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7    path2/file2
 252120000 blob d8ce161addc5173867a3c3c730924388daedbc38    path2/file2sym
 253040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3    path3
 254100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376    path3/file3
 255120000 blob 8599103969b43aff7e430efea79ca4636466794f    path3/file3sym
 256040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2    path3/subp3
 257100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f    path3/subp3/file3
 258120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c    path3/subp3/file3sym
 259EOF
 260test_expect_success SYMLINKS \
 261    'git ls-tree -r output for a known tree.' \
 262    'test_cmp expected current'
 263
 264test_expect_success \
 265    'writing partial tree out with git write-tree --prefix.' \
 266    'ptree=$(git write-tree --prefix=path3)'
 267test_expect_success \
 268    'validate object ID for a known tree.' \
 269    'test "$ptree" = "$expectedptree1"'
 270
 271test_expect_success \
 272    'writing partial tree out with git write-tree --prefix.' \
 273    'ptree=$(git write-tree --prefix=path3/subp3)'
 274test_expect_success \
 275    'validate object ID for a known tree.' \
 276    'test "$ptree" = "$expectedptree2"'
 277
 278cat >badobjects <<EOF
 279100644 blob 1000000000000000000000000000000000000000    dir/file1
 280100644 blob 2000000000000000000000000000000000000000    dir/file2
 281100644 blob 3000000000000000000000000000000000000000    dir/file3
 282100644 blob 4000000000000000000000000000000000000000    dir/file4
 283100644 blob 5000000000000000000000000000000000000000    dir/file5
 284EOF
 285
 286rm .git/index
 287test_expect_success \
 288    'put invalid objects into the index.' \
 289    'git update-index --index-info < badobjects'
 290
 291test_expect_success 'writing this tree without --missing-ok.' '
 292    test_must_fail git write-tree
 293'
 294
 295test_expect_success \
 296    'writing this tree with --missing-ok.' \
 297    'git write-tree --missing-ok'
 298
 299
 300################################################################
 301rm .git/index
 302test_expect_success \
 303    'git read-tree followed by write-tree should be idempotent.' \
 304    'git read-tree $tree &&
 305     test -f .git/index &&
 306     newtree=$(git write-tree) &&
 307     test "$newtree" = "$tree"'
 308
 309$expectfilter >expected <<\EOF
 310:100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M      path0
 311:120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M      path0sym
 312:100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M      path2/file2
 313:120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M      path2/file2sym
 314:100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M      path3/file3
 315:120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M      path3/file3sym
 316:100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M      path3/subp3/file3
 317:120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M      path3/subp3/file3sym
 318EOF
 319test_expect_success \
 320    'validate git diff-files output for a know cache/work tree state.' \
 321    'git diff-files >current && test_cmp current expected >/dev/null'
 322
 323test_expect_success \
 324    'git update-index --refresh should succeed.' \
 325    'git update-index --refresh'
 326
 327test_expect_success \
 328    'no diff after checkout and git update-index --refresh.' \
 329    'git diff-files >current && cmp -s current /dev/null'
 330
 331################################################################
 332P=$expectedtree
 333test_expect_success \
 334    'git commit-tree records the correct tree in a commit.' \
 335    'commit0=$(echo NO | git commit-tree $P) &&
 336     tree=$(git show --pretty=raw $commit0 |
 337         sed -n -e "s/^tree //p" -e "/^author /q") &&
 338     test "z$tree" = "z$P"'
 339
 340test_expect_success \
 341    'git commit-tree records the correct parent in a commit.' \
 342    'commit1=$(echo NO | git commit-tree $P -p $commit0) &&
 343     parent=$(git show --pretty=raw $commit1 |
 344         sed -n -e "s/^parent //p" -e "/^author /q") &&
 345     test "z$commit0" = "z$parent"'
 346
 347test_expect_success \
 348    'git commit-tree omits duplicated parent in a commit.' \
 349    'commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
 350     parent=$(git show --pretty=raw $commit2 |
 351         sed -n -e "s/^parent //p" -e "/^author /q" |
 352         sort -u) &&
 353     test "z$commit0" = "z$parent" &&
 354     numparent=$(git show --pretty=raw $commit2 |
 355         sed -n -e "s/^parent //p" -e "/^author /q" |
 356         wc -l) &&
 357     test $numparent = 1'
 358
 359test_expect_success 'update-index D/F conflict' '
 360        mv path0 tmp &&
 361        mv path2 path0 &&
 362        mv tmp path2 &&
 363        git update-index --add --replace path2 path0/file2 &&
 364        numpath0=$(git ls-files path0 | wc -l) &&
 365        test $numpath0 = 1
 366'
 367
 368test_expect_success SYMLINKS 'absolute path works as expected' '
 369        mkdir first &&
 370        ln -s ../.git first/.git &&
 371        mkdir second &&
 372        ln -s ../first second/other &&
 373        mkdir third &&
 374        dir="$(cd .git; pwd -P)" &&
 375        dir2=third/../second/other/.git &&
 376        test "$dir" = "$(test-path-utils make_absolute_path $dir2)" &&
 377        file="$dir"/index &&
 378        test "$file" = "$(test-path-utils make_absolute_path $dir2/index)" &&
 379        basename=blub &&
 380        test "$dir/$basename" = "$(cd .git && test-path-utils make_absolute_path "$basename")" &&
 381        ln -s ../first/file .git/syml &&
 382        sym="$(cd first; pwd -P)"/file &&
 383        test "$sym" = "$(test-path-utils make_absolute_path "$dir2/syml")"
 384'
 385
 386test_expect_success 'very long name in the index handled sanely' '
 387
 388        a=a && # 1
 389        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
 390        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
 391        a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
 392        a=${a}q &&
 393
 394        >path4 &&
 395        git update-index --add path4 &&
 396        (
 397                git ls-files -s path4 |
 398                sed -e "s/      .*/     /" |
 399                tr -d "\012"
 400                echo "$a"
 401        ) | git update-index --index-info &&
 402        len=$(git ls-files "a*" | wc -c) &&
 403        test $len = 4098
 404'
 405
 406test_done