t0000: simplify HARNESS_ACTIVE hack
[alt-git.git] / t / t0000-basic.sh
blobe6c5b6383914c5010c36c3dc058e9216925777b1
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 not running under a test harness, whether we
54 # are or not. The test-lib output depends on the setting of
55 # this variable, so we need a stable setting under which to run
56 # the sub-test.
57 sane_unset 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
238 > ok 1 - passing test
240 > expecting success: echo foo
241 > foo
242 > ok 2 - test with output
244 > 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
267 > ok 2 - test with output
269 > not ok 3 - failing test
270 > # false
271 > # failed 1 among 3 test(s)
272 > 1..3
276 test_set_prereq HAVEIT
277 haveit=no
278 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
279 test_have_prereq HAVEIT &&
280 haveit=yes
282 donthaveit=yes
283 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
284 donthaveit=no
286 if test $haveit$donthaveit != yesyes
287 then
288 say "bug in test framework: prerequisite tags do not work reliably"
289 exit 1
292 test_set_prereq HAVETHIS
293 haveit=no
294 test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
295 test_have_prereq HAVEIT &&
296 test_have_prereq HAVETHIS &&
297 haveit=yes
299 donthaveit=yes
300 test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
301 donthaveit=no
303 donthaveiteither=yes
304 test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
305 donthaveiteither=no
307 if test $haveit$donthaveit$donthaveiteither != yesyesyes
308 then
309 say "bug in test framework: multiple prerequisite tags do not work reliably"
310 exit 1
313 test_lazy_prereq LAZY_TRUE true
314 havetrue=no
315 test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
316 havetrue=yes
318 donthavetrue=yes
319 test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
320 donthavetrue=no
323 if test "$havetrue$donthavetrue" != yesyes
324 then
325 say 'bug in test framework: lazy prerequisites do not work'
326 exit 1
329 test_lazy_prereq LAZY_FALSE false
330 nothavefalse=no
331 test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
332 nothavefalse=yes
334 havefalse=yes
335 test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
336 havefalse=no
339 if test "$nothavefalse$havefalse" != yesyes
340 then
341 say 'bug in test framework: negative lazy prerequisites do not work'
342 exit 1
345 clean=no
346 test_expect_success 'tests clean up after themselves' '
347 test_when_finished clean=yes
350 if test $clean != yes
351 then
352 say "bug in test framework: basic cleanup command does not work reliably"
353 exit 1
356 test_expect_success 'tests clean up even on failures' "
357 test_must_fail run_sub_test_lib_test \
358 failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
359 test_expect_success 'tests clean up even after a failure' '
360 touch clean-after-failure &&
361 test_when_finished rm clean-after-failure &&
362 (exit 1)
364 test_expect_success 'failure to clean up causes the test to fail' '
365 test_when_finished \"(exit 2)\"
367 test_done
369 check_sub_test_lib_test failing-cleanup <<-\\EOF
370 > not ok 1 - tests clean up even after a failure
371 > # Z
372 > # touch clean-after-failure &&
373 > # test_when_finished rm clean-after-failure &&
374 > # (exit 1)
375 > # Z
376 > not ok 2 - failure to clean up causes the test to fail
377 > # Z
378 > # test_when_finished \"(exit 2)\"
379 > # Z
380 > # failed 2 among 2 test(s)
381 > 1..2
385 ################################################################
386 # Basics of the basics
388 # updating a new file without --add should fail.
389 test_expect_success 'git update-index without --add should fail adding' '
390 test_must_fail git update-index should-be-empty
393 # and with --add it should succeed, even if it is empty (it used to fail).
394 test_expect_success 'git update-index with --add should succeed' '
395 git update-index --add should-be-empty
398 test_expect_success 'writing tree out with git write-tree' '
399 tree=$(git write-tree)
402 # we know the shape and contents of the tree and know the object ID for it.
403 test_expect_success 'validate object ID of a known tree' '
404 test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
407 # Removing paths.
408 test_expect_success 'git update-index without --remove should fail removing' '
409 rm -f should-be-empty full-of-directories &&
410 test_must_fail git update-index should-be-empty
413 test_expect_success 'git update-index with --remove should be able to remove' '
414 git update-index --remove should-be-empty
417 # Empty tree can be written with recent write-tree.
418 test_expect_success 'git write-tree should be able to write an empty tree' '
419 tree=$(git write-tree)
422 test_expect_success 'validate object ID of a known tree' '
423 test "$tree" = 4b825dc642cb6eb9a060e54bf8d69288fbee4904
426 # Various types of objects
428 test_expect_success 'adding various types of objects with git update-index --add' '
429 mkdir path2 path3 path3/subp3 &&
430 paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
432 for p in $paths
434 echo "hello $p" >$p || exit 1
435 test_ln_s_add "hello $p" ${p}sym || exit 1
436 done
437 ) &&
438 find path* ! -type d -print | xargs git update-index --add
441 # Show them and see that matches what we expect.
442 test_expect_success 'showing stage with git ls-files --stage' '
443 git ls-files --stage >current
446 test_expect_success 'validate git ls-files output for a known tree' '
447 cat >expected <<-\EOF &&
448 100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
449 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
450 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
451 120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
452 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
453 120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
454 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
455 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
457 test_cmp expected current
460 test_expect_success 'writing tree out with git write-tree' '
461 tree=$(git write-tree)
464 test_expect_success 'validate object ID for a known tree' '
465 test "$tree" = 087704a96baf1c2d1c869a8b084481e121c88b5b
468 test_expect_success 'showing tree with git ls-tree' '
469 git ls-tree $tree >current
472 test_expect_success 'git ls-tree output for a known tree' '
473 cat >expected <<-\EOF &&
474 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
475 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
476 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
477 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
479 test_cmp expected current
482 # This changed in ls-tree pathspec change -- recursive does
483 # not show tree nodes anymore.
484 test_expect_success 'showing tree with git ls-tree -r' '
485 git ls-tree -r $tree >current
488 test_expect_success 'git ls-tree -r output for a known tree' '
489 cat >expected <<-\EOF &&
490 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
491 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
492 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
493 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
494 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
495 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
496 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
497 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
499 test_cmp expected current
502 # But with -r -t we can have both.
503 test_expect_success 'showing tree with git ls-tree -r -t' '
504 git ls-tree -r -t $tree >current
507 test_expect_success 'git ls-tree -r output for a known tree' '
508 cat >expected <<-\EOF &&
509 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
510 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
511 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
512 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
513 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
514 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
515 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
516 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
517 040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
518 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
519 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
521 test_cmp expected current
524 test_expect_success 'writing partial tree out with git write-tree --prefix' '
525 ptree=$(git write-tree --prefix=path3)
528 test_expect_success 'validate object ID for a known tree' '
529 test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3
532 test_expect_success 'writing partial tree out with git write-tree --prefix' '
533 ptree=$(git write-tree --prefix=path3/subp3)
536 test_expect_success 'validate object ID for a known tree' '
537 test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2
540 test_expect_success 'put invalid objects into the index' '
541 rm -f .git/index &&
542 cat >badobjects <<-\EOF &&
543 100644 blob 1000000000000000000000000000000000000000 dir/file1
544 100644 blob 2000000000000000000000000000000000000000 dir/file2
545 100644 blob 3000000000000000000000000000000000000000 dir/file3
546 100644 blob 4000000000000000000000000000000000000000 dir/file4
547 100644 blob 5000000000000000000000000000000000000000 dir/file5
549 git update-index --index-info <badobjects
552 test_expect_success 'writing this tree without --missing-ok' '
553 test_must_fail git write-tree
556 test_expect_success 'writing this tree with --missing-ok' '
557 git write-tree --missing-ok
561 ################################################################
562 test_expect_success 'git read-tree followed by write-tree should be idempotent' '
563 rm -f .git/index
564 git read-tree $tree &&
565 test -f .git/index &&
566 newtree=$(git write-tree) &&
567 test "$newtree" = "$tree"
570 test_expect_success 'validate git diff-files output for a know cache/work tree state' '
571 cat >expected <<\EOF &&
572 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
573 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
574 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
575 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
576 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
577 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
578 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
579 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
581 git diff-files >current &&
582 test_cmp current expected
585 test_expect_success 'git update-index --refresh should succeed' '
586 git update-index --refresh
589 test_expect_success 'no diff after checkout and git update-index --refresh' '
590 git diff-files >current &&
591 cmp -s current /dev/null
594 ################################################################
595 P=087704a96baf1c2d1c869a8b084481e121c88b5b
597 test_expect_success 'git commit-tree records the correct tree in a commit' '
598 commit0=$(echo NO | git commit-tree $P) &&
599 tree=$(git show --pretty=raw $commit0 |
600 sed -n -e "s/^tree //p" -e "/^author /q") &&
601 test "z$tree" = "z$P"
604 test_expect_success 'git commit-tree records the correct parent in a commit' '
605 commit1=$(echo NO | git commit-tree $P -p $commit0) &&
606 parent=$(git show --pretty=raw $commit1 |
607 sed -n -e "s/^parent //p" -e "/^author /q") &&
608 test "z$commit0" = "z$parent"
611 test_expect_success 'git commit-tree omits duplicated parent in a commit' '
612 commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
613 parent=$(git show --pretty=raw $commit2 |
614 sed -n -e "s/^parent //p" -e "/^author /q" |
615 sort -u) &&
616 test "z$commit0" = "z$parent" &&
617 numparent=$(git show --pretty=raw $commit2 |
618 sed -n -e "s/^parent //p" -e "/^author /q" |
619 wc -l) &&
620 test $numparent = 1
623 test_expect_success 'update-index D/F conflict' '
624 mv path0 tmp &&
625 mv path2 path0 &&
626 mv tmp path2 &&
627 git update-index --add --replace path2 path0/file2 &&
628 numpath0=$(git ls-files path0 | wc -l) &&
629 test $numpath0 = 1
632 test_expect_success 'very long name in the index handled sanely' '
634 a=a && # 1
635 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
636 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
637 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
638 a=${a}q &&
640 >path4 &&
641 git update-index --add path4 &&
643 git ls-files -s path4 |
644 sed -e "s/ .*/ /" |
645 tr -d "\012"
646 echo "$a"
647 ) | git update-index --index-info &&
648 len=$(git ls-files "a*" | wc -c) &&
649 test $len = 4098
652 test_done