testlib-tg: add tg_test_setup_topgit function
[topgit/pro.git] / t / t0002-testlib-basic.sh
blob14b2d887ce843370f4c256a4cf50af4aa83ab586
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 for i in 1 2 3
131 test_expect_success \"passing test #\$i\" 'true'
132 done
133 test_done
135 check_sub_test_lib_test full-pass <<-\\EOF
136 > ok 1 - passing test #1
137 > ok 2 - passing test #2
138 > ok 3 - passing test #3
139 > # full passed all 3 test(s)
140 > 1..3
144 test_expect_success 'pretend we have a partially passing test suite' "
145 test_must_fail run_sub_test_lib_test \
146 partial-pass '2/3 tests passing' <<-\\EOF &&
147 test_expect_success 'passing test #1' 'true'
148 test_expect_success 'failing test #2' 'false'
149 test_expect_success 'passing test #3' 'true'
150 test_done
152 check_sub_test_lib_test partial-pass <<-\\EOF
153 > ok 1 - passing test #1
154 > not ok 2 - failing test #2
155 # failed: partial-pass.sh
157 # false
159 > ok 3 - passing test #3
160 > # partial failed 1 among 3 test(s)
161 > 1..3
165 test_expect_success 'pretend we have a known breakage' "
166 run_sub_test_lib_test failing-todo 'A failing TODO test' <<-\\EOF &&
167 test_expect_success 'passing test' 'true'
168 test_expect_failure 'pretend we have a known breakage' 'false'
169 test_done
171 check_sub_test_lib_test failing-todo <<-\\EOF
172 > ok 1 - passing test
173 > not ok 2 - pretend we have a known breakage # TODO known breakage
174 > # failing still have 1 known breakage(s)
175 > # failing passed all remaining 1 test(s)
176 > 1..2
180 test_expect_success 'pretend we have fixed a known breakage' "
181 run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
182 test_expect_failure 'pretend we have fixed a known breakage' 'true'
183 test_done
185 check_sub_test_lib_test passing-todo <<-\\EOF
186 > ok 1 - pretend we have fixed a known breakage # TODO known breakage vanished
187 > # passing 1 known breakage(s) vanished; please update test(s)
188 > 1..1
192 test_expect_success 'pretend we have fixed one of two known breakages (run in sub test-lib)' "
193 run_sub_test_lib_test partially-passing-todos \
194 '2 TODO tests, one passing' <<-\\EOF &&
195 test_expect_failure 'pretend we have a known breakage' 'false'
196 test_expect_success 'pretend we have a passing test' 'true'
197 test_expect_failure 'pretend we have fixed another known breakage' 'true'
198 test_done
200 check_sub_test_lib_test partially-passing-todos <<-\\EOF
201 > not ok 1 - pretend we have a known breakage # TODO known breakage
202 > ok 2 - pretend we have a passing test
203 > ok 3 - pretend we have fixed another known breakage # TODO known breakage vanished
204 > # partially 1 known breakage(s) vanished; please update test(s)
205 > # partially still have 1 known breakage(s)
206 > # partially passed all remaining 1 test(s)
207 > 1..3
211 test_expect_success 'pretend we have a pass, fail, and known breakage' "
212 test_must_fail run_sub_test_lib_test \
213 mixed-results1 'mixed results #1' <<-\\EOF &&
214 test_expect_success 'passing test' 'true'
215 test_expect_success 'failing test' 'false'
216 test_expect_failure 'pretend we have a known breakage' 'false'
217 test_done
219 check_sub_test_lib_test mixed-results1 <<-\\EOF
220 > ok 1 - passing test
221 > not ok 2 - failing test
222 > # failed: mixed-results1.sh
224 > # false
226 > not ok 3 - pretend we have a known breakage # TODO known breakage
227 > # mixed still have 1 known breakage(s)
228 > # mixed failed 1 among remaining 2 test(s)
229 > 1..3
233 test_expect_success 'pretend we have a mix of all possible results' "
234 test_must_fail run_sub_test_lib_test \
235 mixed-results2 'mixed results #2' <<-\\EOF &&
236 test_expect_success 'passing test' 'true'
237 test_expect_success 'passing test' 'true'
238 test_expect_success 'passing test' 'true'
239 test_expect_success 'passing test' 'true'
240 test_expect_success 'failing test' 'false'
241 test_expect_success 'failing test' 'false'
242 test_expect_success 'failing test' 'false'
243 test_expect_failure 'pretend we have a known breakage' 'false'
244 test_expect_failure 'pretend we have a known breakage' 'false'
245 test_expect_failure 'pretend we have fixed a known breakage' 'true'
246 test_done
248 check_sub_test_lib_test mixed-results2 <<-\\EOF
249 > ok 1 - passing test
250 > ok 2 - passing test
251 > ok 3 - passing test
252 > ok 4 - passing test
253 > not ok 5 - failing test
254 > # failed: mixed-results2.sh
256 > # false
258 > not ok 6 - failing test
259 > # failed: mixed-results2.sh
261 > # false
263 > not ok 7 - failing test
264 > # failed: mixed-results2.sh
266 > # false
268 > not ok 8 - pretend we have a known breakage # TODO known breakage
269 > not ok 9 - pretend we have a known breakage # TODO known breakage
270 > ok 10 - pretend we have fixed a known breakage # TODO known breakage vanished
271 > # mixed 1 known breakage(s) vanished; please update test(s)
272 > # mixed still have 2 known breakage(s)
273 > # mixed failed 3 among remaining 7 test(s)
274 > 1..10
278 test_expect_success 'test --verbose' '
279 test_must_fail run_sub_test_lib_test \
280 test-verbose "test verbose" --verbose <<-\EOF &&
281 test_expect_success "passing test" true
282 test_expect_success "test with output" "echo foo"
283 test_expect_success "failing test" false
284 test_done
286 mv test-verbose/out test-verbose/out+ &&
287 grep -v "^Initialized empty" test-verbose/out+ >test-verbose/out &&
288 check_sub_test_lib_test test-verbose <<-\EOF
289 > expecting success: true
290 > ok 1 - passing test
292 > expecting success: echo foo
293 > foo
294 > ok 2 - test with output
296 > expecting success: false
297 > not ok 3 - failing test
298 > # failed: test-verbose.sh
300 > # false
303 > # test failed 1 among 3 test(s)
304 > 1..3
308 test_expect_success 'test --verbose-only' '
309 test_must_fail run_sub_test_lib_test \
310 test-verbose-only-2 "test verbose-only=2" \
311 --verbose-only=2 <<-\EOF &&
312 test_expect_success "passing test" true
313 test_expect_success "test with output" "echo foo"
314 test_expect_success "failing test" false
315 test_done
317 check_sub_test_lib_test test-verbose-only-2 <<-\EOF
318 > ok 1 - passing test
320 > expecting success: echo foo
321 > foo
322 > ok 2 - test with output
324 > not ok 3 - failing test
325 > # failed: test-verbose-only-2.sh
327 > # false
329 > # test failed 1 among 3 test(s)
330 > 1..3
334 test_expect_success 'TESTLIB_SKIP_TESTS' "
336 TESTLIB_SKIP_TESTS='testlib.2' && export TESTLIB_SKIP_TESTS &&
337 run_sub_test_lib_test testlib-skip-tests-basic \
338 'TESTLIB_SKIP_TESTS' <<-\\EOF &&
339 for i in 1 2 3
341 test_expect_success \"passing test #\$i\" 'true'
342 done
343 test_done
345 check_sub_test_lib_test testlib-skip-tests-basic <<-\\EOF
346 > ok 1 - passing test #1
347 > ok 2 # skip passing test #2 (TESTLIB_SKIP_TESTS)
348 > ok 3 - passing test #3
349 > # testlib passed all 3 test(s)
350 > 1..3
355 test_expect_success 'TESTLIB_SKIP_TESTS several tests' "
357 TESTLIB_SKIP_TESTS='testlib.2 testlib.5' && export TESTLIB_SKIP_TESTS &&
358 run_sub_test_lib_test testlib-skip-tests-several \
359 'TESTLIB_SKIP_TESTS several tests' <<-\\EOF &&
360 for i in 1 2 3 4 5 6
362 test_expect_success \"passing test #\$i\" 'true'
363 done
364 test_done
366 check_sub_test_lib_test testlib-skip-tests-several <<-\\EOF
367 > ok 1 - passing test #1
368 > ok 2 # skip passing test #2 (TESTLIB_SKIP_TESTS)
369 > ok 3 - passing test #3
370 > ok 4 - passing test #4
371 > ok 5 # skip passing test #5 (TESTLIB_SKIP_TESTS)
372 > ok 6 - passing test #6
373 > # testlib passed all 6 test(s)
374 > 1..6
379 test_expect_success 'TESTLIB_SKIP_TESTS sh pattern' "
381 TESTLIB_SKIP_TESTS='testlib.[2-5]' && export TESTLIB_SKIP_TESTS &&
382 run_sub_test_lib_test testlib-skip-tests-sh-pattern \
383 'TESTLIB_SKIP_TESTS sh pattern' <<-\\EOF &&
384 for i in 1 2 3 4 5 6
386 test_expect_success \"passing test #\$i\" 'true'
387 done
388 test_done
390 check_sub_test_lib_test testlib-skip-tests-sh-pattern <<-\\EOF
391 > ok 1 - passing test #1
392 > ok 2 # skip passing test #2 (TESTLIB_SKIP_TESTS)
393 > ok 3 # skip passing test #3 (TESTLIB_SKIP_TESTS)
394 > ok 4 # skip passing test #4 (TESTLIB_SKIP_TESTS)
395 > ok 5 # skip passing test #5 (TESTLIB_SKIP_TESTS)
396 > ok 6 - passing test #6
397 > # testlib passed all 6 test(s)
398 > 1..6
403 test_expect_success '--run basic' "
404 run_sub_test_lib_test run-basic \
405 '--run basic' --run='1 3 5' <<-\\EOF &&
406 for i in 1 2 3 4 5 6
408 test_expect_success \"passing test #\$i\" 'true'
409 done
410 test_done
412 check_sub_test_lib_test run-basic <<-\\EOF
413 > ok 1 - passing test #1
414 > ok 2 # skip passing test #2 (--run)
415 > ok 3 - passing test #3
416 > ok 4 # skip passing test #4 (--run)
417 > ok 5 - passing test #5
418 > ok 6 # skip passing test #6 (--run)
419 > # run passed all 6 test(s)
420 > 1..6
424 test_expect_success '--run with a range' "
425 run_sub_test_lib_test run-range \
426 '--run with a range' --run='1-3' <<-\\EOF &&
427 for i in 1 2 3 4 5 6
429 test_expect_success \"passing test #\$i\" 'true'
430 done
431 test_done
433 check_sub_test_lib_test run-range <<-\\EOF
434 > ok 1 - passing test #1
435 > ok 2 - passing test #2
436 > ok 3 - passing test #3
437 > ok 4 # skip passing test #4 (--run)
438 > ok 5 # skip passing test #5 (--run)
439 > ok 6 # skip passing test #6 (--run)
440 > # run passed all 6 test(s)
441 > 1..6
445 test_expect_success '--run with two ranges' "
446 run_sub_test_lib_test run-two-ranges \
447 '--run with two ranges' --run='1-2 5-6' <<-\\EOF &&
448 for i in 1 2 3 4 5 6
450 test_expect_success \"passing test #\$i\" 'true'
451 done
452 test_done
454 check_sub_test_lib_test run-two-ranges <<-\\EOF
455 > ok 1 - passing test #1
456 > ok 2 - passing test #2
457 > ok 3 # skip passing test #3 (--run)
458 > ok 4 # skip passing test #4 (--run)
459 > ok 5 - passing test #5
460 > ok 6 - passing test #6
461 > # run passed all 6 test(s)
462 > 1..6
466 test_expect_success '--run with a left open range' "
467 run_sub_test_lib_test run-left-open-range \
468 '--run with a left open range' --run='-3' <<-\\EOF &&
469 for i in 1 2 3 4 5 6
471 test_expect_success \"passing test #\$i\" 'true'
472 done
473 test_done
475 check_sub_test_lib_test run-left-open-range <<-\\EOF
476 > ok 1 - passing test #1
477 > ok 2 - passing test #2
478 > ok 3 - passing test #3
479 > ok 4 # skip passing test #4 (--run)
480 > ok 5 # skip passing test #5 (--run)
481 > ok 6 # skip passing test #6 (--run)
482 > # run passed all 6 test(s)
483 > 1..6
487 test_expect_success '--run with a right open range' "
488 run_sub_test_lib_test run-right-open-range \
489 '--run with a right open range' --run='4-' <<-\\EOF &&
490 for i in 1 2 3 4 5 6
492 test_expect_success \"passing test #\$i\" 'true'
493 done
494 test_done
496 check_sub_test_lib_test run-right-open-range <<-\\EOF
497 > ok 1 # skip passing test #1 (--run)
498 > ok 2 # skip passing test #2 (--run)
499 > ok 3 # skip passing test #3 (--run)
500 > ok 4 - passing test #4
501 > ok 5 - passing test #5
502 > ok 6 - passing test #6
503 > # run passed all 6 test(s)
504 > 1..6
508 test_expect_success '--run with basic negation' "
509 run_sub_test_lib_test run-basic-neg \
510 '--run with basic negation' --run='"'!3'"' <<-\\EOF &&
511 for i in 1 2 3 4 5 6
513 test_expect_success \"passing test #\$i\" 'true'
514 done
515 test_done
517 check_sub_test_lib_test run-basic-neg <<-\\EOF
518 > ok 1 - passing test #1
519 > ok 2 - passing test #2
520 > ok 3 # skip passing test #3 (--run)
521 > ok 4 - passing test #4
522 > ok 5 - passing test #5
523 > ok 6 - passing test #6
524 > # run passed all 6 test(s)
525 > 1..6
529 test_expect_success '--run with two negations' "
530 run_sub_test_lib_test run-two-neg \
531 '--run with two negations' --run='"'!3 !6'"' <<-\\EOF &&
532 for i in 1 2 3 4 5 6
534 test_expect_success \"passing test #\$i\" 'true'
535 done
536 test_done
538 check_sub_test_lib_test run-two-neg <<-\\EOF
539 > ok 1 - passing test #1
540 > ok 2 - passing test #2
541 > ok 3 # skip passing test #3 (--run)
542 > ok 4 - passing test #4
543 > ok 5 - passing test #5
544 > ok 6 # skip passing test #6 (--run)
545 > # run passed all 6 test(s)
546 > 1..6
550 test_expect_success '--run a range and negation' "
551 run_sub_test_lib_test run-range-and-neg \
552 '--run a range and negation' --run='"'-4 !2'"' <<-\\EOF &&
553 for i in 1 2 3 4 5 6
555 test_expect_success \"passing test #\$i\" 'true'
556 done
557 test_done
559 check_sub_test_lib_test run-range-and-neg <<-\\EOF
560 > ok 1 - passing test #1
561 > ok 2 # skip passing test #2 (--run)
562 > ok 3 - passing test #3
563 > ok 4 - passing test #4
564 > ok 5 # skip passing test #5 (--run)
565 > ok 6 # skip passing test #6 (--run)
566 > # run passed all 6 test(s)
567 > 1..6
571 test_expect_success '--run range negation' "
572 run_sub_test_lib_test run-range-neg \
573 '--run range negation' --run='"'!1-3'"' <<-\\EOF &&
574 for i in 1 2 3 4 5 6
576 test_expect_success \"passing test #\$i\" 'true'
577 done
578 test_done
580 check_sub_test_lib_test run-range-neg <<-\\EOF
581 > ok 1 # skip passing test #1 (--run)
582 > ok 2 # skip passing test #2 (--run)
583 > ok 3 # skip passing test #3 (--run)
584 > ok 4 - passing test #4
585 > ok 5 - passing test #5
586 > ok 6 - passing test #6
587 > # run passed all 6 test(s)
588 > 1..6
592 test_expect_success '--run include, exclude and include' "
593 run_sub_test_lib_test run-inc-neg-inc \
594 '--run include, exclude and include' \
595 --run='"'1-5 !1-3 2'"' <<-\\EOF &&
596 for i in 1 2 3 4 5 6
598 test_expect_success \"passing test #\$i\" 'true'
599 done
600 test_done
602 check_sub_test_lib_test run-inc-neg-inc <<-\\EOF
603 > ok 1 # skip passing test #1 (--run)
604 > ok 2 - passing test #2
605 > ok 3 # skip passing test #3 (--run)
606 > ok 4 - passing test #4
607 > ok 5 - passing test #5
608 > ok 6 # skip passing test #6 (--run)
609 > # run passed all 6 test(s)
610 > 1..6
614 test_expect_success '--run include, exclude and include, comma separated' "
615 run_sub_test_lib_test run-inc-neg-inc-comma \
616 '--run include, exclude and include, comma separated' \
617 --run=1-5,\!1-3,2 <<-\\EOF &&
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-comma <<-\\EOF
625 > ok 1 # skip passing test #1 (--run)
626 > ok 2 - passing test #2
627 > ok 3 # skip passing test #3 (--run)
628 > ok 4 - passing test #4
629 > ok 5 - passing test #5
630 > ok 6 # skip passing test #6 (--run)
631 > # run passed all 6 test(s)
632 > 1..6
636 test_expect_success '--run exclude and include' "
637 run_sub_test_lib_test run-neg-inc \
638 '--run exclude and include' \
639 --run='"'!3- 5'"' <<-\\EOF &&
640 for i in 1 2 3 4 5 6
642 test_expect_success \"passing test #\$i\" 'true'
643 done
644 test_done
646 check_sub_test_lib_test run-neg-inc <<-\\EOF
647 > ok 1 - passing test #1
648 > ok 2 - passing test #2
649 > ok 3 # skip passing test #3 (--run)
650 > ok 4 # skip passing test #4 (--run)
651 > ok 5 - passing test #5
652 > ok 6 # skip passing test #6 (--run)
653 > # run passed all 6 test(s)
654 > 1..6
658 test_expect_success '--run empty selectors' "
659 run_sub_test_lib_test run-empty-sel \
660 '--run empty selectors' \
661 --run='1,,3,,,5' <<-\\EOF &&
662 for i in 1 2 3 4 5 6
664 test_expect_success \"passing test #\$i\" 'true'
665 done
666 test_done
668 check_sub_test_lib_test run-empty-sel <<-\\EOF
669 > ok 1 - passing test #1
670 > ok 2 # skip passing test #2 (--run)
671 > ok 3 - passing test #3
672 > ok 4 # skip passing test #4 (--run)
673 > ok 5 - passing test #5
674 > ok 6 # skip passing test #6 (--run)
675 > # run passed all 6 test(s)
676 > 1..6
680 test_expect_success '--run invalid range start' "
681 run_sub_test_lib_test_err run-inv-range-start \
682 '--run invalid range start' \
683 --run='a-5' <<-\\EOF &&
684 test_expect_success \"passing test #1\" 'true'
685 test_done
687 check_sub_test_lib_test_err run-inv-range-start \
688 <<-\\EOF_OUT 3<<-\\EOF_ERR
689 > FATAL: Unexpected exit with code 1
690 EOF_OUT
691 > error: --run: invalid non-numeric in range start: 'a-5'
692 EOF_ERR
695 test_expect_success '--run invalid range end' "
696 run_sub_test_lib_test_err run-inv-range-end \
697 '--run invalid range end' \
698 --run='1-z' <<-\\EOF &&
699 test_expect_success \"passing test #1\" 'true'
700 test_done
702 check_sub_test_lib_test_err run-inv-range-end \
703 <<-\\EOF_OUT 3<<-\\EOF_ERR
704 > FATAL: Unexpected exit with code 1
705 EOF_OUT
706 > error: --run: invalid non-numeric in range end: '1-z'
707 EOF_ERR
710 test_expect_success '--run invalid selector' "
711 run_sub_test_lib_test_err run-inv-selector \
712 '--run invalid selector' \
713 --run='1?' <<-\\EOF &&
714 test_expect_success \"passing test #1\" 'true'
715 test_done
717 check_sub_test_lib_test_err run-inv-selector \
718 <<-\\EOF_OUT 3<<-\\EOF_ERR
719 > FATAL: Unexpected exit with code 1
720 EOF_OUT
721 > error: --run: invalid non-numeric in test selector: '1?'
722 EOF_ERR
726 test_set_prereq HAVEIT
727 haveit=no
728 echo "$haveit" >haveit
729 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
730 test_have_prereq HAVEIT &&
731 haveit=yes &&
732 echo "$haveit" >haveit
734 donthaveit=yes
735 echo "$donthaveit" >donthaveit
736 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
737 donthaveit=no &&
738 echo "$donthaveit" >donthaveit
740 haveitf="$(cat haveit)"
741 donthaveitf="$(cat donthaveit)"
742 if test $haveit$donthaveit != noyes || test $haveitf$donthaveitf != yesyes
743 then
744 say "bug in test framework: prerequisite tags do not work reliably"
745 exit 1
748 # Stop with the fussing with files
749 TESTLIB_TEST_NO_SUBSHELL=1
751 test_set_prereq HAVETHIS
752 haveit=no
753 test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
754 test_have_prereq HAVEIT &&
755 test_have_prereq HAVETHIS &&
756 haveit=yes
758 donthaveit=yes
759 test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
760 donthaveit=no
762 donthaveiteither=yes
763 test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
764 donthaveiteither=no
766 if test $haveit$donthaveit$donthaveiteither != yesyesyes
767 then
768 say "bug in test framework: multiple prerequisite tags do not work reliably"
769 exit 1
772 test_lazy_prereq LAZY_TRUE true
773 havetrue=no
774 test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
775 havetrue=yes
777 donthavetrue=yes
778 test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
779 donthavetrue=no
782 if test "$havetrue$donthavetrue" != yesyes
783 then
784 say 'bug in test framework: lazy prerequisites do not work'
785 exit 1
788 test_lazy_prereq LAZY_FALSE false
789 nothavefalse=no
790 test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
791 nothavefalse=yes
793 havefalse=yes
794 test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
795 havefalse=no
798 if test "$nothavefalse$havefalse" != yesyes
799 then
800 say 'bug in test framework: negative lazy prerequisites do not work'
801 exit 1
804 # Mostly done with the no subshell tests
805 sane_unset TESTLIB_TEST_NO_SUBSHELL
807 clean=no
808 test_expect_success 'tests clean up after themselves' '
809 test_when_finished clean=yes
812 if test $clean != yes
813 then
814 say "bug in test framework: basic cleanup command does not work reliably"
815 exit 1
818 test_expect_success 'tests clean up even on failures' "
819 test_must_fail run_sub_test_lib_test \
820 failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
821 test_expect_success 'tests clean up even after a failure' '
822 touch clean-after-failure &&
823 test_when_finished rm clean-after-failure &&
824 (exit 1)
826 test_expect_success 'failure to clean up causes the test to fail' '
827 test_when_finished eval \"(exit 2)\"
829 test_done
831 check_sub_test_lib_test failing-cleanup <<-\\EOF
832 > not ok 1 - tests clean up even after a failure
833 > # failed: failing-cleanup.sh
835 > # touch clean-after-failure &&
836 > # test_when_finished rm clean-after-failure &&
837 > # (exit 1)
839 > not ok 2 - failure to clean up causes the test to fail
840 > # failed: failing-cleanup.sh
842 > # test_when_finished eval \"(exit 2)\"
844 > # failing failed 2 among 2 test(s)
845 > 1..2
849 ################################################################
850 # Basics of the basics
852 # updating a new file without --add should fail.
853 test_expect_success 'git update-index without --add should fail adding' '
854 test_must_fail git update-index should-be-empty
857 # and with --add it should succeed, even if it is empty (it used to fail).
858 test_expect_success 'git update-index with --add should succeed' '
859 git update-index --add should-be-empty
862 TESTLIB_TEST_NO_SUBSHELL=1
863 test_expect_success 'writing tree out with git write-tree' '
864 tree=$(git write-tree)
866 sane_unset TESTLIB_TEST_NO_SUBSHELL
868 # we know the shape and contents of the tree and know the object ID for it.
869 test_expect_success 'validate object ID of a known tree' '
870 test "$tree" = 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
873 # Removing paths.
874 test_expect_success 'git update-index without --remove should fail removing' '
875 rm -f should-be-empty full-of-directories &&
876 test_must_fail git update-index should-be-empty
879 test_expect_success 'git update-index with --remove should be able to remove' '
880 git update-index --remove should-be-empty
883 # Empty tree can be written with recent write-tree.
884 TESTLIB_TEST_NO_SUBSHELL=1
885 test_tolerate_failure 'git write-tree should be able to write an empty tree' '
886 tree=$(git write-tree)
888 sane_unset TESTLIB_TEST_NO_SUBSHELL
890 if test -n "$tree"
891 then
892 test_result='test_expect_success'
893 else
894 test_result='test_tolerate_failure'
896 $test_result 'validate object ID of a known tree' '
897 test "$tree" = $EMPTY_TREE
900 # Various types of objects
902 test_expect_success 'adding various types of objects with git update-index --add' '
903 mkdir path2 path3 path3/subp3 &&
904 paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
906 for p in $paths
908 echo "hello $p" >$p || exit 1
909 test_ln_s_add "hello $p" ${p}sym || exit 1
910 done
911 ) &&
912 find path* ! -type d -print | xargs git update-index --add
915 # Show them and see that matches what we expect.
916 test_expect_success 'showing stage with git ls-files --stage' '
917 git ls-files --stage >current
920 test_expect_success 'validate git ls-files output for a known tree' '
921 cat >expected <<-\EOF &&
922 100644 f87290f8eb2cbbea7857214459a0739927eab154 0 path0
923 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0 path0sym
924 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0 path2/file2
925 120000 d8ce161addc5173867a3c3c730924388daedbc38 0 path2/file2sym
926 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0 path3/file3
927 120000 8599103969b43aff7e430efea79ca4636466794f 0 path3/file3sym
928 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0 path3/subp3/file3
929 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0 path3/subp3/file3sym
931 test_cmp expected current
934 TESTLIB_TEST_NO_SUBSHELL=1
935 test_expect_success 'writing tree out with git write-tree' '
936 tree=$(git write-tree)
938 sane_unset TESTLIB_TEST_NO_SUBSHELL
940 test_expect_success 'validate object ID for a known tree' '
941 test "$tree" = 087704a96baf1c2d1c869a8b084481e121c88b5b
944 test_expect_success 'showing tree with git ls-tree' '
945 git ls-tree $tree >current
948 test_expect_success 'git ls-tree output for a known tree' '
949 cat >expected <<-\EOF &&
950 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
951 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
952 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
953 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
955 test_cmp expected current
958 # This changed in ls-tree pathspec change -- recursive does
959 # not show tree nodes anymore.
960 test_expect_success 'showing tree with git ls-tree -r' '
961 git ls-tree -r $tree >current
964 test_expect_success 'git ls-tree -r output for a known tree' '
965 cat >expected <<-\EOF &&
966 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
967 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
968 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
969 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
970 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
971 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
972 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
973 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
975 test_cmp expected current
978 # But with -r -t we can have both.
979 test_expect_success 'showing tree with git ls-tree -r -t' '
980 git ls-tree -r -t $tree >current
983 test_expect_success 'git ls-tree -r output for a known tree' '
984 cat >expected <<-\EOF &&
985 100644 blob f87290f8eb2cbbea7857214459a0739927eab154 path0
986 120000 blob 15a98433ae33114b085f3eb3bb03b832b3180a01 path0sym
987 040000 tree 58a09c23e2ca152193f2786e06986b7b6712bdbe path2
988 100644 blob 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 path2/file2
989 120000 blob d8ce161addc5173867a3c3c730924388daedbc38 path2/file2sym
990 040000 tree 21ae8269cacbe57ae09138dcc3a2887f904d02b3 path3
991 100644 blob 0aa34cae68d0878578ad119c86ca2b5ed5b28376 path3/file3
992 120000 blob 8599103969b43aff7e430efea79ca4636466794f path3/file3sym
993 040000 tree 3c5e5399f3a333eddecce7a9b9465b63f65f51e2 path3/subp3
994 100644 blob 00fb5908cb97c2564a9783c0c64087333b3b464f path3/subp3/file3
995 120000 blob 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c path3/subp3/file3sym
997 test_cmp expected current
1000 TESTLIB_TEST_NO_SUBSHELL=1
1001 test_expect_success 'writing partial tree out with git write-tree --prefix' '
1002 ptree=$(git write-tree --prefix=path3)
1004 sane_unset TESTLIB_TEST_NO_SUBSHELL
1006 test_expect_success 'validate object ID for a known tree' '
1007 test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3
1010 TESTLIB_TEST_NO_SUBSHELL=1
1011 test_expect_success 'writing partial tree out with git write-tree --prefix' '
1012 ptree=$(git write-tree --prefix=path3/subp3)
1014 sane_unset TESTLIB_TEST_NO_SUBSHELL
1016 test_expect_success 'validate object ID for a known tree' '
1017 test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2
1020 test_expect_success 'put invalid objects into the index' '
1021 rm -f .git/index &&
1022 cat >badobjects <<-\EOF &&
1023 100644 blob 1000000000000000000000000000000000000000 dir/file1
1024 100644 blob 2000000000000000000000000000000000000000 dir/file2
1025 100644 blob 3000000000000000000000000000000000000000 dir/file3
1026 100644 blob 4000000000000000000000000000000000000000 dir/file4
1027 100644 blob 5000000000000000000000000000000000000000 dir/file5
1029 git update-index --index-info <badobjects
1032 test_expect_success 'writing this tree without --missing-ok' '
1033 test_must_fail git write-tree
1036 test_expect_success 'writing this tree with --missing-ok' '
1037 git write-tree --missing-ok
1041 ################################################################
1042 test_expect_success 'git read-tree followed by write-tree should be idempotent' '
1043 rm -f .git/index &&
1044 git read-tree $tree &&
1045 test -f .git/index &&
1046 newtree=$(git write-tree) &&
1047 test "$newtree" = "$tree"
1050 test_expect_success 'validate git diff-files output for a know cache/work tree state' '
1051 cat >expected <<\EOF &&
1052 :100644 100644 f87290f8eb2cbbea7857214459a0739927eab154 0000000000000000000000000000000000000000 M path0
1053 :120000 120000 15a98433ae33114b085f3eb3bb03b832b3180a01 0000000000000000000000000000000000000000 M path0sym
1054 :100644 100644 3feff949ed00a62d9f7af97c15cd8a30595e7ac7 0000000000000000000000000000000000000000 M path2/file2
1055 :120000 120000 d8ce161addc5173867a3c3c730924388daedbc38 0000000000000000000000000000000000000000 M path2/file2sym
1056 :100644 100644 0aa34cae68d0878578ad119c86ca2b5ed5b28376 0000000000000000000000000000000000000000 M path3/file3
1057 :120000 120000 8599103969b43aff7e430efea79ca4636466794f 0000000000000000000000000000000000000000 M path3/file3sym
1058 :100644 100644 00fb5908cb97c2564a9783c0c64087333b3b464f 0000000000000000000000000000000000000000 M path3/subp3/file3
1059 :120000 120000 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c 0000000000000000000000000000000000000000 M path3/subp3/file3sym
1061 git diff-files >current &&
1062 test_cmp current expected
1065 test_expect_success 'git update-index --refresh should succeed' '
1066 git update-index --refresh
1069 test_expect_success 'no diff after checkout and git update-index --refresh' '
1070 git diff-files >current &&
1071 cmp -s current /dev/null
1074 ################################################################
1075 P=087704a96baf1c2d1c869a8b084481e121c88b5b
1077 TESTLIB_TEST_NO_SUBSHELL=1
1078 test_expect_success 'git commit-tree records the correct tree in a commit' '
1079 commit0=$(echo NO | git commit-tree $P) &&
1080 tree=$(git show --pretty=raw $commit0 |
1081 sed -n -e "s/^tree //p" -e "/^author /q") &&
1082 test "z$tree" = "z$P"
1084 sane_unset TESTLIB_TEST_NO_SUBSHELL
1086 test_expect_success 'git commit-tree records the correct parent in a commit' '
1087 commit1=$(echo NO | git commit-tree $P -p $commit0) &&
1088 parent=$(git show --pretty=raw $commit1 |
1089 sed -n -e "s/^parent //p" -e "/^author /q") &&
1090 test "z$commit0" = "z$parent"
1093 test_expect_success 'git commit-tree omits duplicated parent in a commit' '
1094 commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
1095 parent=$(git show --pretty=raw $commit2 |
1096 sed -n -e "s/^parent //p" -e "/^author /q" |
1097 sort -u) &&
1098 test "z$commit0" = "z$parent" &&
1099 numparent=$(git show --pretty=raw $commit2 |
1100 sed -n -e "s/^parent //p" -e "/^author /q" |
1101 wc -l) &&
1102 test $numparent = 1
1105 test_expect_success 'update-index D/F conflict' '
1106 mv path0 tmp &&
1107 mv path2 path0 &&
1108 mv tmp path2 &&
1109 git update-index --add --replace path2 path0/file2 &&
1110 numpath0=$(git ls-files path0 | wc -l) &&
1111 test $numpath0 = 1
1114 test_expect_success 'very long name in the index handled sanely' '
1116 a=a && # 1
1117 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
1118 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
1119 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
1120 a=${a}q &&
1122 >path4 &&
1123 git update-index --add path4 &&
1125 git ls-files -s path4 |
1126 sed -e "s/ .*/ /" |
1127 tr -d "\012"
1128 echo "$a"
1129 ) | git update-index --index-info &&
1130 len=$(git ls-files "a*" | wc -c) &&
1131 test $len = 4098
1134 test_done