Merge branch 'al/mergetool-printf-fix'
[git.git] / t / t0000-basic.sh
blobcefe33d6d1976e4017569507a6f76ad89c83e4ff
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 mkdir "$name" &&
52 cd "$name" &&
53 cat >"$name.sh" <<-EOF &&
54 #!$SHELL_PATH
56 test_description='$descr (run in sub test-lib)
58 This is run in a sub test-lib so that we do not get incorrect
59 passing metrics
62 # Point to the t/test-lib.sh, which isn't in ../ as usual
63 . "\$TEST_DIRECTORY"/test-lib.sh
64 EOF
65 cat >>"$name.sh" &&
66 chmod +x "$name.sh" &&
67 export TEST_DIRECTORY &&
68 ./"$name.sh" >out 2>err
72 check_sub_test_lib_test () {
73 name="$1" # stdin is the expected output from the test
75 cd "$name" &&
76 ! test -s err &&
77 sed -e 's/^> //' -e 's/Z$//' >expect &&
78 test_cmp expect out
82 test_expect_success 'pretend we have a fully passing test suite' "
83 run_sub_test_lib_test full-pass '3 passing tests' <<-\\EOF &&
84 for i in 1 2 3
86 test_expect_success \"passing test #\$i\" 'true'
87 done
88 test_done
89 EOF
90 check_sub_test_lib_test full-pass <<-\\EOF
91 > ok 1 - passing test #1
92 > ok 2 - passing test #2
93 > ok 3 - passing test #3
94 > # passed all 3 test(s)
95 > 1..3
96 EOF
99 test_expect_success 'pretend we have a partially passing test suite' "
100 test_must_fail run_sub_test_lib_test \
101 partial-pass '2/3 tests passing' <<-\\EOF &&
102 test_expect_success 'passing test #1' 'true'
103 test_expect_success 'failing test #2' 'false'
104 test_expect_success 'passing test #3' 'true'
105 test_done
107 check_sub_test_lib_test partial-pass <<-\\EOF
108 > ok 1 - passing test #1
109 > not ok 2 - failing test #2
110 # false
111 > ok 3 - passing test #3
112 > # failed 1 among 3 test(s)
113 > 1..3
117 test_expect_success 'pretend we have a known breakage' "
118 run_sub_test_lib_test failing-todo 'A failing TODO test' <<-\\EOF &&
119 test_expect_success 'passing test' 'true'
120 test_expect_failure 'pretend we have a known breakage' 'false'
121 test_done
123 check_sub_test_lib_test failing-todo <<-\\EOF
124 > ok 1 - passing test
125 > not ok 2 - pretend we have a known breakage # TODO known breakage
126 > # still have 1 known breakage(s)
127 > # passed all remaining 1 test(s)
128 > 1..2
132 test_expect_success 'pretend we have fixed a known breakage' "
133 run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
134 test_expect_failure 'pretend we have fixed a known breakage' 'true'
135 test_done
137 check_sub_test_lib_test passing-todo <<-\\EOF
138 > ok 1 - pretend we have fixed a known breakage # TODO known breakage vanished
139 > # 1 known breakage(s) vanished; please update test(s)
140 > 1..1
144 test_expect_success 'pretend we have fixed one of two known breakages (run in sub test-lib)' "
145 run_sub_test_lib_test partially-passing-todos \
146 '2 TODO tests, one passing' <<-\\EOF &&
147 test_expect_failure 'pretend we have a known breakage' 'false'
148 test_expect_success 'pretend we have a passing test' 'true'
149 test_expect_failure 'pretend we have fixed another known breakage' 'true'
150 test_done
152 check_sub_test_lib_test partially-passing-todos <<-\\EOF
153 > not ok 1 - pretend we have a known breakage # TODO known breakage
154 > ok 2 - pretend we have a passing test
155 > ok 3 - pretend we have fixed another known breakage # TODO known breakage vanished
156 > # 1 known breakage(s) vanished; please update test(s)
157 > # still have 1 known breakage(s)
158 > # passed all remaining 1 test(s)
159 > 1..3
163 test_expect_success 'pretend we have a pass, fail, and known breakage' "
164 test_must_fail run_sub_test_lib_test \
165 mixed-results1 'mixed results #1' <<-\\EOF &&
166 test_expect_success 'passing test' 'true'
167 test_expect_success 'failing test' 'false'
168 test_expect_failure 'pretend we have a known breakage' 'false'
169 test_done
171 check_sub_test_lib_test mixed-results1 <<-\\EOF
172 > ok 1 - passing test
173 > not ok 2 - failing test
174 > # false
175 > not ok 3 - pretend we have a known breakage # TODO known breakage
176 > # still have 1 known breakage(s)
177 > # failed 1 among remaining 2 test(s)
178 > 1..3
182 test_expect_success 'pretend we have a mix of all possible results' "
183 test_must_fail run_sub_test_lib_test \
184 mixed-results2 'mixed results #2' <<-\\EOF &&
185 test_expect_success 'passing test' 'true'
186 test_expect_success 'passing test' 'true'
187 test_expect_success 'passing test' 'true'
188 test_expect_success 'passing test' 'true'
189 test_expect_success 'failing test' 'false'
190 test_expect_success 'failing test' 'false'
191 test_expect_success 'failing test' 'false'
192 test_expect_failure 'pretend we have a known breakage' 'false'
193 test_expect_failure 'pretend we have a known breakage' 'false'
194 test_expect_failure 'pretend we have fixed a known breakage' 'true'
195 test_done
197 check_sub_test_lib_test mixed-results2 <<-\\EOF
198 > ok 1 - passing test
199 > ok 2 - passing test
200 > ok 3 - passing test
201 > ok 4 - passing test
202 > not ok 5 - failing test
203 > # false
204 > not ok 6 - failing test
205 > # false
206 > not ok 7 - failing test
207 > # false
208 > not ok 8 - pretend we have a known breakage # TODO known breakage
209 > not ok 9 - pretend we have a known breakage # TODO known breakage
210 > ok 10 - pretend we have fixed a known breakage # TODO known breakage vanished
211 > # 1 known breakage(s) vanished; please update test(s)
212 > # still have 2 known breakage(s)
213 > # failed 3 among remaining 7 test(s)
214 > 1..10
218 test_set_prereq HAVEIT
219 haveit=no
220 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
221 test_have_prereq HAVEIT &&
222 haveit=yes
224 donthaveit=yes
225 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
226 donthaveit=no
228 if test $haveit$donthaveit != yesyes
229 then
230 say "bug in test framework: prerequisite tags do not work reliably"
231 exit 1
234 test_set_prereq HAVETHIS
235 haveit=no
236 test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
237 test_have_prereq HAVEIT &&
238 test_have_prereq HAVETHIS &&
239 haveit=yes
241 donthaveit=yes
242 test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
243 donthaveit=no
245 donthaveiteither=yes
246 test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
247 donthaveiteither=no
249 if test $haveit$donthaveit$donthaveiteither != yesyesyes
250 then
251 say "bug in test framework: multiple prerequisite tags do not work reliably"
252 exit 1
255 test_lazy_prereq LAZY_TRUE true
256 havetrue=no
257 test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
258 havetrue=yes
260 donthavetrue=yes
261 test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
262 donthavetrue=no
265 if test "$havetrue$donthavetrue" != yesyes
266 then
267 say 'bug in test framework: lazy prerequisites do not work'
268 exit 1
271 test_lazy_prereq LAZY_FALSE false
272 nothavefalse=no
273 test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
274 nothavefalse=yes
276 havefalse=yes
277 test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
278 havefalse=no
281 if test "$nothavefalse$havefalse" != yesyes
282 then
283 say 'bug in test framework: negative lazy prerequisites do not work'
284 exit 1
287 clean=no
288 test_expect_success 'tests clean up after themselves' '
289 test_when_finished clean=yes
292 if test $clean != yes
293 then
294 say "bug in test framework: basic cleanup command does not work reliably"
295 exit 1
298 test_expect_success 'tests clean up even on failures' "
299 test_must_fail run_sub_test_lib_test \
300 failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
301 test_expect_success 'tests clean up even after a failure' '
302 touch clean-after-failure &&
303 test_when_finished rm clean-after-failure &&
304 (exit 1)
306 test_expect_success 'failure to clean up causes the test to fail' '
307 test_when_finished \"(exit 2)\"
309 test_done
311 check_sub_test_lib_test failing-cleanup <<-\\EOF
312 > not ok 1 - tests clean up even after a failure
313 > # Z
314 > # touch clean-after-failure &&
315 > # test_when_finished rm clean-after-failure &&
316 > # (exit 1)
317 > # Z
318 > not ok 2 - failure to clean up causes the test to fail
319 > # Z
320 > # test_when_finished \"(exit 2)\"
321 > # Z
322 > # failed 2 among 2 test(s)
323 > 1..2
327 ################################################################
328 # Basics of the basics
330 # updating a new file without --add should fail.
331 test_expect_success 'git update-index without --add should fail adding' '
332 test_must_fail git update-index should-be-empty
335 # and with --add it should succeed, even if it is empty (it used to fail).
336 test_expect_success 'git update-index with --add should succeed' '
337 git update-index --add should-be-empty
340 test_expect_success 'writing tree out with git write-tree' '
341 tree=$(git write-tree)
344 # we know the shape and contents of the tree and know the object ID for it.
345 test_expect_success 'validate object ID of a known tree' '
346 test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
349 # Removing paths.
350 test_expect_success 'git update-index without --remove should fail removing' '
351 rm -f should-be-empty full-of-directories &&
352 test_must_fail git update-index should-be-empty
355 test_expect_success 'git update-index with --remove should be able to remove' '
356 git update-index --remove should-be-empty
359 # Empty tree can be written with recent write-tree.
360 test_expect_success 'git write-tree should be able to write an empty tree' '
361 tree=$(git write-tree)
364 test_expect_success 'validate object ID of a known tree' '
365 test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
368 # Various types of objects
370 # Some filesystems do not support symblic links; on such systems
371 # some expected values are different
372 if test_have_prereq SYMLINKS
373 then
374 expectfilter=cat
375 expectedtree=087704a96baf1c2d1c869a8b084481e121c88b5b
376 expectedptree1=21ae8269cacbe57ae09138dcc3a2887f904d02b3
377 expectedptree2=3c5e5399f3a333eddecce7a9b9465b63f65f51e2
378 else
379 expectfilter='grep -v sym'
380 expectedtree=8e18edf7d7edcf4371a3ac6ae5f07c2641db7c46
381 expectedptree1=cfb8591b2f65de8b8cc1020cd7d9e67e7793b325
382 expectedptree2=ce580448f0148b985a513b693fdf7d802cacb44f
386 test_expect_success 'adding various types of objects with git update-index --add' '
387 mkdir path2 path3 path3/subp3 &&
388 paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
390 for p in $paths
392 echo "hello $p" >$p || exit 1
393 if test_have_prereq SYMLINKS
394 then
395 ln -s "hello $p" ${p}sym || exit 1
397 done
398 ) &&
399 find path* ! -type d -print | xargs git update-index --add
402 # Show them and see that matches what we expect.
403 test_expect_success 'showing stage with git ls-files --stage' '
404 git ls-files --stage >current
407 test_expect_success 'validate git ls-files output for a known tree' '
408 $expectfilter >expected <<-\EOF &&
409 100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
410 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
411 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
412 120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
413 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
414 120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
415 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
416 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
418 test_cmp expected current
421 test_expect_success 'writing tree out with git write-tree' '
422 tree=$(git write-tree)
425 test_expect_success 'validate object ID for a known tree' '
426 test "$tree" = "$expectedtree"
429 test_expect_success 'showing tree with git ls-tree' '
430 git ls-tree $tree >current
433 test_expect_success SYMLINKS 'git ls-tree output for a known tree' '
434 cat >expected <<-\EOF &&
435 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
436 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
437 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
438 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
440 test_cmp expected current
443 # This changed in ls-tree pathspec change -- recursive does
444 # not show tree nodes anymore.
445 test_expect_success 'showing tree with git ls-tree -r' '
446 git ls-tree -r $tree >current
449 test_expect_success 'git ls-tree -r output for a known tree' '
450 $expectfilter >expected <<-\EOF &&
451 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
452 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
453 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
454 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
455 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
456 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
457 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
458 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
460 test_cmp expected current
463 # But with -r -t we can have both.
464 test_expect_success 'showing tree with git ls-tree -r -t' '
465 git ls-tree -r -t $tree >current
468 test_expect_success SYMLINKS 'git ls-tree -r output for a known tree' '
469 cat >expected <<-\EOF &&
470 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
471 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
472 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
473 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
474 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
475 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
476 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
477 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
478 040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
479 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
480 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
482 test_cmp expected current
485 test_expect_success 'writing partial tree out with git write-tree --prefix' '
486 ptree=$(git write-tree --prefix=path3)
489 test_expect_success 'validate object ID for a known tree' '
490 test "$ptree" = "$expectedptree1"
493 test_expect_success 'writing partial tree out with git write-tree --prefix' '
494 ptree=$(git write-tree --prefix=path3/subp3)
497 test_expect_success 'validate object ID for a known tree' '
498 test "$ptree" = "$expectedptree2"
501 test_expect_success 'put invalid objects into the index' '
502 rm -f .git/index &&
503 cat >badobjects <<-\EOF &&
504 100644 blob 1000000000000000000000000000000000000000 dir/file1
505 100644 blob 2000000000000000000000000000000000000000 dir/file2
506 100644 blob 3000000000000000000000000000000000000000 dir/file3
507 100644 blob 4000000000000000000000000000000000000000 dir/file4
508 100644 blob 5000000000000000000000000000000000000000 dir/file5
510 git update-index --index-info <badobjects
513 test_expect_success 'writing this tree without --missing-ok' '
514 test_must_fail git write-tree
517 test_expect_success 'writing this tree with --missing-ok' '
518 git write-tree --missing-ok
522 ################################################################
523 test_expect_success 'git read-tree followed by write-tree should be idempotent' '
524 rm -f .git/index
525 git read-tree $tree &&
526 test -f .git/index &&
527 newtree=$(git write-tree) &&
528 test "$newtree" = "$tree"
531 test_expect_success 'validate git diff-files output for a know cache/work tree state' '
532 $expectfilter >expected <<\EOF &&
533 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
534 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
535 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
536 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
537 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
538 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
539 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
540 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
542 git diff-files >current &&
543 test_cmp current expected
546 test_expect_success 'git update-index --refresh should succeed' '
547 git update-index --refresh
550 test_expect_success 'no diff after checkout and git update-index --refresh' '
551 git diff-files >current &&
552 cmp -s current /dev/null
555 ################################################################
556 P=$expectedtree
558 test_expect_success 'git commit-tree records the correct tree in a commit' '
559 commit0=$(echo NO | git commit-tree $P) &&
560 tree=$(git show --pretty=raw $commit0 |
561 sed -n -e "s/^tree //p" -e "/^author /q") &&
562 test "z$tree" = "z$P"
565 test_expect_success 'git commit-tree records the correct parent in a commit' '
566 commit1=$(echo NO | git commit-tree $P -p $commit0) &&
567 parent=$(git show --pretty=raw $commit1 |
568 sed -n -e "s/^parent //p" -e "/^author /q") &&
569 test "z$commit0" = "z$parent"
572 test_expect_success 'git commit-tree omits duplicated parent in a commit' '
573 commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
574 parent=$(git show --pretty=raw $commit2 |
575 sed -n -e "s/^parent //p" -e "/^author /q" |
576 sort -u) &&
577 test "z$commit0" = "z$parent" &&
578 numparent=$(git show --pretty=raw $commit2 |
579 sed -n -e "s/^parent //p" -e "/^author /q" |
580 wc -l) &&
581 test $numparent = 1
584 test_expect_success 'update-index D/F conflict' '
585 mv path0 tmp &&
586 mv path2 path0 &&
587 mv tmp path2 &&
588 git update-index --add --replace path2 path0/file2 &&
589 numpath0=$(git ls-files path0 | wc -l) &&
590 test $numpath0 = 1
593 test_expect_success 'very long name in the index handled sanely' '
595 a=a && # 1
596 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
597 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
598 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
599 a=${a}q &&
601 >path4 &&
602 git update-index --add path4 &&
604 git ls-files -s path4 |
605 sed -e "s/ .*/ /" |
606 tr -d "\012"
607 echo "$a"
608 ) | git update-index --index-info &&
609 len=$(git ls-files "a*" | wc -c) &&
610 test $len = 4098
613 test_done