testlib: pimp out test_plan
[topgit/pro.git] / t / t0002-testlib-basic.sh
blobe324791166c586c41c8cc7625b79f6f516306b87
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 test_plan \?
27 ################################################################
28 # git init has been done in an empty repository.
29 # make sure it is empty.
31 test_expect_success '.git/objects should be empty after git init in an empty repo' '
32 find .git/objects -type f -print >should-be-empty &&
33 test_line_count = 0 should-be-empty
36 # also it should have 2 subdirectories; no fan-out anymore, pack, and info.
37 # 3 is counting "objects" itself
38 test_expect_success '.git/objects should have 3 subdirectories' '
39 find .git/objects -type d -print >full-of-directories &&
40 test_line_count = 3 full-of-directories
43 ################################################################
44 # Test harness
45 test_expect_success 'success is reported like this' '
49 _run_sub_test_lib_test_common () {
50 neg="$1" name="$2" descr="$3" # stdin is the body of the test code
51 shift 3
52 mkdir "$name" &&
54 # Pretend we're not running under a test harness, whether we
55 # are or not. The test-lib output depends on the setting of
56 # this variable, so we need a stable setting under which to run
57 # the sub-test.
58 sane_unset HARNESS_ACTIVE &&
59 cd "$name" &&
60 ln -s "$TESTLIB_DIRECTORY/test-lib.sh" . &&
61 cat >"$name.sh" <<-EOF &&
62 #!$SHELL_PATH
64 test_description='$descr (run in sub test-lib)
66 This is run in a sub test-lib so that we do not get incorrect
67 passing metrics
70 . ./test-lib.sh
72 # Unset LINENO as it's not universally supported and we do not
73 # want to have to count lines to generate the expected output!
74 # Attempting this:
75 # unset LINENO || :
76 # may cause some broken sh implementations to abruptly terminate!
77 # Instead just unalias all to avoid picking up the line numbers
78 # after making sure unalias itself is not a function and
79 # dealing with broken zsh that's missing a proper "unalias -a".
80 { "unset" -f unalias; } >/dev/null 2>&1 || :
81 { "unalias" -a || unalias -m "*"; } >/dev/null 2>&1 || :
83 EOF
84 cat >>"$name.sh" &&
85 chmod +x "$name.sh" &&
86 TEST_OUTPUT_DIRECTORY=$(pwd) &&
87 export TEST_OUTPUT_DIRECTORY &&
88 if test -z "$neg"
89 then
90 ./"$name.sh" "$@" >out 2>err
91 else
92 ! ./"$name.sh" "$@" >out 2>err
97 run_sub_test_lib_test () {
98 _run_sub_test_lib_test_common '' "$@"
101 run_sub_test_lib_test_err () {
102 _run_sub_test_lib_test_common '!' "$@"
105 check_sub_test_lib_test () {
106 name="$1" # stdin is the expected output from the test
108 cd "$name" &&
109 ! test -s err &&
110 sed -e 's/^> //' -e 's/Z$//' >expect &&
111 test_cmp expect out
115 check_sub_test_lib_test_err () {
116 name="$1" # stdin is the expected output from the test
117 # expected error output is in descriptior 3
119 cd "$name" &&
120 sed -e 's/^> //' -e 's/Z$//' >expect.out &&
121 test_cmp expect.out out &&
122 sed -e 's/^> //' -e 's/Z$//' <&3 >expect.err &&
123 test_cmp expect.err err
127 test_expect_success 'pretend we have a fully passing test suite' "
128 run_sub_test_lib_test full-pass '3 passing tests' <<-\\EOF &&
129 test_plan 3
130 for i in 1 2 3
132 test_expect_success \"passing test #\$i\" 'true'
133 done
134 test_done
136 check_sub_test_lib_test full-pass <<-\\EOF
137 > 1..3
138 > ok 1 - passing test #1
139 > ok 2 - passing test #2
140 > ok 3 - passing test #3
141 > # full passed all 3 test(s)
145 test_expect_success 'pretend we have a partially passing test suite' "
146 test_must_fail run_sub_test_lib_test \
147 partial-pass '2/3 tests passing' <<-\\EOF &&
148 test_plan 3
149 test_expect_success 'passing test #1' 'true'
150 test_expect_success 'failing test #2' 'false'
151 test_expect_success 'passing test #3' 'true'
152 test_done
154 check_sub_test_lib_test partial-pass <<-\\EOF
155 > 1..3
156 > ok 1 - passing test #1
157 > not ok 2 - failing test #2
158 # failed: partial-pass.sh
160 # false
162 > ok 3 - passing test #3
163 > # partial failed 1 among 3 test(s)
167 test_expect_success 'pretend we have a known breakage' "
168 run_sub_test_lib_test failing-todo 'A failing TODO test' <<-\\EOF &&
169 test_plan 2
170 test_expect_success 'passing test' 'true'
171 test_expect_failure 'pretend we have a known breakage' 'false'
172 test_done
174 check_sub_test_lib_test failing-todo <<-\\EOF
175 > 1..2
176 > ok 1 - passing test
177 > not ok 2 - pretend we have a known breakage # TODO known breakage
178 > # failing still have 1 known breakage(s)
179 > # failing passed all remaining 1 test(s)
183 test_expect_success 'pretend we have fixed a known breakage' "
184 run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
185 test_plan 1
186 test_expect_failure 'pretend we have fixed a known breakage' 'true'
187 test_done
189 check_sub_test_lib_test passing-todo <<-\\EOF
190 > 1..1
191 > ok 1 - pretend we have fixed a known breakage # TODO known breakage vanished
192 > # passing 1 known breakage(s) vanished; please update test(s)
196 test_expect_success 'pretend we have fixed one of two known breakages (run in sub test-lib)' "
197 run_sub_test_lib_test partially-passing-todos \
198 '2 TODO tests, one passing' <<-\\EOF &&
199 test_plan 3
200 test_expect_failure 'pretend we have a known breakage' 'false'
201 test_expect_success 'pretend we have a passing test' 'true'
202 test_expect_failure 'pretend we have fixed another known breakage' 'true'
203 test_done
205 check_sub_test_lib_test partially-passing-todos <<-\\EOF
206 > 1..3
207 > not ok 1 - pretend we have a known breakage # TODO known breakage
208 > ok 2 - pretend we have a passing test
209 > ok 3 - pretend we have fixed another known breakage # TODO known breakage vanished
210 > # partially 1 known breakage(s) vanished; please update test(s)
211 > # partially still have 1 known breakage(s)
212 > # partially passed all remaining 1 test(s)
216 test_expect_success 'pretend we have a pass, fail, and known breakage' "
217 test_must_fail run_sub_test_lib_test \
218 mixed-results1 'mixed results #1' <<-\\EOF &&
219 test_plan 3
220 test_expect_success 'passing test' 'true'
221 test_expect_success 'failing test' 'false'
222 test_expect_failure 'pretend we have a known breakage' 'false'
223 test_done
225 check_sub_test_lib_test mixed-results1 <<-\\EOF
226 > 1..3
227 > ok 1 - passing test
228 > not ok 2 - failing test
229 > # failed: mixed-results1.sh
231 > # false
233 > not ok 3 - pretend we have a known breakage # TODO known breakage
234 > # mixed still have 1 known breakage(s)
235 > # mixed failed 1 among remaining 2 test(s)
239 test_expect_success 'pretend we have a mix of all possible results' "
240 test_must_fail run_sub_test_lib_test \
241 mixed-results2 'mixed results #2' <<-\\EOF &&
242 test_plan 10
243 test_expect_success 'passing test' 'true'
244 test_expect_success 'passing test' 'true'
245 test_expect_success 'passing test' 'true'
246 test_expect_success 'passing test' 'true'
247 test_expect_success 'failing test' 'false'
248 test_expect_success 'failing test' 'false'
249 test_expect_success 'failing test' 'false'
250 test_expect_failure 'pretend we have a known breakage' 'false'
251 test_expect_failure 'pretend we have a known breakage' 'false'
252 test_expect_failure 'pretend we have fixed a known breakage' 'true'
253 test_done
255 check_sub_test_lib_test mixed-results2 <<-\\EOF
256 > 1..10
257 > ok 1 - passing test
258 > ok 2 - passing test
259 > ok 3 - passing test
260 > ok 4 - passing test
261 > not ok 5 - failing test
262 > # failed: mixed-results2.sh
264 > # false
266 > not ok 6 - failing test
267 > # failed: mixed-results2.sh
269 > # false
271 > not ok 7 - failing test
272 > # failed: mixed-results2.sh
274 > # false
276 > not ok 8 - pretend we have a known breakage # TODO known breakage
277 > not ok 9 - pretend we have a known breakage # TODO known breakage
278 > ok 10 - pretend we have fixed a known breakage # TODO known breakage vanished
279 > # mixed 1 known breakage(s) vanished; please update test(s)
280 > # mixed still have 2 known breakage(s)
281 > # mixed failed 3 among remaining 7 test(s)
285 test_expect_success 'test --verbose' '
286 test_must_fail run_sub_test_lib_test \
287 test-verbose "test verbose" --verbose <<-\EOF &&
288 test_plan 3
289 test_expect_success "passing test" true
290 test_expect_success "test with output" "echo foo"
291 test_expect_success "failing test" false
292 test_done
294 mv test-verbose/out test-verbose/out+ &&
295 grep -v "^Initialized empty" test-verbose/out+ >test-verbose/out &&
296 check_sub_test_lib_test test-verbose <<-\EOF
297 > 1..3
298 > expecting success: true
299 > ok 1 - passing test
301 > expecting success: echo foo
302 > foo
303 > ok 2 - test with output
305 > expecting success: false
306 > not ok 3 - failing test
307 > # failed: test-verbose.sh
309 > # false
312 > # test failed 1 among 3 test(s)
316 test_expect_success 'test --verbose-only' '
317 test_must_fail run_sub_test_lib_test \
318 test-verbose-only-2 "test verbose-only=2" \
319 --verbose-only=2 <<-\EOF &&
320 test_plan 3
321 test_expect_success "passing test" true
322 test_expect_success "test with output" "echo foo"
323 test_expect_success "failing test" false
324 test_done
326 check_sub_test_lib_test test-verbose-only-2 <<-\EOF
327 > 1..3
328 > ok 1 - passing test
330 > expecting success: echo foo
331 > foo
332 > ok 2 - test with output
334 > not ok 3 - failing test
335 > # failed: test-verbose-only-2.sh
337 > # false
339 > # test failed 1 among 3 test(s)
343 test_expect_success 'TESTLIB_SKIP_TESTS' "
345 TESTLIB_SKIP_TESTS='testlib.2' && export TESTLIB_SKIP_TESTS &&
346 run_sub_test_lib_test testlib-skip-tests-basic \
347 'TESTLIB_SKIP_TESTS' <<-\\EOF &&
348 test_plan 3
349 for i in 1 2 3
351 test_expect_success \"passing test #\$i\" 'true'
352 done
353 test_done
355 check_sub_test_lib_test testlib-skip-tests-basic <<-\\EOF
356 > 1..3
357 > ok 1 - passing test #1
358 > ok 2 # skip passing test #2 (TESTLIB_SKIP_TESTS)
359 > ok 3 - passing test #3
360 > # testlib passed all 3 test(s)
365 test_expect_success 'TESTLIB_SKIP_TESTS several tests' "
367 TESTLIB_SKIP_TESTS='testlib.2 testlib.5' && export TESTLIB_SKIP_TESTS &&
368 run_sub_test_lib_test testlib-skip-tests-several \
369 'TESTLIB_SKIP_TESTS several tests' <<-\\EOF &&
370 test_plan 6
371 for i in 1 2 3 4 5 6
373 test_expect_success \"passing test #\$i\" 'true'
374 done
375 test_done
377 check_sub_test_lib_test testlib-skip-tests-several <<-\\EOF
378 > 1..6
379 > ok 1 - passing test #1
380 > ok 2 # skip passing test #2 (TESTLIB_SKIP_TESTS)
381 > ok 3 - passing test #3
382 > ok 4 - passing test #4
383 > ok 5 # skip passing test #5 (TESTLIB_SKIP_TESTS)
384 > ok 6 - passing test #6
385 > # testlib passed all 6 test(s)
390 test_expect_success 'TESTLIB_SKIP_TESTS sh pattern' "
392 TESTLIB_SKIP_TESTS='testlib.[2-5]' && export TESTLIB_SKIP_TESTS &&
393 run_sub_test_lib_test testlib-skip-tests-sh-pattern \
394 'TESTLIB_SKIP_TESTS sh pattern' <<-\\EOF &&
395 test_plan 6
396 for i in 1 2 3 4 5 6
398 test_expect_success \"passing test #\$i\" 'true'
399 done
400 test_done
402 check_sub_test_lib_test testlib-skip-tests-sh-pattern <<-\\EOF
403 > 1..6
404 > ok 1 - passing test #1
405 > ok 2 # skip passing test #2 (TESTLIB_SKIP_TESTS)
406 > ok 3 # skip passing test #3 (TESTLIB_SKIP_TESTS)
407 > ok 4 # skip passing test #4 (TESTLIB_SKIP_TESTS)
408 > ok 5 # skip passing test #5 (TESTLIB_SKIP_TESTS)
409 > ok 6 - passing test #6
410 > # testlib passed all 6 test(s)
415 test_expect_success '--run basic' "
416 run_sub_test_lib_test run-basic \
417 '--run basic' --run='1 3 5' <<-\\EOF &&
418 test_plan 6
419 for i in 1 2 3 4 5 6
421 test_expect_success \"passing test #\$i\" 'true'
422 done
423 test_done
425 check_sub_test_lib_test run-basic <<-\\EOF
426 > 1..6
427 > ok 1 - passing test #1
428 > ok 2 # skip passing test #2 (--run)
429 > ok 3 - passing test #3
430 > ok 4 # skip passing test #4 (--run)
431 > ok 5 - passing test #5
432 > ok 6 # skip passing test #6 (--run)
433 > # run passed all 6 test(s)
437 test_expect_success '--run with a range' "
438 run_sub_test_lib_test run-range \
439 '--run with a range' --run='1-3' <<-\\EOF &&
440 test_plan 6
441 for i in 1 2 3 4 5 6
443 test_expect_success \"passing test #\$i\" 'true'
444 done
445 test_done
447 check_sub_test_lib_test run-range <<-\\EOF
448 > 1..6
449 > ok 1 - passing test #1
450 > ok 2 - passing test #2
451 > ok 3 - passing test #3
452 > ok 4 # skip passing test #4 (--run)
453 > ok 5 # skip passing test #5 (--run)
454 > ok 6 # skip passing test #6 (--run)
455 > # run passed all 6 test(s)
459 test_expect_success '--run with two ranges' "
460 run_sub_test_lib_test run-two-ranges \
461 '--run with two ranges' --run='1-2 5-6' <<-\\EOF &&
462 test_plan 6
463 for i in 1 2 3 4 5 6
465 test_expect_success \"passing test #\$i\" 'true'
466 done
467 test_done
469 check_sub_test_lib_test run-two-ranges <<-\\EOF
470 > 1..6
471 > ok 1 - passing test #1
472 > ok 2 - passing test #2
473 > ok 3 # skip passing test #3 (--run)
474 > ok 4 # skip passing test #4 (--run)
475 > ok 5 - passing test #5
476 > ok 6 - passing test #6
477 > # run passed all 6 test(s)
481 test_expect_success '--run with a left open range' "
482 run_sub_test_lib_test run-left-open-range \
483 '--run with a left open range' --run='-3' <<-\\EOF &&
484 test_plan 6
485 for i in 1 2 3 4 5 6
487 test_expect_success \"passing test #\$i\" 'true'
488 done
489 test_done
491 check_sub_test_lib_test run-left-open-range <<-\\EOF
492 > 1..6
493 > ok 1 - passing test #1
494 > ok 2 - passing test #2
495 > ok 3 - passing test #3
496 > ok 4 # skip passing test #4 (--run)
497 > ok 5 # skip passing test #5 (--run)
498 > ok 6 # skip passing test #6 (--run)
499 > # run passed all 6 test(s)
503 test_expect_success '--run with a right open range' "
504 run_sub_test_lib_test run-right-open-range \
505 '--run with a right open range' --run='4-' <<-\\EOF &&
506 test_plan 6
507 for i in 1 2 3 4 5 6
509 test_expect_success \"passing test #\$i\" 'true'
510 done
511 test_done
513 check_sub_test_lib_test run-right-open-range <<-\\EOF
514 > 1..6
515 > ok 1 # skip passing test #1 (--run)
516 > ok 2 # skip passing test #2 (--run)
517 > ok 3 # skip passing test #3 (--run)
518 > ok 4 - passing test #4
519 > ok 5 - passing test #5
520 > ok 6 - passing test #6
521 > # run passed all 6 test(s)
525 test_expect_success '--run with basic negation' "
526 run_sub_test_lib_test run-basic-neg \
527 '--run with basic negation' --run='"'!3'"' <<-\\EOF &&
528 test_plan 6
529 for i in 1 2 3 4 5 6
531 test_expect_success \"passing test #\$i\" 'true'
532 done
533 test_done
535 check_sub_test_lib_test run-basic-neg <<-\\EOF
536 > 1..6
537 > ok 1 - passing test #1
538 > ok 2 - passing test #2
539 > ok 3 # skip passing test #3 (--run)
540 > ok 4 - passing test #4
541 > ok 5 - passing test #5
542 > ok 6 - passing test #6
543 > # run passed all 6 test(s)
547 test_expect_success '--run with two negations' "
548 run_sub_test_lib_test run-two-neg \
549 '--run with two negations' --run='"'!3 !6'"' <<-\\EOF &&
550 test_plan 6
551 for i in 1 2 3 4 5 6
553 test_expect_success \"passing test #\$i\" 'true'
554 done
555 test_done
557 check_sub_test_lib_test run-two-neg <<-\\EOF
558 > 1..6
559 > ok 1 - passing test #1
560 > ok 2 - passing test #2
561 > ok 3 # skip passing test #3 (--run)
562 > ok 4 - passing test #4
563 > ok 5 - passing test #5
564 > ok 6 # skip passing test #6 (--run)
565 > # run passed all 6 test(s)
569 test_expect_success '--run a range and negation' "
570 run_sub_test_lib_test run-range-and-neg \
571 '--run a range and negation' --run='"'-4 !2'"' <<-\\EOF &&
572 test_plan 6
573 for i in 1 2 3 4 5 6
575 test_expect_success \"passing test #\$i\" 'true'
576 done
577 test_done
579 check_sub_test_lib_test run-range-and-neg <<-\\EOF
580 > 1..6
581 > ok 1 - passing test #1
582 > ok 2 # skip passing test #2 (--run)
583 > ok 3 - passing test #3
584 > ok 4 - passing test #4
585 > ok 5 # skip passing test #5 (--run)
586 > ok 6 # skip passing test #6 (--run)
587 > # run passed all 6 test(s)
591 test_expect_success '--run range negation' "
592 run_sub_test_lib_test run-range-neg \
593 '--run range negation' --run='"'!1-3'"' <<-\\EOF &&
594 test_plan 6
595 for i in 1 2 3 4 5 6
597 test_expect_success \"passing test #\$i\" 'true'
598 done
599 test_done
601 check_sub_test_lib_test run-range-neg <<-\\EOF
602 > 1..6
603 > ok 1 # skip passing test #1 (--run)
604 > ok 2 # skip passing test #2 (--run)
605 > ok 3 # skip passing test #3 (--run)
606 > ok 4 - passing test #4
607 > ok 5 - passing test #5
608 > ok 6 - passing test #6
609 > # run passed all 6 test(s)
613 test_expect_success '--run include, exclude and include' "
614 run_sub_test_lib_test run-inc-neg-inc \
615 '--run include, exclude and include' \
616 --run='"'1-5 !1-3 2'"' <<-\\EOF &&
617 test_plan 6
618 for i in 1 2 3 4 5 6
620 test_expect_success \"passing test #\$i\" 'true'
621 done
622 test_done
624 check_sub_test_lib_test run-inc-neg-inc <<-\\EOF
625 > 1..6
626 > ok 1 # skip passing test #1 (--run)
627 > ok 2 - passing test #2
628 > ok 3 # skip passing test #3 (--run)
629 > ok 4 - passing test #4
630 > ok 5 - passing test #5
631 > ok 6 # skip passing test #6 (--run)
632 > # run passed all 6 test(s)
636 test_expect_success '--run include, exclude and include, comma separated' "
637 run_sub_test_lib_test run-inc-neg-inc-comma \
638 '--run include, exclude and include, comma separated' \
639 --run=1-5,\!1-3,2 <<-\\EOF &&
640 test_plan 6
641 for i in 1 2 3 4 5 6
643 test_expect_success \"passing test #\$i\" 'true'
644 done
645 test_done
647 check_sub_test_lib_test run-inc-neg-inc-comma <<-\\EOF
648 > 1..6
649 > ok 1 # skip passing test #1 (--run)
650 > ok 2 - passing test #2
651 > ok 3 # skip passing test #3 (--run)
652 > ok 4 - passing test #4
653 > ok 5 - passing test #5
654 > ok 6 # skip passing test #6 (--run)
655 > # run passed all 6 test(s)
659 test_expect_success '--run exclude and include' "
660 run_sub_test_lib_test run-neg-inc \
661 '--run exclude and include' \
662 --run='"'!3- 5'"' <<-\\EOF &&
663 test_plan 6
664 for i in 1 2 3 4 5 6
666 test_expect_success \"passing test #\$i\" 'true'
667 done
668 test_done
670 check_sub_test_lib_test run-neg-inc <<-\\EOF
671 > 1..6
672 > ok 1 - passing test #1
673 > ok 2 - passing test #2
674 > ok 3 # skip passing test #3 (--run)
675 > ok 4 # skip passing test #4 (--run)
676 > ok 5 - passing test #5
677 > ok 6 # skip passing test #6 (--run)
678 > # run passed all 6 test(s)
682 test_expect_success '--run empty selectors' "
683 run_sub_test_lib_test run-empty-sel \
684 '--run empty selectors' \
685 --run='1,,3,,,5' <<-\\EOF &&
686 test_plan 6
687 for i in 1 2 3 4 5 6
689 test_expect_success \"passing test #\$i\" 'true'
690 done
691 test_done
693 check_sub_test_lib_test run-empty-sel <<-\\EOF
694 > 1..6
695 > ok 1 - passing test #1
696 > ok 2 # skip passing test #2 (--run)
697 > ok 3 - passing test #3
698 > ok 4 # skip passing test #4 (--run)
699 > ok 5 - passing test #5
700 > ok 6 # skip passing test #6 (--run)
701 > # run passed all 6 test(s)
705 test_expect_success '--run invalid range start' "
706 run_sub_test_lib_test_err run-inv-range-start \
707 '--run invalid range start' \
708 --run='a-5' <<-\\EOF &&
709 test_expect_success \"passing test #1\" 'true'
710 test_done
712 check_sub_test_lib_test_err run-inv-range-start \
713 <<-\\EOF_OUT 3<<-\\EOF_ERR
714 > FATAL: Unexpected exit with code 1
715 EOF_OUT
716 > error: --run: invalid non-numeric in range start: 'a-5'
717 EOF_ERR
720 test_expect_success '--run invalid range end' "
721 run_sub_test_lib_test_err run-inv-range-end \
722 '--run invalid range end' \
723 --run='1-z' <<-\\EOF &&
724 test_expect_success \"passing test #1\" 'true'
725 test_done
727 check_sub_test_lib_test_err run-inv-range-end \
728 <<-\\EOF_OUT 3<<-\\EOF_ERR
729 > FATAL: Unexpected exit with code 1
730 EOF_OUT
731 > error: --run: invalid non-numeric in range end: '1-z'
732 EOF_ERR
735 test_expect_success '--run invalid selector' "
736 run_sub_test_lib_test_err run-inv-selector \
737 '--run invalid selector' \
738 --run='1?' <<-\\EOF &&
739 test_expect_success \"passing test #1\" 'true'
740 test_done
742 check_sub_test_lib_test_err run-inv-selector \
743 <<-\\EOF_OUT 3<<-\\EOF_ERR
744 > FATAL: Unexpected exit with code 1
745 EOF_OUT
746 > error: --run: invalid non-numeric in test selector: '1?'
747 EOF_ERR
751 test_set_prereq HAVEIT
752 haveit=no
753 echo "$haveit" >haveit
754 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
755 test_have_prereq HAVEIT &&
756 haveit=yes &&
757 echo "$haveit" >haveit
759 donthaveit=yes
760 echo "$donthaveit" >donthaveit
761 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
762 donthaveit=no &&
763 echo "$donthaveit" >donthaveit
765 haveitf="$(cat haveit)"
766 donthaveitf="$(cat donthaveit)"
767 if test $haveit$donthaveit != noyes || test $haveitf$donthaveitf != yesyes
768 then
769 say "bug in test framework: prerequisite tags do not work reliably"
770 exit 1
773 # Stop with the fussing with files
774 TESTLIB_TEST_NO_SUBSHELL=1
776 test_set_prereq HAVETHIS
777 haveit=no
778 test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
779 test_have_prereq HAVEIT &&
780 test_have_prereq HAVETHIS &&
781 haveit=yes
783 donthaveit=yes
784 test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
785 donthaveit=no
787 donthaveiteither=yes
788 test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
789 donthaveiteither=no
791 if test $haveit$donthaveit$donthaveiteither != yesyesyes
792 then
793 say "bug in test framework: multiple prerequisite tags do not work reliably"
794 exit 1
797 test_lazy_prereq LAZY_TRUE true
798 havetrue=no
799 test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
800 havetrue=yes
802 donthavetrue=yes
803 test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
804 donthavetrue=no
807 if test "$havetrue$donthavetrue" != yesyes
808 then
809 say 'bug in test framework: lazy prerequisites do not work'
810 exit 1
813 test_lazy_prereq LAZY_FALSE false
814 nothavefalse=no
815 test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
816 nothavefalse=yes
818 havefalse=yes
819 test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
820 havefalse=no
823 if test "$nothavefalse$havefalse" != yesyes
824 then
825 say 'bug in test framework: negative lazy prerequisites do not work'
826 exit 1
829 # Mostly done with the no subshell tests
830 sane_unset TESTLIB_TEST_NO_SUBSHELL
832 clean=no
833 test_expect_success 'tests clean up after themselves' '
834 test_when_finished clean=yes
837 if test $clean != yes
838 then
839 say "bug in test framework: basic cleanup command does not work reliably"
840 exit 1
843 test_expect_success 'tests clean up even on failures' "
844 test_must_fail run_sub_test_lib_test \
845 failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
846 test_plan 2
847 test_expect_success 'tests clean up even after a failure' '
848 touch clean-after-failure &&
849 test_when_finished rm clean-after-failure &&
850 (exit 1)
852 test_expect_success 'failure to clean up causes the test to fail' '
853 test_when_finished eval \"(exit 2)\"
855 test_done
857 check_sub_test_lib_test failing-cleanup <<-\\EOF
858 > 1..2
859 > not ok 1 - tests clean up even after a failure
860 > # failed: failing-cleanup.sh
862 > # touch clean-after-failure &&
863 > # test_when_finished rm clean-after-failure &&
864 > # (exit 1)
866 > not ok 2 - failure to clean up causes the test to fail
867 > # failed: failing-cleanup.sh
869 > # test_when_finished eval \"(exit 2)\"
871 > # failing failed 2 among 2 test(s)
875 ################################################################
876 # Basics of the basics
878 # updating a new file without --add should fail.
879 test_expect_success 'git update-index without --add should fail adding' '
880 test_must_fail git update-index should-be-empty
883 # and with --add it should succeed, even if it is empty (it used to fail).
884 test_expect_success 'git update-index with --add should succeed' '
885 git update-index --add should-be-empty
888 TESTLIB_TEST_NO_SUBSHELL=1
889 test_expect_success 'writing tree out with git write-tree' '
890 tree=$(git write-tree)
892 sane_unset TESTLIB_TEST_NO_SUBSHELL
894 # we know the shape and contents of the tree and know the object ID for it.
895 test_expect_success 'validate object ID of a known tree' '
896 test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
899 # Removing paths.
900 test_expect_success 'git update-index without --remove should fail removing' '
901 rm -f should-be-empty full-of-directories &&
902 test_must_fail git update-index should-be-empty
905 test_expect_success 'git update-index with --remove should be able to remove' '
906 git update-index --remove should-be-empty
909 # Empty tree can be written with recent write-tree.
910 TESTLIB_TEST_NO_SUBSHELL=1
911 test_tolerate_failure 'git write-tree should be able to write an empty tree' '
912 tree=$(git write-tree)
914 sane_unset TESTLIB_TEST_NO_SUBSHELL
916 if test -n "$tree"
917 then
918 test_result='test_expect_success'
919 else
920 test_result='test_tolerate_failure'
922 $test_result 'validate object ID of a known tree' '
923 test "$tree" = $EMPTY_TREE
926 # Various types of objects
928 test_expect_success 'adding various types of objects with git update-index --add' '
929 mkdir path2 path3 path3/subp3 &&
930 paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
932 for p in $paths
934 echo "hello $p" >$p || exit 1
935 test_ln_s_add "hello $p" ${p}sym || exit 1
936 done
937 ) &&
938 find path* ! -type d -print | xargs git update-index --add
941 # Show them and see that matches what we expect.
942 test_expect_success 'showing stage with git ls-files --stage' '
943 git ls-files --stage >current
946 test_expect_success 'validate git ls-files output for a known tree' '
947 cat >expected <<-\EOF &&
948 100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
949 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
950 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
951 120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
952 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
953 120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
954 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
955 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
957 test_cmp expected current
960 TESTLIB_TEST_NO_SUBSHELL=1
961 test_expect_success 'writing tree out with git write-tree' '
962 tree=$(git write-tree)
964 sane_unset TESTLIB_TEST_NO_SUBSHELL
966 test_expect_success 'validate object ID for a known tree' '
967 test "$tree" = 087704a96baf1c2d1c869a8b084481e121c88b5b
970 test_expect_success 'showing tree with git ls-tree' '
971 git ls-tree $tree >current
974 test_expect_success 'git ls-tree output for a known tree' '
975 cat >expected <<-\EOF &&
976 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
977 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
978 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
979 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
981 test_cmp expected current
984 # This changed in ls-tree pathspec change -- recursive does
985 # not show tree nodes anymore.
986 test_expect_success 'showing tree with git ls-tree -r' '
987 git ls-tree -r $tree >current
990 test_expect_success 'git ls-tree -r output for a known tree' '
991 cat >expected <<-\EOF &&
992 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
993 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
994 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
995 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
996 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
997 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
998 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
999 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
1001 test_cmp expected current
1004 # But with -r -t we can have both.
1005 test_expect_success 'showing tree with git ls-tree -r -t' '
1006 git ls-tree -r -t $tree >current
1009 test_expect_success 'git ls-tree -r output for a known tree' '
1010 cat >expected <<-\EOF &&
1011 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
1012 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
1013 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
1014 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
1015 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
1016 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
1017 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
1018 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
1019 040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
1020 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
1021 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
1023 test_cmp expected current
1026 TESTLIB_TEST_NO_SUBSHELL=1
1027 test_expect_success 'writing partial tree out with git write-tree --prefix' '
1028 ptree=$(git write-tree --prefix=path3)
1030 sane_unset TESTLIB_TEST_NO_SUBSHELL
1032 test_expect_success 'validate object ID for a known tree' '
1033 test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3
1036 TESTLIB_TEST_NO_SUBSHELL=1
1037 test_expect_success 'writing partial tree out with git write-tree --prefix' '
1038 ptree=$(git write-tree --prefix=path3/subp3)
1040 sane_unset TESTLIB_TEST_NO_SUBSHELL
1042 test_expect_success 'validate object ID for a known tree' '
1043 test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2
1046 test_expect_success 'put invalid objects into the index' '
1047 rm -f .git/index &&
1048 cat >badobjects <<-\EOF &&
1049 100644 blob 1000000000000000000000000000000000000000 dir/file1
1050 100644 blob 2000000000000000000000000000000000000000 dir/file2
1051 100644 blob 3000000000000000000000000000000000000000 dir/file3
1052 100644 blob 4000000000000000000000000000000000000000 dir/file4
1053 100644 blob 5000000000000000000000000000000000000000 dir/file5
1055 git update-index --index-info <badobjects
1058 test_expect_success 'writing this tree without --missing-ok' '
1059 test_must_fail git write-tree
1062 test_expect_success 'writing this tree with --missing-ok' '
1063 git write-tree --missing-ok
1067 ################################################################
1068 test_expect_success 'git read-tree followed by write-tree should be idempotent' '
1069 rm -f .git/index &&
1070 git read-tree $tree &&
1071 test -f .git/index &&
1072 newtree=$(git write-tree) &&
1073 test "$newtree" = "$tree"
1076 test_expect_success 'validate git diff-files output for a know cache/work tree state' '
1077 cat >expected <<\EOF &&
1078 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
1079 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
1080 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
1081 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
1082 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
1083 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
1084 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
1085 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
1087 git diff-files >current &&
1088 test_cmp current expected
1091 test_expect_success 'git update-index --refresh should succeed' '
1092 git update-index --refresh
1095 test_expect_success 'no diff after checkout and git update-index --refresh' '
1096 git diff-files >current &&
1097 cmp -s current /dev/null
1100 ################################################################
1101 P=087704a96baf1c2d1c869a8b084481e121c88b5b
1103 TESTLIB_TEST_NO_SUBSHELL=1
1104 test_expect_success 'git commit-tree records the correct tree in a commit' '
1105 commit0=$(echo NO | git commit-tree $P) &&
1106 tree=$(git show --pretty=raw $commit0 |
1107 sed -n -e "s/^tree //p" -e "/^author /q") &&
1108 test "z$tree" = "z$P"
1110 sane_unset TESTLIB_TEST_NO_SUBSHELL
1112 test_expect_success 'git commit-tree records the correct parent in a commit' '
1113 commit1=$(echo NO | git commit-tree $P -p $commit0) &&
1114 parent=$(git show --pretty=raw $commit1 |
1115 sed -n -e "s/^parent //p" -e "/^author /q") &&
1116 test "z$commit0" = "z$parent"
1119 test_expect_success 'git commit-tree omits duplicated parent in a commit' '
1120 commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
1121 parent=$(git show --pretty=raw $commit2 |
1122 sed -n -e "s/^parent //p" -e "/^author /q" |
1123 sort -u) &&
1124 test "z$commit0" = "z$parent" &&
1125 numparent=$(git show --pretty=raw $commit2 |
1126 sed -n -e "s/^parent //p" -e "/^author /q" |
1127 wc -l) &&
1128 test $numparent = 1
1131 test_expect_success 'update-index D/F conflict' '
1132 mv path0 tmp &&
1133 mv path2 path0 &&
1134 mv tmp path2 &&
1135 git update-index --add --replace path2 path0/file2 &&
1136 numpath0=$(git ls-files path0 | wc -l) &&
1137 test $numpath0 = 1
1140 test_expect_success 'very long name in the index handled sanely' '
1142 a=a && # 1
1143 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
1144 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
1145 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
1146 a=${a}q &&
1148 >path4 &&
1149 git update-index --add path4 &&
1151 git ls-files -s path4 |
1152 sed -e "s/ .*/ /" |
1153 tr -d "\012"
1154 echo "$a"
1155 ) | git update-index --index-info &&
1156 len=$(git ls-files "a*" | wc -c) &&
1157 test $len = 4098
1160 test_done