test-lib: verbose mode for only tests matching a pattern
[alt-git.git] / t / t0000-basic.sh
blob4f1844f7822f37cb841290bac48035d9e1336fd1
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 export HARNESS_ACTIVE=t &&
57 cd "$name" &&
58 cat >"$name.sh" <<-EOF &&
59 #!$SHELL_PATH
61 test_description='$descr (run in sub test-lib)
63 This is run in a sub test-lib so that we do not get incorrect
64 passing metrics
67 # Point to the t/test-lib.sh, which isn't in ../ as usual
68 . "\$TEST_DIRECTORY"/test-lib.sh
69 EOF
70 cat >>"$name.sh" &&
71 chmod +x "$name.sh" &&
72 export TEST_DIRECTORY &&
73 ./"$name.sh" "$@" >out 2>err
77 check_sub_test_lib_test () {
78 name="$1" # stdin is the expected output from the test
80 cd "$name" &&
81 ! test -s err &&
82 sed -e 's/^> //' -e 's/Z$//' >expect &&
83 test_cmp expect out
87 test_expect_success 'pretend we have a fully passing test suite' "
88 run_sub_test_lib_test full-pass '3 passing tests' <<-\\EOF &&
89 for i in 1 2 3
91 test_expect_success \"passing test #\$i\" 'true'
92 done
93 test_done
94 EOF
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)
100 > 1..3
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'
110 test_done
112 check_sub_test_lib_test partial-pass <<-\\EOF
113 > ok 1 - passing test #1
114 > not ok 2 - failing test #2
115 # false
116 > ok 3 - passing test #3
117 > # failed 1 among 3 test(s)
118 > 1..3
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'
126 test_done
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)
133 > 1..2
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'
140 test_done
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)
145 > 1..1
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'
155 test_done
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)
164 > 1..3
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'
174 test_done
176 check_sub_test_lib_test mixed-results1 <<-\\EOF
177 > ok 1 - passing test
178 > not ok 2 - failing test
179 > # false
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)
183 > 1..3
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'
200 test_done
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
208 > # false
209 > not ok 6 - failing test
210 > # false
211 > not ok 7 - failing test
212 > # false
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)
219 > 1..10
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
229 test_done
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
236 > ok 1 - passing test
238 > expecting success: echo foo
239 > foo
241 > ok 2 - test with output
243 > expecting success: false
245 > not ok 3 - failing test
246 > # false
248 > # failed 1 among 3 test(s)
249 > 1..3
253 test_expect_success 'test --verbose-only' '
254 test_must_fail run_sub_test_lib_test \
255 test-verbose-only-2 "test verbose-only=2" \
256 --verbose-only=2 <<-\EOF &&
257 test_expect_success "passing test" true
258 test_expect_success "test with output" "echo foo"
259 test_expect_success "failing test" false
260 test_done
262 check_sub_test_lib_test test-verbose-only-2 <<-\EOF
263 > ok 1 - passing test
265 > expecting success: echo foo
266 > foo
268 > ok 2 - test with output
270 > not ok 3 - failing test
271 > # false
272 > # failed 1 among 3 test(s)
273 > 1..3
277 test_set_prereq HAVEIT
278 haveit=no
279 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
280 test_have_prereq HAVEIT &&
281 haveit=yes
283 donthaveit=yes
284 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
285 donthaveit=no
287 if test $haveit$donthaveit != yesyes
288 then
289 say "bug in test framework: prerequisite tags do not work reliably"
290 exit 1
293 test_set_prereq HAVETHIS
294 haveit=no
295 test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
296 test_have_prereq HAVEIT &&
297 test_have_prereq HAVETHIS &&
298 haveit=yes
300 donthaveit=yes
301 test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
302 donthaveit=no
304 donthaveiteither=yes
305 test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
306 donthaveiteither=no
308 if test $haveit$donthaveit$donthaveiteither != yesyesyes
309 then
310 say "bug in test framework: multiple prerequisite tags do not work reliably"
311 exit 1
314 test_lazy_prereq LAZY_TRUE true
315 havetrue=no
316 test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
317 havetrue=yes
319 donthavetrue=yes
320 test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
321 donthavetrue=no
324 if test "$havetrue$donthavetrue" != yesyes
325 then
326 say 'bug in test framework: lazy prerequisites do not work'
327 exit 1
330 test_lazy_prereq LAZY_FALSE false
331 nothavefalse=no
332 test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
333 nothavefalse=yes
335 havefalse=yes
336 test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
337 havefalse=no
340 if test "$nothavefalse$havefalse" != yesyes
341 then
342 say 'bug in test framework: negative lazy prerequisites do not work'
343 exit 1
346 clean=no
347 test_expect_success 'tests clean up after themselves' '
348 test_when_finished clean=yes
351 if test $clean != yes
352 then
353 say "bug in test framework: basic cleanup command does not work reliably"
354 exit 1
357 test_expect_success 'tests clean up even on failures' "
358 test_must_fail run_sub_test_lib_test \
359 failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
360 test_expect_success 'tests clean up even after a failure' '
361 touch clean-after-failure &&
362 test_when_finished rm clean-after-failure &&
363 (exit 1)
365 test_expect_success 'failure to clean up causes the test to fail' '
366 test_when_finished \"(exit 2)\"
368 test_done
370 check_sub_test_lib_test failing-cleanup <<-\\EOF
371 > not ok 1 - tests clean up even after a failure
372 > # Z
373 > # touch clean-after-failure &&
374 > # test_when_finished rm clean-after-failure &&
375 > # (exit 1)
376 > # Z
377 > not ok 2 - failure to clean up causes the test to fail
378 > # Z
379 > # test_when_finished \"(exit 2)\"
380 > # Z
381 > # failed 2 among 2 test(s)
382 > 1..2
386 ################################################################
387 # Basics of the basics
389 # updating a new file without --add should fail.
390 test_expect_success 'git update-index without --add should fail adding' '
391 test_must_fail git update-index should-be-empty
394 # and with --add it should succeed, even if it is empty (it used to fail).
395 test_expect_success 'git update-index with --add should succeed' '
396 git update-index --add should-be-empty
399 test_expect_success 'writing tree out with git write-tree' '
400 tree=$(git write-tree)
403 # we know the shape and contents of the tree and know the object ID for it.
404 test_expect_success 'validate object ID of a known tree' '
405 test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
408 # Removing paths.
409 test_expect_success 'git update-index without --remove should fail removing' '
410 rm -f should-be-empty full-of-directories &&
411 test_must_fail git update-index should-be-empty
414 test_expect_success 'git update-index with --remove should be able to remove' '
415 git update-index --remove should-be-empty
418 # Empty tree can be written with recent write-tree.
419 test_expect_success 'git write-tree should be able to write an empty tree' '
420 tree=$(git write-tree)
423 test_expect_success 'validate object ID of a known tree' '
424 test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
427 # Various types of objects
429 # Some filesystems do not support symblic links; on such systems
430 # some expected values are different
431 if test_have_prereq SYMLINKS
432 then
433 expectfilter=cat
434 expectedtree=087704a96baf1c2d1c869a8b084481e121c88b5b
435 expectedptree1=21ae8269cacbe57ae09138dcc3a2887f904d02b3
436 expectedptree2=3c5e5399f3a333eddecce7a9b9465b63f65f51e2
437 else
438 expectfilter='grep -v sym'
439 expectedtree=8e18edf7d7edcf4371a3ac6ae5f07c2641db7c46
440 expectedptree1=cfb8591b2f65de8b8cc1020cd7d9e67e7793b325
441 expectedptree2=ce580448f0148b985a513b693fdf7d802cacb44f
445 test_expect_success 'adding various types of objects with git update-index --add' '
446 mkdir path2 path3 path3/subp3 &&
447 paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
449 for p in $paths
451 echo "hello $p" >$p || exit 1
452 if test_have_prereq SYMLINKS
453 then
454 ln -s "hello $p" ${p}sym || exit 1
456 done
457 ) &&
458 find path* ! -type d -print | xargs git update-index --add
461 # Show them and see that matches what we expect.
462 test_expect_success 'showing stage with git ls-files --stage' '
463 git ls-files --stage >current
466 test_expect_success 'validate git ls-files output for a known tree' '
467 $expectfilter >expected <<-\EOF &&
468 100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
469 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
470 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
471 120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
472 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
473 120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
474 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
475 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
477 test_cmp expected current
480 test_expect_success 'writing tree out with git write-tree' '
481 tree=$(git write-tree)
484 test_expect_success 'validate object ID for a known tree' '
485 test "$tree" = "$expectedtree"
488 test_expect_success 'showing tree with git ls-tree' '
489 git ls-tree $tree >current
492 test_expect_success SYMLINKS 'git ls-tree output for a known tree' '
493 cat >expected <<-\EOF &&
494 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
495 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
496 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
497 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
499 test_cmp expected current
502 # This changed in ls-tree pathspec change -- recursive does
503 # not show tree nodes anymore.
504 test_expect_success 'showing tree with git ls-tree -r' '
505 git ls-tree -r $tree >current
508 test_expect_success 'git ls-tree -r output for a known tree' '
509 $expectfilter >expected <<-\EOF &&
510 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
511 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
512 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
513 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
514 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
515 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
516 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
517 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
519 test_cmp expected current
522 # But with -r -t we can have both.
523 test_expect_success 'showing tree with git ls-tree -r -t' '
524 git ls-tree -r -t $tree >current
527 test_expect_success SYMLINKS 'git ls-tree -r output for a known tree' '
528 cat >expected <<-\EOF &&
529 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
530 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
531 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
532 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
533 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
534 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
535 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
536 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
537 040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
538 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
539 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
541 test_cmp expected current
544 test_expect_success 'writing partial tree out with git write-tree --prefix' '
545 ptree=$(git write-tree --prefix=path3)
548 test_expect_success 'validate object ID for a known tree' '
549 test "$ptree" = "$expectedptree1"
552 test_expect_success 'writing partial tree out with git write-tree --prefix' '
553 ptree=$(git write-tree --prefix=path3/subp3)
556 test_expect_success 'validate object ID for a known tree' '
557 test "$ptree" = "$expectedptree2"
560 test_expect_success 'put invalid objects into the index' '
561 rm -f .git/index &&
562 cat >badobjects <<-\EOF &&
563 100644 blob 1000000000000000000000000000000000000000 dir/file1
564 100644 blob 2000000000000000000000000000000000000000 dir/file2
565 100644 blob 3000000000000000000000000000000000000000 dir/file3
566 100644 blob 4000000000000000000000000000000000000000 dir/file4
567 100644 blob 5000000000000000000000000000000000000000 dir/file5
569 git update-index --index-info <badobjects
572 test_expect_success 'writing this tree without --missing-ok' '
573 test_must_fail git write-tree
576 test_expect_success 'writing this tree with --missing-ok' '
577 git write-tree --missing-ok
581 ################################################################
582 test_expect_success 'git read-tree followed by write-tree should be idempotent' '
583 rm -f .git/index
584 git read-tree $tree &&
585 test -f .git/index &&
586 newtree=$(git write-tree) &&
587 test "$newtree" = "$tree"
590 test_expect_success 'validate git diff-files output for a know cache/work tree state' '
591 $expectfilter >expected <<\EOF &&
592 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
593 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
594 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
595 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
596 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
597 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
598 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
599 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
601 git diff-files >current &&
602 test_cmp current expected
605 test_expect_success 'git update-index --refresh should succeed' '
606 git update-index --refresh
609 test_expect_success 'no diff after checkout and git update-index --refresh' '
610 git diff-files >current &&
611 cmp -s current /dev/null
614 ################################################################
615 P=$expectedtree
617 test_expect_success 'git commit-tree records the correct tree in a commit' '
618 commit0=$(echo NO | git commit-tree $P) &&
619 tree=$(git show --pretty=raw $commit0 |
620 sed -n -e "s/^tree //p" -e "/^author /q") &&
621 test "z$tree" = "z$P"
624 test_expect_success 'git commit-tree records the correct parent in a commit' '
625 commit1=$(echo NO | git commit-tree $P -p $commit0) &&
626 parent=$(git show --pretty=raw $commit1 |
627 sed -n -e "s/^parent //p" -e "/^author /q") &&
628 test "z$commit0" = "z$parent"
631 test_expect_success 'git commit-tree omits duplicated parent in a commit' '
632 commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
633 parent=$(git show --pretty=raw $commit2 |
634 sed -n -e "s/^parent //p" -e "/^author /q" |
635 sort -u) &&
636 test "z$commit0" = "z$parent" &&
637 numparent=$(git show --pretty=raw $commit2 |
638 sed -n -e "s/^parent //p" -e "/^author /q" |
639 wc -l) &&
640 test $numparent = 1
643 test_expect_success 'update-index D/F conflict' '
644 mv path0 tmp &&
645 mv path2 path0 &&
646 mv tmp path2 &&
647 git update-index --add --replace path2 path0/file2 &&
648 numpath0=$(git ls-files path0 | wc -l) &&
649 test $numpath0 = 1
652 test_expect_success 'very long name in the index handled sanely' '
654 a=a && # 1
655 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
656 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
657 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
658 a=${a}q &&
660 >path4 &&
661 git update-index --add path4 &&
663 git ls-files -s path4 |
664 sed -e "s/ .*/ /" |
665 tr -d "\012"
666 echo "$a"
667 ) | git update-index --index-info &&
668 len=$(git ls-files "a*" | wc -c) &&
669 test $len = 4098
672 test_done