test-lib*.sh: set GIT_CEILING_DIRECTORIES
[topgit/pro.git] / t / t0001-testlib-basic.sh
blobee468368382198a776dd6e494c65bfdb43cdd552
1 #!/bin/sh
3 # Copyright (C) 2005 Junio C Hamano
4 # Copyright (C) 2016 Kyle J. McKay
5 # All rights reserved
8 test_description='Test the very basics part #1.
10 The rest of the test suite does not check the basic operation of git
11 plumbing commands to work very carefully. Their job is to concentrate
12 on tricky features that caused bugs in the past to detect regression.
14 This test runs very basic features, like registering things in cache,
15 writing tree, etc.
17 Note that this test *deliberately* hard-codes many expected object
18 IDs. When object ID computation changes, like in the previous case of
19 swapping compression and hashing order, the person who is making the
20 modification *should* take notice and update the test vectors here.
23 . ./test-lib.sh
25 ################################################################
26 # git init has been done in an empty repository.
27 # make sure it is empty.
29 test_expect_success '.git/objects should be empty after git init in an empty repo' '
30 find .git/objects -type f -print >should-be-empty &&
31 test_line_count = 0 should-be-empty
34 # also it should have 2 subdirectories; no fan-out anymore, pack, and info.
35 # 3 is counting "objects" itself
36 test_expect_success '.git/objects should have 3 subdirectories' '
37 find .git/objects -type d -print >full-of-directories &&
38 test_line_count = 3 full-of-directories
41 ################################################################
42 # Test harness
43 test_expect_success 'success is reported like this' '
47 _run_sub_test_lib_test_common () {
48 neg="$1" name="$2" descr="$3" # stdin is the body of the test code
49 shift 3
50 mkdir "$name" &&
52 # Pretend we're not running under a test harness, whether we
53 # are or not. The test-lib output depends on the setting of
54 # this variable, so we need a stable setting under which to run
55 # the sub-test.
56 sane_unset HARNESS_ACTIVE &&
57 cd "$name" &&
58 ln -s "$TESTLIB_DIRECTORY/test-lib.sh" . &&
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 # Unset LINENO as it's not universally supported and we do not
69 # want to have to count lines to generate the expected output!
70 unset LINENO
72 . ./test-lib.sh
74 EOF
75 cat >>"$name.sh" &&
76 chmod +x "$name.sh" &&
77 TEST_OUTPUT_DIRECTORY=$(pwd) &&
78 export TEST_OUTPUT_DIRECTORY &&
79 if test -z "$neg"
80 then
81 ./"$name.sh" "$@" >out 2>err
82 else
83 ! ./"$name.sh" "$@" >out 2>err
88 run_sub_test_lib_test () {
89 _run_sub_test_lib_test_common '' "$@"
92 run_sub_test_lib_test_err () {
93 _run_sub_test_lib_test_common '!' "$@"
96 check_sub_test_lib_test () {
97 name="$1" # stdin is the expected output from the test
99 cd "$name" &&
100 ! test -s err &&
101 sed -e 's/^> //' -e 's/Z$//' >expect &&
102 test_cmp expect out
106 check_sub_test_lib_test_err () {
107 name="$1" # stdin is the expected output from the test
108 # expected error output is in descriptior 3
110 cd "$name" &&
111 sed -e 's/^> //' -e 's/Z$//' >expect.out &&
112 test_cmp expect.out out &&
113 sed -e 's/^> //' -e 's/Z$//' <&3 >expect.err &&
114 test_cmp expect.err err
118 test_expect_success 'pretend we have a fully passing test suite' "
119 run_sub_test_lib_test full-pass '3 passing tests' <<-\\EOF &&
120 for i in 1 2 3
122 test_expect_success \"passing test #\$i\" 'true'
123 done
124 test_done
126 check_sub_test_lib_test full-pass <<-\\EOF
127 > ok 1 - passing test #1
128 > ok 2 - passing test #2
129 > ok 3 - passing test #3
130 > # passed all 3 test(s)
131 > 1..3
135 test_expect_success 'pretend we have a partially passing test suite' "
136 test_must_fail run_sub_test_lib_test \
137 partial-pass '2/3 tests passing' <<-\\EOF &&
138 test_expect_success 'passing test #1' 'true'
139 test_expect_success 'failing test #2' 'false'
140 test_expect_success 'passing test #3' 'true'
141 test_done
143 check_sub_test_lib_test partial-pass <<-\\EOF
144 > ok 1 - passing test #1
145 > not ok 2 - failing test #2
146 # failed: partial-pass.sh
148 # false
150 > ok 3 - passing test #3
151 > # failed 1 among 3 test(s)
152 > 1..3
156 test_expect_success 'pretend we have a known breakage' "
157 run_sub_test_lib_test failing-todo 'A failing TODO test' <<-\\EOF &&
158 test_expect_success 'passing test' 'true'
159 test_expect_failure 'pretend we have a known breakage' 'false'
160 test_done
162 check_sub_test_lib_test failing-todo <<-\\EOF
163 > ok 1 - passing test
164 > not ok 2 - pretend we have a known breakage # TODO known breakage
165 > # still have 1 known breakage(s)
166 > # passed all remaining 1 test(s)
167 > 1..2
171 test_expect_success 'pretend we have fixed a known breakage' "
172 run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
173 test_expect_failure 'pretend we have fixed a known breakage' 'true'
174 test_done
176 check_sub_test_lib_test passing-todo <<-\\EOF
177 > ok 1 - pretend we have fixed a known breakage # TODO known breakage vanished
178 > # 1 known breakage(s) vanished; please update test(s)
179 > 1..1
183 test_expect_success 'pretend we have fixed one of two known breakages (run in sub test-lib)' "
184 run_sub_test_lib_test partially-passing-todos \
185 '2 TODO tests, one passing' <<-\\EOF &&
186 test_expect_failure 'pretend we have a known breakage' 'false'
187 test_expect_success 'pretend we have a passing test' 'true'
188 test_expect_failure 'pretend we have fixed another known breakage' 'true'
189 test_done
191 check_sub_test_lib_test partially-passing-todos <<-\\EOF
192 > not ok 1 - pretend we have a known breakage # TODO known breakage
193 > ok 2 - pretend we have a passing test
194 > ok 3 - pretend we have fixed another known breakage # TODO known breakage vanished
195 > # 1 known breakage(s) vanished; please update test(s)
196 > # still have 1 known breakage(s)
197 > # passed all remaining 1 test(s)
198 > 1..3
202 test_expect_success 'pretend we have a pass, fail, and known breakage' "
203 test_must_fail run_sub_test_lib_test \
204 mixed-results1 'mixed results #1' <<-\\EOF &&
205 test_expect_success 'passing test' 'true'
206 test_expect_success 'failing test' 'false'
207 test_expect_failure 'pretend we have a known breakage' 'false'
208 test_done
210 check_sub_test_lib_test mixed-results1 <<-\\EOF
211 > ok 1 - passing test
212 > not ok 2 - failing test
213 > # failed: mixed-results1.sh
215 > # false
217 > not ok 3 - pretend we have a known breakage # TODO known breakage
218 > # still have 1 known breakage(s)
219 > # failed 1 among remaining 2 test(s)
220 > 1..3
224 test_expect_success 'pretend we have a mix of all possible results' "
225 test_must_fail run_sub_test_lib_test \
226 mixed-results2 'mixed results #2' <<-\\EOF &&
227 test_expect_success 'passing test' 'true'
228 test_expect_success 'passing test' 'true'
229 test_expect_success 'passing test' 'true'
230 test_expect_success 'passing test' 'true'
231 test_expect_success 'failing test' 'false'
232 test_expect_success 'failing test' 'false'
233 test_expect_success 'failing test' 'false'
234 test_expect_failure 'pretend we have a known breakage' 'false'
235 test_expect_failure 'pretend we have a known breakage' 'false'
236 test_expect_failure 'pretend we have fixed a known breakage' 'true'
237 test_done
239 check_sub_test_lib_test mixed-results2 <<-\\EOF
240 > ok 1 - passing test
241 > ok 2 - passing test
242 > ok 3 - passing test
243 > ok 4 - passing test
244 > not ok 5 - failing test
245 > # failed: mixed-results2.sh
247 > # false
249 > not ok 6 - failing test
250 > # failed: mixed-results2.sh
252 > # false
254 > not ok 7 - failing test
255 > # failed: mixed-results2.sh
257 > # false
259 > not ok 8 - pretend we have a known breakage # TODO known breakage
260 > not ok 9 - pretend we have a known breakage # TODO known breakage
261 > ok 10 - pretend we have fixed a known breakage # TODO known breakage vanished
262 > # 1 known breakage(s) vanished; please update test(s)
263 > # still have 2 known breakage(s)
264 > # failed 3 among remaining 7 test(s)
265 > 1..10
269 test_expect_success 'test --verbose' '
270 test_must_fail run_sub_test_lib_test \
271 test-verbose "test verbose" --verbose <<-\EOF &&
272 test_expect_success "passing test" true
273 test_expect_success "test with output" "echo foo"
274 test_expect_success "failing test" false
275 test_done
277 mv test-verbose/out test-verbose/out+ &&
278 grep -v "^Initialized empty" test-verbose/out+ >test-verbose/out &&
279 check_sub_test_lib_test test-verbose <<-\EOF
280 > expecting success: true
281 > ok 1 - passing test
283 > expecting success: echo foo
284 > foo
285 > ok 2 - test with output
287 > expecting success: false
288 > not ok 3 - failing test
289 > # failed: test-verbose.sh
291 > # false
294 > # failed 1 among 3 test(s)
295 > 1..3
299 test_expect_success 'test --verbose-only' '
300 test_must_fail run_sub_test_lib_test \
301 test-verbose-only-2 "test verbose-only=2" \
302 --verbose-only=2 <<-\EOF &&
303 test_expect_success "passing test" true
304 test_expect_success "test with output" "echo foo"
305 test_expect_success "failing test" false
306 test_done
308 check_sub_test_lib_test test-verbose-only-2 <<-\EOF
309 > ok 1 - passing test
311 > expecting success: echo foo
312 > foo
313 > ok 2 - test with output
315 > not ok 3 - failing test
316 > # failed: test-verbose-only-2.sh
318 > # false
320 > # failed 1 among 3 test(s)
321 > 1..3
325 test_expect_success 'TESTLIB_SKIP_TESTS' "
327 TESTLIB_SKIP_TESTS='testlib.2' && export TESTLIB_SKIP_TESTS &&
328 run_sub_test_lib_test testlib-skip-tests-basic \
329 'TESTLIB_SKIP_TESTS' <<-\\EOF &&
330 for i in 1 2 3
332 test_expect_success \"passing test #\$i\" 'true'
333 done
334 test_done
336 check_sub_test_lib_test testlib-skip-tests-basic <<-\\EOF
337 > ok 1 - passing test #1
338 > ok 2 # skip passing test #2 (TESTLIB_SKIP_TESTS)
339 > ok 3 - passing test #3
340 > # passed all 3 test(s)
341 > 1..3
346 test_expect_success 'TESTLIB_SKIP_TESTS several tests' "
348 TESTLIB_SKIP_TESTS='testlib.2 testlib.5' && export TESTLIB_SKIP_TESTS &&
349 run_sub_test_lib_test testlib-skip-tests-several \
350 'TESTLIB_SKIP_TESTS several tests' <<-\\EOF &&
351 for i in 1 2 3 4 5 6
353 test_expect_success \"passing test #\$i\" 'true'
354 done
355 test_done
357 check_sub_test_lib_test testlib-skip-tests-several <<-\\EOF
358 > ok 1 - passing test #1
359 > ok 2 # skip passing test #2 (TESTLIB_SKIP_TESTS)
360 > ok 3 - passing test #3
361 > ok 4 - passing test #4
362 > ok 5 # skip passing test #5 (TESTLIB_SKIP_TESTS)
363 > ok 6 - passing test #6
364 > # passed all 6 test(s)
365 > 1..6
370 test_expect_success 'TESTLIB_SKIP_TESTS sh pattern' "
372 TESTLIB_SKIP_TESTS='testlib.[2-5]' && export TESTLIB_SKIP_TESTS &&
373 run_sub_test_lib_test testlib-skip-tests-sh-pattern \
374 'TESTLIB_SKIP_TESTS sh pattern' <<-\\EOF &&
375 for i in 1 2 3 4 5 6
377 test_expect_success \"passing test #\$i\" 'true'
378 done
379 test_done
381 check_sub_test_lib_test testlib-skip-tests-sh-pattern <<-\\EOF
382 > ok 1 - passing test #1
383 > ok 2 # skip passing test #2 (TESTLIB_SKIP_TESTS)
384 > ok 3 # skip passing test #3 (TESTLIB_SKIP_TESTS)
385 > ok 4 # skip passing test #4 (TESTLIB_SKIP_TESTS)
386 > ok 5 # skip passing test #5 (TESTLIB_SKIP_TESTS)
387 > ok 6 - passing test #6
388 > # passed all 6 test(s)
389 > 1..6
394 test_expect_success '--run basic' "
395 run_sub_test_lib_test run-basic \
396 '--run basic' --run='1 3 5' <<-\\EOF &&
397 for i in 1 2 3 4 5 6
399 test_expect_success \"passing test #\$i\" 'true'
400 done
401 test_done
403 check_sub_test_lib_test run-basic <<-\\EOF
404 > ok 1 - passing test #1
405 > ok 2 # skip passing test #2 (--run)
406 > ok 3 - passing test #3
407 > ok 4 # skip passing test #4 (--run)
408 > ok 5 - passing test #5
409 > ok 6 # skip passing test #6 (--run)
410 > # passed all 6 test(s)
411 > 1..6
415 test_expect_success '--run with a range' "
416 run_sub_test_lib_test run-range \
417 '--run with a range' --run='1-3' <<-\\EOF &&
418 for i in 1 2 3 4 5 6
420 test_expect_success \"passing test #\$i\" 'true'
421 done
422 test_done
424 check_sub_test_lib_test run-range <<-\\EOF
425 > ok 1 - passing test #1
426 > ok 2 - passing test #2
427 > ok 3 - passing test #3
428 > ok 4 # skip passing test #4 (--run)
429 > ok 5 # skip passing test #5 (--run)
430 > ok 6 # skip passing test #6 (--run)
431 > # passed all 6 test(s)
432 > 1..6
436 test_expect_success '--run with two ranges' "
437 run_sub_test_lib_test run-two-ranges \
438 '--run with two ranges' --run='1-2 5-6' <<-\\EOF &&
439 for i in 1 2 3 4 5 6
441 test_expect_success \"passing test #\$i\" 'true'
442 done
443 test_done
445 check_sub_test_lib_test run-two-ranges <<-\\EOF
446 > ok 1 - passing test #1
447 > ok 2 - passing test #2
448 > ok 3 # skip passing test #3 (--run)
449 > ok 4 # skip passing test #4 (--run)
450 > ok 5 - passing test #5
451 > ok 6 - passing test #6
452 > # passed all 6 test(s)
453 > 1..6
457 test_expect_success '--run with a left open range' "
458 run_sub_test_lib_test run-left-open-range \
459 '--run with a left open range' --run='-3' <<-\\EOF &&
460 for i in 1 2 3 4 5 6
462 test_expect_success \"passing test #\$i\" 'true'
463 done
464 test_done
466 check_sub_test_lib_test run-left-open-range <<-\\EOF
467 > ok 1 - passing test #1
468 > ok 2 - passing test #2
469 > ok 3 - passing test #3
470 > ok 4 # skip passing test #4 (--run)
471 > ok 5 # skip passing test #5 (--run)
472 > ok 6 # skip passing test #6 (--run)
473 > # passed all 6 test(s)
474 > 1..6
478 test_expect_success '--run with a right open range' "
479 run_sub_test_lib_test run-right-open-range \
480 '--run with a right open range' --run='4-' <<-\\EOF &&
481 for i in 1 2 3 4 5 6
483 test_expect_success \"passing test #\$i\" 'true'
484 done
485 test_done
487 check_sub_test_lib_test run-right-open-range <<-\\EOF
488 > ok 1 # skip passing test #1 (--run)
489 > ok 2 # skip passing test #2 (--run)
490 > ok 3 # skip passing test #3 (--run)
491 > ok 4 - passing test #4
492 > ok 5 - passing test #5
493 > ok 6 - passing test #6
494 > # passed all 6 test(s)
495 > 1..6
499 test_expect_success '--run with basic negation' "
500 run_sub_test_lib_test run-basic-neg \
501 '--run with basic negation' --run='"'!3'"' <<-\\EOF &&
502 for i in 1 2 3 4 5 6
504 test_expect_success \"passing test #\$i\" 'true'
505 done
506 test_done
508 check_sub_test_lib_test run-basic-neg <<-\\EOF
509 > ok 1 - passing test #1
510 > ok 2 - passing test #2
511 > ok 3 # skip passing test #3 (--run)
512 > ok 4 - passing test #4
513 > ok 5 - passing test #5
514 > ok 6 - passing test #6
515 > # passed all 6 test(s)
516 > 1..6
520 test_expect_success '--run with two negations' "
521 run_sub_test_lib_test run-two-neg \
522 '--run with two negations' --run='"'!3 !6'"' <<-\\EOF &&
523 for i in 1 2 3 4 5 6
525 test_expect_success \"passing test #\$i\" 'true'
526 done
527 test_done
529 check_sub_test_lib_test run-two-neg <<-\\EOF
530 > ok 1 - passing test #1
531 > ok 2 - passing test #2
532 > ok 3 # skip passing test #3 (--run)
533 > ok 4 - passing test #4
534 > ok 5 - passing test #5
535 > ok 6 # skip passing test #6 (--run)
536 > # passed all 6 test(s)
537 > 1..6
541 test_expect_success '--run a range and negation' "
542 run_sub_test_lib_test run-range-and-neg \
543 '--run a range and negation' --run='"'-4 !2'"' <<-\\EOF &&
544 for i in 1 2 3 4 5 6
546 test_expect_success \"passing test #\$i\" 'true'
547 done
548 test_done
550 check_sub_test_lib_test run-range-and-neg <<-\\EOF
551 > ok 1 - passing test #1
552 > ok 2 # skip passing test #2 (--run)
553 > ok 3 - passing test #3
554 > ok 4 - passing test #4
555 > ok 5 # skip passing test #5 (--run)
556 > ok 6 # skip passing test #6 (--run)
557 > # passed all 6 test(s)
558 > 1..6
562 test_expect_success '--run range negation' "
563 run_sub_test_lib_test run-range-neg \
564 '--run range negation' --run='"'!1-3'"' <<-\\EOF &&
565 for i in 1 2 3 4 5 6
567 test_expect_success \"passing test #\$i\" 'true'
568 done
569 test_done
571 check_sub_test_lib_test run-range-neg <<-\\EOF
572 > ok 1 # skip passing test #1 (--run)
573 > ok 2 # skip passing test #2 (--run)
574 > ok 3 # skip passing test #3 (--run)
575 > ok 4 - passing test #4
576 > ok 5 - passing test #5
577 > ok 6 - passing test #6
578 > # passed all 6 test(s)
579 > 1..6
583 test_expect_success '--run include, exclude and include' "
584 run_sub_test_lib_test run-inc-neg-inc \
585 '--run include, exclude and include' \
586 --run='"'1-5 !1-3 2'"' <<-\\EOF &&
587 for i in 1 2 3 4 5 6
589 test_expect_success \"passing test #\$i\" 'true'
590 done
591 test_done
593 check_sub_test_lib_test run-inc-neg-inc <<-\\EOF
594 > ok 1 # skip passing test #1 (--run)
595 > ok 2 - passing test #2
596 > ok 3 # skip passing test #3 (--run)
597 > ok 4 - passing test #4
598 > ok 5 - passing test #5
599 > ok 6 # skip passing test #6 (--run)
600 > # passed all 6 test(s)
601 > 1..6
605 test_expect_success '--run include, exclude and include, comma separated' "
606 run_sub_test_lib_test run-inc-neg-inc-comma \
607 '--run include, exclude and include, comma separated' \
608 --run=1-5,\!1-3,2 <<-\\EOF &&
609 for i in 1 2 3 4 5 6
611 test_expect_success \"passing test #\$i\" 'true'
612 done
613 test_done
615 check_sub_test_lib_test run-inc-neg-inc-comma <<-\\EOF
616 > ok 1 # skip passing test #1 (--run)
617 > ok 2 - passing test #2
618 > ok 3 # skip passing test #3 (--run)
619 > ok 4 - passing test #4
620 > ok 5 - passing test #5
621 > ok 6 # skip passing test #6 (--run)
622 > # passed all 6 test(s)
623 > 1..6
627 test_expect_success '--run exclude and include' "
628 run_sub_test_lib_test run-neg-inc \
629 '--run exclude and include' \
630 --run='"'!3- 5'"' <<-\\EOF &&
631 for i in 1 2 3 4 5 6
633 test_expect_success \"passing test #\$i\" 'true'
634 done
635 test_done
637 check_sub_test_lib_test run-neg-inc <<-\\EOF
638 > ok 1 - passing test #1
639 > ok 2 - passing test #2
640 > ok 3 # skip passing test #3 (--run)
641 > ok 4 # skip passing test #4 (--run)
642 > ok 5 - passing test #5
643 > ok 6 # skip passing test #6 (--run)
644 > # passed all 6 test(s)
645 > 1..6
649 test_expect_success '--run empty selectors' "
650 run_sub_test_lib_test run-empty-sel \
651 '--run empty selectors' \
652 --run='1,,3,,,5' <<-\\EOF &&
653 for i in 1 2 3 4 5 6
655 test_expect_success \"passing test #\$i\" 'true'
656 done
657 test_done
659 check_sub_test_lib_test run-empty-sel <<-\\EOF
660 > ok 1 - passing test #1
661 > ok 2 # skip passing test #2 (--run)
662 > ok 3 - passing test #3
663 > ok 4 # skip passing test #4 (--run)
664 > ok 5 - passing test #5
665 > ok 6 # skip passing test #6 (--run)
666 > # passed all 6 test(s)
667 > 1..6
671 test_expect_success '--run invalid range start' "
672 run_sub_test_lib_test_err run-inv-range-start \
673 '--run invalid range start' \
674 --run='a-5' <<-\\EOF &&
675 test_expect_success \"passing test #1\" 'true'
676 test_done
678 check_sub_test_lib_test_err run-inv-range-start \
679 <<-\\EOF_OUT 3<<-\\EOF_ERR
680 > FATAL: Unexpected exit with code 1
681 EOF_OUT
682 > error: --run: invalid non-numeric in range start: 'a-5'
683 EOF_ERR
686 test_expect_success '--run invalid range end' "
687 run_sub_test_lib_test_err run-inv-range-end \
688 '--run invalid range end' \
689 --run='1-z' <<-\\EOF &&
690 test_expect_success \"passing test #1\" 'true'
691 test_done
693 check_sub_test_lib_test_err run-inv-range-end \
694 <<-\\EOF_OUT 3<<-\\EOF_ERR
695 > FATAL: Unexpected exit with code 1
696 EOF_OUT
697 > error: --run: invalid non-numeric in range end: '1-z'
698 EOF_ERR
701 test_expect_success '--run invalid selector' "
702 run_sub_test_lib_test_err run-inv-selector \
703 '--run invalid selector' \
704 --run='1?' <<-\\EOF &&
705 test_expect_success \"passing test #1\" 'true'
706 test_done
708 check_sub_test_lib_test_err run-inv-selector \
709 <<-\\EOF_OUT 3<<-\\EOF_ERR
710 > FATAL: Unexpected exit with code 1
711 EOF_OUT
712 > error: --run: invalid non-numeric in test selector: '1?'
713 EOF_ERR
717 test_set_prereq HAVEIT
718 haveit=no
719 echo "$haveit" >haveit
720 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
721 test_have_prereq HAVEIT &&
722 haveit=yes &&
723 echo "$haveit" >haveit
725 donthaveit=yes
726 echo "$donthaveit" >donthaveit
727 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
728 donthaveit=no &&
729 echo "$donthaveit" >donthaveit
731 haveitf="$(cat haveit)"
732 donthaveitf="$(cat donthaveit)"
733 if test $haveit$donthaveit != noyes || test $haveitf$donthaveitf != yesyes
734 then
735 say "bug in test framework: prerequisite tags do not work reliably"
736 exit 1
739 # Stop with the fussing with files
740 TESTLIB_TEST_NO_SUBSHELL=1
742 test_set_prereq HAVETHIS
743 haveit=no
744 test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
745 test_have_prereq HAVEIT &&
746 test_have_prereq HAVETHIS &&
747 haveit=yes
749 donthaveit=yes
750 test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
751 donthaveit=no
753 donthaveiteither=yes
754 test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
755 donthaveiteither=no
757 if test $haveit$donthaveit$donthaveiteither != yesyesyes
758 then
759 say "bug in test framework: multiple prerequisite tags do not work reliably"
760 exit 1
763 test_lazy_prereq LAZY_TRUE true
764 havetrue=no
765 test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
766 havetrue=yes
768 donthavetrue=yes
769 test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
770 donthavetrue=no
773 if test "$havetrue$donthavetrue" != yesyes
774 then
775 say 'bug in test framework: lazy prerequisites do not work'
776 exit 1
779 test_lazy_prereq LAZY_FALSE false
780 nothavefalse=no
781 test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
782 nothavefalse=yes
784 havefalse=yes
785 test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
786 havefalse=no
789 if test "$nothavefalse$havefalse" != yesyes
790 then
791 say 'bug in test framework: negative lazy prerequisites do not work'
792 exit 1
795 clean=no
796 test_expect_success 'tests clean up after themselves' '
797 test_when_finished clean=yes
800 if test $clean != yes
801 then
802 say "bug in test framework: basic cleanup command does not work reliably"
803 exit 1
806 # Done with the no subshell tests
807 unset TESTLIB_TEST_NO_SUBSHELL
809 test_expect_success 'tests clean up even on failures' "
810 test_must_fail run_sub_test_lib_test \
811 failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
812 TESTLIB_TEST_NO_SUBSHELL=1
813 test_expect_success 'tests clean up even after a failure' '
814 touch clean-after-failure &&
815 test_when_finished rm clean-after-failure &&
816 (exit 1)
818 test_expect_success 'failure to clean up causes the test to fail' '
819 test_when_finished \"(exit 2)\"
821 test_done
823 check_sub_test_lib_test failing-cleanup <<-\\EOF
824 > not ok 1 - tests clean up even after a failure
825 > # failed: failing-cleanup.sh
827 > # touch clean-after-failure &&
828 > # test_when_finished rm clean-after-failure &&
829 > # (exit 1)
831 > not ok 2 - failure to clean up causes the test to fail
832 > # failed: failing-cleanup.sh
834 > # test_when_finished \"(exit 2)\"
836 > # failed 2 among 2 test(s)
837 > 1..2
841 ################################################################
842 # Basics of the basics
844 # updating a new file without --add should fail.
845 test_expect_success 'git update-index without --add should fail adding' '
846 test_must_fail git update-index should-be-empty
849 # and with --add it should succeed, even if it is empty (it used to fail).
850 test_expect_success 'git update-index with --add should succeed' '
851 git update-index --add should-be-empty
854 TESTLIB_TEST_NO_SUBSHELL=1
855 test_expect_success 'writing tree out with git write-tree' '
856 tree=$(git write-tree)
858 unset TESTLIB_TEST_NO_SUBSHELL
860 # we know the shape and contents of the tree and know the object ID for it.
861 test_expect_success 'validate object ID of a known tree' '
862 test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
865 # Removing paths.
866 test_expect_success 'git update-index without --remove should fail removing' '
867 rm -f should-be-empty full-of-directories &&
868 test_must_fail git update-index should-be-empty
871 test_expect_success 'git update-index with --remove should be able to remove' '
872 git update-index --remove should-be-empty
875 # Empty tree can be written with recent write-tree.
876 TESTLIB_TEST_NO_SUBSHELL=1
877 test_tolerate_failure 'git write-tree should be able to write an empty tree' '
878 tree=$(git write-tree)
880 unset TESTLIB_TEST_NO_SUBSHELL
882 if test -n "$tree"
883 then
884 test_result='test_expect_success'
885 else
886 test_result='test_tolerate_failure'
888 $test_result 'validate object ID of a known tree' '
889 test "$tree" = $EMPTY_TREE
892 # Various types of objects
894 test_expect_success 'adding various types of objects with git update-index --add' '
895 mkdir path2 path3 path3/subp3 &&
896 paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
898 for p in $paths
900 echo "hello $p" >$p || exit 1
901 test_ln_s_add "hello $p" ${p}sym || exit 1
902 done
903 ) &&
904 find path* ! -type d -print | xargs git update-index --add
907 # Show them and see that matches what we expect.
908 test_expect_success 'showing stage with git ls-files --stage' '
909 git ls-files --stage >current
912 test_expect_success 'validate git ls-files output for a known tree' '
913 cat >expected <<-\EOF &&
914 100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
915 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
916 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
917 120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
918 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
919 120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
920 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
921 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
923 test_cmp expected current
926 TESTLIB_TEST_NO_SUBSHELL=1
927 test_expect_success 'writing tree out with git write-tree' '
928 tree=$(git write-tree)
930 unset TESTLIB_TEST_NO_SUBSHELL
932 test_expect_success 'validate object ID for a known tree' '
933 test "$tree" = 087704a96baf1c2d1c869a8b084481e121c88b5b
936 test_expect_success 'showing tree with git ls-tree' '
937 git ls-tree $tree >current
940 test_expect_success 'git ls-tree output for a known tree' '
941 cat >expected <<-\EOF &&
942 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
943 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
944 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
945 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
947 test_cmp expected current
950 # This changed in ls-tree pathspec change -- recursive does
951 # not show tree nodes anymore.
952 test_expect_success 'showing tree with git ls-tree -r' '
953 git ls-tree -r $tree >current
956 test_expect_success 'git ls-tree -r output for a known tree' '
957 cat >expected <<-\EOF &&
958 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
959 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
960 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
961 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
962 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
963 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
964 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
965 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
967 test_cmp expected current
970 # But with -r -t we can have both.
971 test_expect_success 'showing tree with git ls-tree -r -t' '
972 git ls-tree -r -t $tree >current
975 test_expect_success 'git ls-tree -r output for a known tree' '
976 cat >expected <<-\EOF &&
977 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
978 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
979 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
980 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
981 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
982 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
983 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
984 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
985 040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
986 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
987 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
989 test_cmp expected current
992 TESTLIB_TEST_NO_SUBSHELL=1
993 test_expect_success 'writing partial tree out with git write-tree --prefix' '
994 ptree=$(git write-tree --prefix=path3)
996 unset TESTLIB_TEST_NO_SUBSHELL
998 test_expect_success 'validate object ID for a known tree' '
999 test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3
1002 TESTLIB_TEST_NO_SUBSHELL=1
1003 test_expect_success 'writing partial tree out with git write-tree --prefix' '
1004 ptree=$(git write-tree --prefix=path3/subp3)
1006 unset TESTLIB_TEST_NO_SUBSHELL
1008 test_expect_success 'validate object ID for a known tree' '
1009 test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2
1012 test_expect_success 'put invalid objects into the index' '
1013 rm -f .git/index &&
1014 cat >badobjects <<-\EOF &&
1015 100644 blob 1000000000000000000000000000000000000000 dir/file1
1016 100644 blob 2000000000000000000000000000000000000000 dir/file2
1017 100644 blob 3000000000000000000000000000000000000000 dir/file3
1018 100644 blob 4000000000000000000000000000000000000000 dir/file4
1019 100644 blob 5000000000000000000000000000000000000000 dir/file5
1021 git update-index --index-info <badobjects
1024 test_expect_success 'writing this tree without --missing-ok' '
1025 test_must_fail git write-tree
1028 test_expect_success 'writing this tree with --missing-ok' '
1029 git write-tree --missing-ok
1033 ################################################################
1034 test_expect_success 'git read-tree followed by write-tree should be idempotent' '
1035 rm -f .git/index &&
1036 git read-tree $tree &&
1037 test -f .git/index &&
1038 newtree=$(git write-tree) &&
1039 test "$newtree" = "$tree"
1042 test_expect_success 'validate git diff-files output for a know cache/work tree state' '
1043 cat >expected <<\EOF &&
1044 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
1045 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
1046 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
1047 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
1048 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
1049 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
1050 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
1051 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
1053 git diff-files >current &&
1054 test_cmp current expected
1057 test_expect_success 'git update-index --refresh should succeed' '
1058 git update-index --refresh
1061 test_expect_success 'no diff after checkout and git update-index --refresh' '
1062 git diff-files >current &&
1063 cmp -s current /dev/null
1066 ################################################################
1067 P=087704a96baf1c2d1c869a8b084481e121c88b5b
1069 TESTLIB_TEST_NO_SUBSHELL=1
1070 test_expect_success 'git commit-tree records the correct tree in a commit' '
1071 commit0=$(echo NO | git commit-tree $P) &&
1072 tree=$(git show --pretty=raw $commit0 |
1073 sed -n -e "s/^tree //p" -e "/^author /q") &&
1074 test "z$tree" = "z$P"
1076 unset TESTLIB_TEST_NO_SUBSHELL
1078 test_expect_success 'git commit-tree records the correct parent in a commit' '
1079 commit1=$(echo NO | git commit-tree $P -p $commit0) &&
1080 parent=$(git show --pretty=raw $commit1 |
1081 sed -n -e "s/^parent //p" -e "/^author /q") &&
1082 test "z$commit0" = "z$parent"
1085 test_expect_success 'git commit-tree omits duplicated parent in a commit' '
1086 commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
1087 parent=$(git show --pretty=raw $commit2 |
1088 sed -n -e "s/^parent //p" -e "/^author /q" |
1089 sort -u) &&
1090 test "z$commit0" = "z$parent" &&
1091 numparent=$(git show --pretty=raw $commit2 |
1092 sed -n -e "s/^parent //p" -e "/^author /q" |
1093 wc -l) &&
1094 test $numparent = 1
1097 test_expect_success 'update-index D/F conflict' '
1098 mv path0 tmp &&
1099 mv path2 path0 &&
1100 mv tmp path2 &&
1101 git update-index --add --replace path2 path0/file2 &&
1102 numpath0=$(git ls-files path0 | wc -l) &&
1103 test $numpath0 = 1
1106 test_expect_success 'very long name in the index handled sanely' '
1108 a=a && # 1
1109 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
1110 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
1111 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
1112 a=${a}q &&
1114 >path4 &&
1115 git update-index --add path4 &&
1117 git ls-files -s path4 |
1118 sed -e "s/ .*/ /" |
1119 tr -d "\012"
1120 echo "$a"
1121 ) | git update-index --index-info &&
1122 len=$(git ls-files "a*" | wc -c) &&
1123 test $len = 4098
1126 test_done