3 # Copyright (c) 2005 Junio C Hamano
6 test_description='Test the very basics part #1.
8 The rest of the test suite does not check the basic operation of git
9 plumbing commands to work very carefully. Their job is to concentrate
10 on tricky features that caused bugs in the past to detect regression.
12 This test runs very basic features, like registering things in cache,
15 Note that this test *deliberately* hard-codes many expected object
16 IDs. When object ID computation changes, like in the previous case of
17 swapping compression and hashing order, the person who is making the
18 modification *should* take notice and update the test vectors here.
23 ################################################################
24 # git init has been done in an empty repository.
25 # make sure it is empty.
27 test_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
32 # also it should have 2 subdirectories; no fan-out anymore, pack, and info.
33 # 3 is counting "objects" itself
34 test_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
39 ################################################################
41 test_expect_success 'success is reported like this' '
45 run_sub_test_lib_test () {
46 name="$1" descr="$2" # stdin is the body of the test code
50 # Pretend we're not running under a test harness, whether we
51 # are or not. The test-lib output depends on the setting of
52 # this variable, so we need a stable setting under which to run
54 sane_unset HARNESS_ACTIVE &&
56 cat >"$name.sh" <<-EOF &&
59 test_description='$descr (run in sub test-lib)
61 This is run in a sub test-lib so that we do not get incorrect
65 # Point to the t/test-lib.sh, which isn't in ../ as usual
66 . "\$TEST_DIRECTORY"/test-lib.sh
69 chmod +x "$name.sh" &&
70 export TEST_DIRECTORY &&
71 TEST_OUTPUT_DIRECTORY=$(pwd) &&
72 export TEST_OUTPUT_DIRECTORY &&
73 ./"$name.sh" "$@" >out 2>err
77 check_sub_test_lib_test () {
78 name="$1" # stdin is the expected output from the test
82 sed -e 's/^> //' -e 's/Z$//' >expect &&
87 test_expect_success 'pretend we have a fully passing test suite' "
88 run_sub_test_lib_test full-pass '3 passing tests' <<-\\EOF &&
91 test_expect_success \"passing test #\$i\" 'true'
95 check_sub_test_lib_test full-pass <<-\\EOF
96 > ok 1 - passing test #1
97 > ok 2 - passing test #2
98 > ok 3 - passing test #3
99 > # passed all 3 test(s)
104 test_expect_success 'pretend we have a partially passing test suite' "
105 test_must_fail run_sub_test_lib_test \
106 partial-pass '2/3 tests passing' <<-\\EOF &&
107 test_expect_success 'passing test #1' 'true'
108 test_expect_success 'failing test #2' 'false'
109 test_expect_success 'passing test #3' 'true'
112 check_sub_test_lib_test partial-pass <<-\\EOF
113 > ok 1 - passing test #1
114 > not ok 2 - failing test #2
116 > ok 3 - passing test #3
117 > # failed 1 among 3 test(s)
122 test_expect_success 'pretend we have a known breakage' "
123 run_sub_test_lib_test failing-todo 'A failing TODO test' <<-\\EOF &&
124 test_expect_success 'passing test' 'true'
125 test_expect_failure 'pretend we have a known breakage' 'false'
128 check_sub_test_lib_test failing-todo <<-\\EOF
129 > ok 1 - passing test
130 > not ok 2 - pretend we have a known breakage # TODO known breakage
131 > # still have 1 known breakage(s)
132 > # passed all remaining 1 test(s)
137 test_expect_success 'pretend we have fixed a known breakage' "
138 run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
139 test_expect_failure 'pretend we have fixed a known breakage' 'true'
142 check_sub_test_lib_test passing-todo <<-\\EOF
143 > ok 1 - pretend we have fixed a known breakage # TODO known breakage vanished
144 > # 1 known breakage(s) vanished; please update test(s)
149 test_expect_success 'pretend we have fixed one of two known breakages (run in sub test-lib)' "
150 run_sub_test_lib_test partially-passing-todos \
151 '2 TODO tests, one passing' <<-\\EOF &&
152 test_expect_failure 'pretend we have a known breakage' 'false'
153 test_expect_success 'pretend we have a passing test' 'true'
154 test_expect_failure 'pretend we have fixed another known breakage' 'true'
157 check_sub_test_lib_test partially-passing-todos <<-\\EOF
158 > not ok 1 - pretend we have a known breakage # TODO known breakage
159 > ok 2 - pretend we have a passing test
160 > ok 3 - pretend we have fixed another known breakage # TODO known breakage vanished
161 > # 1 known breakage(s) vanished; please update test(s)
162 > # still have 1 known breakage(s)
163 > # passed all remaining 1 test(s)
168 test_expect_success 'pretend we have a pass, fail, and known breakage' "
169 test_must_fail run_sub_test_lib_test \
170 mixed-results1 'mixed results #1' <<-\\EOF &&
171 test_expect_success 'passing test' 'true'
172 test_expect_success 'failing test' 'false'
173 test_expect_failure 'pretend we have a known breakage' 'false'
176 check_sub_test_lib_test mixed-results1 <<-\\EOF
177 > ok 1 - passing test
178 > not ok 2 - failing test
180 > not ok 3 - pretend we have a known breakage # TODO known breakage
181 > # still have 1 known breakage(s)
182 > # failed 1 among remaining 2 test(s)
187 test_expect_success 'pretend we have a mix of all possible results' "
188 test_must_fail run_sub_test_lib_test \
189 mixed-results2 'mixed results #2' <<-\\EOF &&
190 test_expect_success 'passing test' 'true'
191 test_expect_success 'passing test' 'true'
192 test_expect_success 'passing test' 'true'
193 test_expect_success 'passing test' 'true'
194 test_expect_success 'failing test' 'false'
195 test_expect_success 'failing test' 'false'
196 test_expect_success 'failing test' 'false'
197 test_expect_failure 'pretend we have a known breakage' 'false'
198 test_expect_failure 'pretend we have a known breakage' 'false'
199 test_expect_failure 'pretend we have fixed a known breakage' 'true'
202 check_sub_test_lib_test mixed-results2 <<-\\EOF
203 > ok 1 - passing test
204 > ok 2 - passing test
205 > ok 3 - passing test
206 > ok 4 - passing test
207 > not ok 5 - failing test
209 > not ok 6 - failing test
211 > not ok 7 - failing test
213 > not ok 8 - pretend we have a known breakage # TODO known breakage
214 > not ok 9 - pretend we have a known breakage # TODO known breakage
215 > ok 10 - pretend we have fixed a known breakage # TODO known breakage vanished
216 > # 1 known breakage(s) vanished; please update test(s)
217 > # still have 2 known breakage(s)
218 > # failed 3 among remaining 7 test(s)
223 test_expect_success 'test --verbose' '
224 test_must_fail run_sub_test_lib_test \
225 test-verbose "test verbose" --verbose <<-\EOF &&
226 test_expect_success "passing test" true
227 test_expect_success "test with output" "echo foo"
228 test_expect_success "failing test" false
231 mv test-verbose/out test-verbose/out+
232 grep -v "^Initialized empty" test-verbose/out+ >test-verbose/out &&
233 check_sub_test_lib_test test-verbose <<-\EOF
234 > expecting success: true
235 > ok 1 - passing test
237 > expecting success: echo foo
239 > ok 2 - test with output
241 > expecting success: false
242 > not ok 3 - failing test
245 > # failed 1 among 3 test(s)
250 test_expect_success 'test --verbose-only' '
251 test_must_fail run_sub_test_lib_test \
252 test-verbose-only-2 "test verbose-only=2" \
253 --verbose-only=2 <<-\EOF &&
254 test_expect_success "passing test" true
255 test_expect_success "test with output" "echo foo"
256 test_expect_success "failing test" false
259 check_sub_test_lib_test test-verbose-only-2 <<-\EOF
260 > ok 1 - passing test
262 > expecting success: echo foo
264 > ok 2 - test with output
266 > not ok 3 - failing test
268 > # failed 1 among 3 test(s)
273 test_set_prereq HAVEIT
275 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
276 test_have_prereq HAVEIT &&
280 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
283 if test $haveit$donthaveit != yesyes
285 say "bug in test framework: prerequisite tags do not work reliably"
289 test_set_prereq HAVETHIS
291 test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
292 test_have_prereq HAVEIT &&
293 test_have_prereq HAVETHIS &&
297 test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
301 test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
304 if test $haveit$donthaveit$donthaveiteither != yesyesyes
306 say "bug in test framework: multiple prerequisite tags do not work reliably"
310 test_lazy_prereq LAZY_TRUE true
312 test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
316 test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
320 if test "$havetrue$donthavetrue" != yesyes
322 say 'bug in test framework: lazy prerequisites do not work'
326 test_lazy_prereq LAZY_FALSE false
328 test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
332 test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
336 if test "$nothavefalse$havefalse" != yesyes
338 say 'bug in test framework: negative lazy prerequisites do not work'
343 test_expect_success 'tests clean up after themselves' '
344 test_when_finished clean=yes
347 if test $clean != yes
349 say "bug in test framework: basic cleanup command does not work reliably"
353 test_expect_success 'tests clean up even on failures' "
354 test_must_fail run_sub_test_lib_test \
355 failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
356 test_expect_success 'tests clean up even after a failure' '
357 touch clean-after-failure &&
358 test_when_finished rm clean-after-failure &&
361 test_expect_success 'failure to clean up causes the test to fail' '
362 test_when_finished \"(exit 2)\"
366 check_sub_test_lib_test failing-cleanup <<-\\EOF
367 > not ok 1 - tests clean up even after a failure
369 > # touch clean-after-failure &&
370 > # test_when_finished rm clean-after-failure &&
373 > not ok 2 - failure to clean up causes the test to fail
375 > # test_when_finished \"(exit 2)\"
377 > # failed 2 among 2 test(s)
382 ################################################################
383 # Basics of the basics
385 # updating a new file without --add should fail.
386 test_expect_success 'git update-index without --add should fail adding' '
387 test_must_fail git update-index should-be-empty
390 # and with --add it should succeed, even if it is empty (it used to fail).
391 test_expect_success 'git update-index with --add should succeed' '
392 git update-index --add should-be-empty
395 test_expect_success 'writing tree out with git write-tree' '
396 tree=$(git write-tree)
399 # we know the shape and contents of the tree and know the object ID for it.
400 test_expect_success 'validate object ID of a known tree' '
401 test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
405 test_expect_success 'git update-index without --remove should fail removing' '
406 rm -f should-be-empty full-of-directories &&
407 test_must_fail git update-index should-be-empty
410 test_expect_success 'git update-index with --remove should be able to remove' '
411 git update-index --remove should-be-empty
414 # Empty tree can be written with recent write-tree.
415 test_expect_success 'git write-tree should be able to write an empty tree' '
416 tree=$(git write-tree)
419 test_expect_success 'validate object ID of a known tree' '
420 test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
423 # Various types of objects
425 test_expect_success 'adding various types of objects with git update-index --add' '
426 mkdir path2 path3 path3/subp3 &&
427 paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
431 echo "hello $p" >$p || exit 1
432 test_ln_s_add "hello $p" ${p}sym || exit 1
435 find path* ! -type d -print | xargs git update-index --add
438 # Show them and see that matches what we expect.
439 test_expect_success 'showing stage with git ls-files --stage' '
440 git ls-files --stage >current
443 test_expect_success 'validate git ls-files output for a known tree' '
444 cat >expected <<-\EOF &&
445 100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
446 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
447 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
448 120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
449 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
450 120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
451 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
452 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
454 test_cmp expected current
457 test_expect_success 'writing tree out with git write-tree' '
458 tree=$(git write-tree)
461 test_expect_success 'validate object ID for a known tree' '
462 test "$tree" = 087704a96baf1c2d1c869a8b084481e121c88b5b
465 test_expect_success 'showing tree with git ls-tree' '
466 git ls-tree $tree >current
469 test_expect_success 'git ls-tree output for a known tree' '
470 cat >expected <<-\EOF &&
471 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
472 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
473 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
474 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
476 test_cmp expected current
479 # This changed in ls-tree pathspec change -- recursive does
480 # not show tree nodes anymore.
481 test_expect_success 'showing tree with git ls-tree -r' '
482 git ls-tree -r $tree >current
485 test_expect_success 'git ls-tree -r output for a known tree' '
486 cat >expected <<-\EOF &&
487 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
488 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
489 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
490 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
491 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
492 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
493 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
494 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
496 test_cmp expected current
499 # But with -r -t we can have both.
500 test_expect_success 'showing tree with git ls-tree -r -t' '
501 git ls-tree -r -t $tree >current
504 test_expect_success 'git ls-tree -r output for a known tree' '
505 cat >expected <<-\EOF &&
506 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
507 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
508 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
509 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
510 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
511 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
512 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
513 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
514 040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
515 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
516 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
518 test_cmp expected current
521 test_expect_success 'writing partial tree out with git write-tree --prefix' '
522 ptree=$(git write-tree --prefix=path3)
525 test_expect_success 'validate object ID for a known tree' '
526 test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3
529 test_expect_success 'writing partial tree out with git write-tree --prefix' '
530 ptree=$(git write-tree --prefix=path3/subp3)
533 test_expect_success 'validate object ID for a known tree' '
534 test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2
537 test_expect_success 'put invalid objects into the index' '
539 cat >badobjects <<-\EOF &&
540 100644 blob 1000000000000000000000000000000000000000 dir/file1
541 100644 blob 2000000000000000000000000000000000000000 dir/file2
542 100644 blob 3000000000000000000000000000000000000000 dir/file3
543 100644 blob 4000000000000000000000000000000000000000 dir/file4
544 100644 blob 5000000000000000000000000000000000000000 dir/file5
546 git update-index --index-info <badobjects
549 test_expect_success 'writing this tree without --missing-ok' '
550 test_must_fail git write-tree
553 test_expect_success 'writing this tree with --missing-ok' '
554 git write-tree --missing-ok
558 ################################################################
559 test_expect_success 'git read-tree followed by write-tree should be idempotent' '
561 git read-tree $tree &&
562 test -f .git/index &&
563 newtree=$(git write-tree) &&
564 test "$newtree" = "$tree"
567 test_expect_success 'validate git diff-files output for a know cache/work tree state' '
568 cat >expected <<\EOF &&
569 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
570 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
571 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
572 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
573 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
574 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
575 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
576 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
578 git diff-files >current &&
579 test_cmp current expected
582 test_expect_success 'git update-index --refresh should succeed' '
583 git update-index --refresh
586 test_expect_success 'no diff after checkout and git update-index --refresh' '
587 git diff-files >current &&
588 cmp -s current /dev/null
591 ################################################################
592 P=087704a96baf1c2d1c869a8b084481e121c88b5b
594 test_expect_success 'git commit-tree records the correct tree in a commit' '
595 commit0=$(echo NO | git commit-tree $P) &&
596 tree=$(git show --pretty=raw $commit0 |
597 sed -n -e "s/^tree //p" -e "/^author /q") &&
598 test "z$tree" = "z$P"
601 test_expect_success 'git commit-tree records the correct parent in a commit' '
602 commit1=$(echo NO | git commit-tree $P -p $commit0) &&
603 parent=$(git show --pretty=raw $commit1 |
604 sed -n -e "s/^parent //p" -e "/^author /q") &&
605 test "z$commit0" = "z$parent"
608 test_expect_success 'git commit-tree omits duplicated parent in a commit' '
609 commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
610 parent=$(git show --pretty=raw $commit2 |
611 sed -n -e "s/^parent //p" -e "/^author /q" |
613 test "z$commit0" = "z$parent" &&
614 numparent=$(git show --pretty=raw $commit2 |
615 sed -n -e "s/^parent //p" -e "/^author /q" |
620 test_expect_success 'update-index D/F conflict' '
624 git update-index --add --replace path2 path0/file2 &&
625 numpath0=$(git ls-files path0 | wc -l) &&
629 test_expect_success 'very long name in the index handled sanely' '
632 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
633 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
634 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
638 git update-index --add path4 &&
640 git ls-files -s path4 |
644 ) | git update-index --index-info &&
645 len=$(git ls-files "a*" | wc -c) &&