t0000: set TEST_OUTPUT_DIRECTORY for sub-tests
[git.git] / t / t0000-basic.sh
blobbc4e3e27b555ca1d71e8ff3b98f2220066527336
1 #!/bin/sh
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,
13 writing tree, etc.
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.
21 . ./test-lib.sh
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 ################################################################
40 # Test harness
41 test_expect_success 'success is reported like this' '
44 test_expect_failure 'pretend we have a known breakage' '
45 false
48 run_sub_test_lib_test () {
49 name="$1" descr="$2" # stdin is the body of the test code
50 shift 2
51 mkdir "$name" &&
53 # Pretend we're a test harness. This prevents
54 # test-lib from writing the counts to a file that will
55 # later be summarized, showing spurious "failed" tests
56 HARNESS_ACTIVE=t &&
57 export HARNESS_ACTIVE &&
58 cd "$name" &&
59 cat >"$name.sh" <<-EOF &&
60 #!$SHELL_PATH
62 test_description='$descr (run in sub test-lib)
64 This is run in a sub test-lib so that we do not get incorrect
65 passing metrics
68 # Point to the t/test-lib.sh, which isn't in ../ as usual
69 . "\$TEST_DIRECTORY"/test-lib.sh
70 EOF
71 cat >>"$name.sh" &&
72 chmod +x "$name.sh" &&
73 export TEST_DIRECTORY &&
74 TEST_OUTPUT_DIRECTORY=$(pwd) &&
75 export TEST_OUTPUT_DIRECTORY &&
76 ./"$name.sh" "$@" >out 2>err
80 check_sub_test_lib_test () {
81 name="$1" # stdin is the expected output from the test
83 cd "$name" &&
84 ! test -s err &&
85 sed -e 's/^> //' -e 's/Z$//' >expect &&
86 test_cmp expect out
90 test_expect_success 'pretend we have a fully passing test suite' "
91 run_sub_test_lib_test full-pass '3 passing tests' <<-\\EOF &&
92 for i in 1 2 3
94 test_expect_success \"passing test #\$i\" 'true'
95 done
96 test_done
97 EOF
98 check_sub_test_lib_test full-pass <<-\\EOF
99 > ok 1 - passing test #1
100 > ok 2 - passing test #2
101 > ok 3 - passing test #3
102 > # passed all 3 test(s)
103 > 1..3
107 test_expect_success 'pretend we have a partially passing test suite' "
108 test_must_fail run_sub_test_lib_test \
109 partial-pass '2/3 tests passing' <<-\\EOF &&
110 test_expect_success 'passing test #1' 'true'
111 test_expect_success 'failing test #2' 'false'
112 test_expect_success 'passing test #3' 'true'
113 test_done
115 check_sub_test_lib_test partial-pass <<-\\EOF
116 > ok 1 - passing test #1
117 > not ok 2 - failing test #2
118 # false
119 > ok 3 - passing test #3
120 > # failed 1 among 3 test(s)
121 > 1..3
125 test_expect_success 'pretend we have a known breakage' "
126 run_sub_test_lib_test failing-todo 'A failing TODO test' <<-\\EOF &&
127 test_expect_success 'passing test' 'true'
128 test_expect_failure 'pretend we have a known breakage' 'false'
129 test_done
131 check_sub_test_lib_test failing-todo <<-\\EOF
132 > ok 1 - passing test
133 > not ok 2 - pretend we have a known breakage # TODO known breakage
134 > # still have 1 known breakage(s)
135 > # passed all remaining 1 test(s)
136 > 1..2
140 test_expect_success 'pretend we have fixed a known breakage' "
141 run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
142 test_expect_failure 'pretend we have fixed a known breakage' 'true'
143 test_done
145 check_sub_test_lib_test passing-todo <<-\\EOF
146 > ok 1 - pretend we have fixed a known breakage # TODO known breakage vanished
147 > # 1 known breakage(s) vanished; please update test(s)
148 > 1..1
152 test_expect_success 'pretend we have fixed one of two known breakages (run in sub test-lib)' "
153 run_sub_test_lib_test partially-passing-todos \
154 '2 TODO tests, one passing' <<-\\EOF &&
155 test_expect_failure 'pretend we have a known breakage' 'false'
156 test_expect_success 'pretend we have a passing test' 'true'
157 test_expect_failure 'pretend we have fixed another known breakage' 'true'
158 test_done
160 check_sub_test_lib_test partially-passing-todos <<-\\EOF
161 > not ok 1 - pretend we have a known breakage # TODO known breakage
162 > ok 2 - pretend we have a passing test
163 > ok 3 - pretend we have fixed another known breakage # TODO known breakage vanished
164 > # 1 known breakage(s) vanished; please update test(s)
165 > # still have 1 known breakage(s)
166 > # passed all remaining 1 test(s)
167 > 1..3
171 test_expect_success 'pretend we have a pass, fail, and known breakage' "
172 test_must_fail run_sub_test_lib_test \
173 mixed-results1 'mixed results #1' <<-\\EOF &&
174 test_expect_success 'passing test' 'true'
175 test_expect_success 'failing test' 'false'
176 test_expect_failure 'pretend we have a known breakage' 'false'
177 test_done
179 check_sub_test_lib_test mixed-results1 <<-\\EOF
180 > ok 1 - passing test
181 > not ok 2 - failing test
182 > # false
183 > not ok 3 - pretend we have a known breakage # TODO known breakage
184 > # still have 1 known breakage(s)
185 > # failed 1 among remaining 2 test(s)
186 > 1..3
190 test_expect_success 'pretend we have a mix of all possible results' "
191 test_must_fail run_sub_test_lib_test \
192 mixed-results2 'mixed results #2' <<-\\EOF &&
193 test_expect_success 'passing test' 'true'
194 test_expect_success 'passing test' 'true'
195 test_expect_success 'passing test' 'true'
196 test_expect_success 'passing test' 'true'
197 test_expect_success 'failing test' 'false'
198 test_expect_success 'failing test' 'false'
199 test_expect_success 'failing test' 'false'
200 test_expect_failure 'pretend we have a known breakage' 'false'
201 test_expect_failure 'pretend we have a known breakage' 'false'
202 test_expect_failure 'pretend we have fixed a known breakage' 'true'
203 test_done
205 check_sub_test_lib_test mixed-results2 <<-\\EOF
206 > ok 1 - passing test
207 > ok 2 - passing test
208 > ok 3 - passing test
209 > ok 4 - passing test
210 > not ok 5 - failing test
211 > # false
212 > not ok 6 - failing test
213 > # false
214 > not ok 7 - failing test
215 > # false
216 > not ok 8 - pretend we have a known breakage # TODO known breakage
217 > not ok 9 - pretend we have a known breakage # TODO known breakage
218 > ok 10 - pretend we have fixed a known breakage # TODO known breakage vanished
219 > # 1 known breakage(s) vanished; please update test(s)
220 > # still have 2 known breakage(s)
221 > # failed 3 among remaining 7 test(s)
222 > 1..10
226 test_expect_success 'test --verbose' '
227 test_must_fail run_sub_test_lib_test \
228 test-verbose "test verbose" --verbose <<-\EOF &&
229 test_expect_success "passing test" true
230 test_expect_success "test with output" "echo foo"
231 test_expect_success "failing test" false
232 test_done
234 mv test-verbose/out test-verbose/out+
235 grep -v "^Initialized empty" test-verbose/out+ >test-verbose/out &&
236 check_sub_test_lib_test test-verbose <<-\EOF
237 > expecting success: true
239 > ok 1 - passing test
241 > expecting success: echo foo
242 > foo
244 > ok 2 - test with output
246 > expecting success: false
248 > not ok 3 - failing test
249 > # false
251 > # failed 1 among 3 test(s)
252 > 1..3
256 test_expect_success 'test --verbose-only' '
257 test_must_fail run_sub_test_lib_test \
258 test-verbose-only-2 "test verbose-only=2" \
259 --verbose-only=2 <<-\EOF &&
260 test_expect_success "passing test" true
261 test_expect_success "test with output" "echo foo"
262 test_expect_success "failing test" false
263 test_done
265 check_sub_test_lib_test test-verbose-only-2 <<-\EOF
266 > ok 1 - passing test
268 > expecting success: echo foo
269 > foo
271 > ok 2 - test with output
273 > not ok 3 - failing test
274 > # false
275 > # failed 1 among 3 test(s)
276 > 1..3
280 test_set_prereq HAVEIT
281 haveit=no
282 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
283 test_have_prereq HAVEIT &&
284 haveit=yes
286 donthaveit=yes
287 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
288 donthaveit=no
290 if test $haveit$donthaveit != yesyes
291 then
292 say "bug in test framework: prerequisite tags do not work reliably"
293 exit 1
296 test_set_prereq HAVETHIS
297 haveit=no
298 test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
299 test_have_prereq HAVEIT &&
300 test_have_prereq HAVETHIS &&
301 haveit=yes
303 donthaveit=yes
304 test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
305 donthaveit=no
307 donthaveiteither=yes
308 test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
309 donthaveiteither=no
311 if test $haveit$donthaveit$donthaveiteither != yesyesyes
312 then
313 say "bug in test framework: multiple prerequisite tags do not work reliably"
314 exit 1
317 test_lazy_prereq LAZY_TRUE true
318 havetrue=no
319 test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
320 havetrue=yes
322 donthavetrue=yes
323 test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
324 donthavetrue=no
327 if test "$havetrue$donthavetrue" != yesyes
328 then
329 say 'bug in test framework: lazy prerequisites do not work'
330 exit 1
333 test_lazy_prereq LAZY_FALSE false
334 nothavefalse=no
335 test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
336 nothavefalse=yes
338 havefalse=yes
339 test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
340 havefalse=no
343 if test "$nothavefalse$havefalse" != yesyes
344 then
345 say 'bug in test framework: negative lazy prerequisites do not work'
346 exit 1
349 clean=no
350 test_expect_success 'tests clean up after themselves' '
351 test_when_finished clean=yes
354 if test $clean != yes
355 then
356 say "bug in test framework: basic cleanup command does not work reliably"
357 exit 1
360 test_expect_success 'tests clean up even on failures' "
361 test_must_fail run_sub_test_lib_test \
362 failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
363 test_expect_success 'tests clean up even after a failure' '
364 touch clean-after-failure &&
365 test_when_finished rm clean-after-failure &&
366 (exit 1)
368 test_expect_success 'failure to clean up causes the test to fail' '
369 test_when_finished \"(exit 2)\"
371 test_done
373 check_sub_test_lib_test failing-cleanup <<-\\EOF
374 > not ok 1 - tests clean up even after a failure
375 > # Z
376 > # touch clean-after-failure &&
377 > # test_when_finished rm clean-after-failure &&
378 > # (exit 1)
379 > # Z
380 > not ok 2 - failure to clean up causes the test to fail
381 > # Z
382 > # test_when_finished \"(exit 2)\"
383 > # Z
384 > # failed 2 among 2 test(s)
385 > 1..2
389 ################################################################
390 # Basics of the basics
392 # updating a new file without --add should fail.
393 test_expect_success 'git update-index without --add should fail adding' '
394 test_must_fail git update-index should-be-empty
397 # and with --add it should succeed, even if it is empty (it used to fail).
398 test_expect_success 'git update-index with --add should succeed' '
399 git update-index --add should-be-empty
402 test_expect_success 'writing tree out with git write-tree' '
403 tree=$(git write-tree)
406 # we know the shape and contents of the tree and know the object ID for it.
407 test_expect_success 'validate object ID of a known tree' '
408 test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
411 # Removing paths.
412 test_expect_success 'git update-index without --remove should fail removing' '
413 rm -f should-be-empty full-of-directories &&
414 test_must_fail git update-index should-be-empty
417 test_expect_success 'git update-index with --remove should be able to remove' '
418 git update-index --remove should-be-empty
421 # Empty tree can be written with recent write-tree.
422 test_expect_success 'git write-tree should be able to write an empty tree' '
423 tree=$(git write-tree)
426 test_expect_success 'validate object ID of a known tree' '
427 test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
430 # Various types of objects
432 test_expect_success 'adding various types of objects with git update-index --add' '
433 mkdir path2 path3 path3/subp3 &&
434 paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
436 for p in $paths
438 echo "hello $p" >$p || exit 1
439 test_ln_s_add "hello $p" ${p}sym || exit 1
440 done
441 ) &&
442 find path* ! -type d -print | xargs git update-index --add
445 # Show them and see that matches what we expect.
446 test_expect_success 'showing stage with git ls-files --stage' '
447 git ls-files --stage >current
450 test_expect_success 'validate git ls-files output for a known tree' '
451 cat >expected <<-\EOF &&
452 100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
453 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
454 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
455 120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
456 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
457 120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
458 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
459 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
461 test_cmp expected current
464 test_expect_success 'writing tree out with git write-tree' '
465 tree=$(git write-tree)
468 test_expect_success 'validate object ID for a known tree' '
469 test "$tree" = 087704a96baf1c2d1c869a8b084481e121c88b5b
472 test_expect_success 'showing tree with git ls-tree' '
473 git ls-tree $tree >current
476 test_expect_success 'git ls-tree output for a known tree' '
477 cat >expected <<-\EOF &&
478 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
479 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
480 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
481 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
483 test_cmp expected current
486 # This changed in ls-tree pathspec change -- recursive does
487 # not show tree nodes anymore.
488 test_expect_success 'showing tree with git ls-tree -r' '
489 git ls-tree -r $tree >current
492 test_expect_success 'git ls-tree -r output for a known tree' '
493 cat >expected <<-\EOF &&
494 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
495 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
496 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
497 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
498 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
499 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
500 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
501 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
503 test_cmp expected current
506 # But with -r -t we can have both.
507 test_expect_success 'showing tree with git ls-tree -r -t' '
508 git ls-tree -r -t $tree >current
511 test_expect_success 'git ls-tree -r output for a known tree' '
512 cat >expected <<-\EOF &&
513 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
514 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
515 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
516 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
517 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
518 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
519 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
520 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
521 040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
522 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
523 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
525 test_cmp expected current
528 test_expect_success 'writing partial tree out with git write-tree --prefix' '
529 ptree=$(git write-tree --prefix=path3)
532 test_expect_success 'validate object ID for a known tree' '
533 test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3
536 test_expect_success 'writing partial tree out with git write-tree --prefix' '
537 ptree=$(git write-tree --prefix=path3/subp3)
540 test_expect_success 'validate object ID for a known tree' '
541 test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2
544 test_expect_success 'put invalid objects into the index' '
545 rm -f .git/index &&
546 cat >badobjects <<-\EOF &&
547 100644 blob 1000000000000000000000000000000000000000 dir/file1
548 100644 blob 2000000000000000000000000000000000000000 dir/file2
549 100644 blob 3000000000000000000000000000000000000000 dir/file3
550 100644 blob 4000000000000000000000000000000000000000 dir/file4
551 100644 blob 5000000000000000000000000000000000000000 dir/file5
553 git update-index --index-info <badobjects
556 test_expect_success 'writing this tree without --missing-ok' '
557 test_must_fail git write-tree
560 test_expect_success 'writing this tree with --missing-ok' '
561 git write-tree --missing-ok
565 ################################################################
566 test_expect_success 'git read-tree followed by write-tree should be idempotent' '
567 rm -f .git/index
568 git read-tree $tree &&
569 test -f .git/index &&
570 newtree=$(git write-tree) &&
571 test "$newtree" = "$tree"
574 test_expect_success 'validate git diff-files output for a know cache/work tree state' '
575 cat >expected <<\EOF &&
576 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
577 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
578 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
579 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
580 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
581 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
582 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
583 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
585 git diff-files >current &&
586 test_cmp current expected
589 test_expect_success 'git update-index --refresh should succeed' '
590 git update-index --refresh
593 test_expect_success 'no diff after checkout and git update-index --refresh' '
594 git diff-files >current &&
595 cmp -s current /dev/null
598 ################################################################
599 P=087704a96baf1c2d1c869a8b084481e121c88b5b
601 test_expect_success 'git commit-tree records the correct tree in a commit' '
602 commit0=$(echo NO | git commit-tree $P) &&
603 tree=$(git show --pretty=raw $commit0 |
604 sed -n -e "s/^tree //p" -e "/^author /q") &&
605 test "z$tree" = "z$P"
608 test_expect_success 'git commit-tree records the correct parent in a commit' '
609 commit1=$(echo NO | git commit-tree $P -p $commit0) &&
610 parent=$(git show --pretty=raw $commit1 |
611 sed -n -e "s/^parent //p" -e "/^author /q") &&
612 test "z$commit0" = "z$parent"
615 test_expect_success 'git commit-tree omits duplicated parent in a commit' '
616 commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
617 parent=$(git show --pretty=raw $commit2 |
618 sed -n -e "s/^parent //p" -e "/^author /q" |
619 sort -u) &&
620 test "z$commit0" = "z$parent" &&
621 numparent=$(git show --pretty=raw $commit2 |
622 sed -n -e "s/^parent //p" -e "/^author /q" |
623 wc -l) &&
624 test $numparent = 1
627 test_expect_success 'update-index D/F conflict' '
628 mv path0 tmp &&
629 mv path2 path0 &&
630 mv tmp path2 &&
631 git update-index --add --replace path2 path0/file2 &&
632 numpath0=$(git ls-files path0 | wc -l) &&
633 test $numpath0 = 1
636 test_expect_success 'very long name in the index handled sanely' '
638 a=a && # 1
639 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
640 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
641 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
642 a=${a}q &&
644 >path4 &&
645 git update-index --add path4 &&
647 git ls-files -s path4 |
648 sed -e "s/ .*/ /" |
649 tr -d "\012"
650 echo "$a"
651 ) | git update-index --index-info &&
652 len=$(git ls-files "a*" | wc -c) &&
653 test $len = 4098
656 test_done