tg.sh: handle help -h
[topgit/pro.git] / t / t0002-testlib-basic.sh
blob6b614bdfc1a7c454ee060c3fd226bfe32fd5ec30
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" --no-tap-only "$@" >out 2>err
91 else
92 ! ./"$name.sh" --no-tap-only "$@" >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: 2 - failing test #2
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: 2 - failing test
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: 5 - failing test
264 > # false
266 > not ok 6 - failing test
267 > # failed: mixed-results2.sh: 6 - failing test
269 > # false
271 > not ok 7 - failing test
272 > # failed: mixed-results2.sh: 7 - failing test
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: 3 - failing test
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: 3 - failing test
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' --no-quiet <<-\\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' --no-quiet <<-\\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 > ok 1 - passing test #1
471 > ok 2 - passing test #2
472 > ok 5 - passing test #5
473 > ok 6 - passing test #6
474 > # run passed all 6 test(s)
475 > 1..6
479 test_expect_success '--run with a left open range' "
480 run_sub_test_lib_test run-left-open-range \
481 '--run with a left open range' --run='-3' --no-quiet <<-\\EOF &&
482 test_plan 6
483 for i in 1 2 3 4 5 6
485 test_expect_success \"passing test #\$i\" 'true'
486 done
487 test_done
489 check_sub_test_lib_test run-left-open-range <<-\\EOF
490 > 1..6
491 > ok 1 - passing test #1
492 > ok 2 - passing test #2
493 > ok 3 - passing test #3
494 > ok 4 # skip passing test #4 (--run)
495 > ok 5 # skip passing test #5 (--run)
496 > ok 6 # skip passing test #6 (--run)
497 > # run passed all 6 test(s)
501 test_expect_success '--run with a right open range' "
502 run_sub_test_lib_test run-right-open-range \
503 '--run with a right open range' --run='4-' --no-quiet <<-\\EOF &&
504 test_plan 6
505 for i in 1 2 3 4 5 6
507 test_expect_success \"passing test #\$i\" 'true'
508 done
509 test_done
511 check_sub_test_lib_test run-right-open-range <<-\\EOF
512 > 1..6
513 > ok 1 # skip passing test #1 (--run)
514 > ok 2 # skip passing test #2 (--run)
515 > ok 3 # skip passing test #3 (--run)
516 > ok 4 - passing test #4
517 > ok 5 - passing test #5
518 > ok 6 - passing test #6
519 > # run passed all 6 test(s)
523 test_expect_success '--run with basic negation' "
524 run_sub_test_lib_test run-basic-neg \
525 '--run with basic negation' --run='"'!3'"' <<-\\EOF &&
526 test_plan 6
527 for i in 1 2 3 4 5 6
529 test_expect_success \"passing test #\$i\" 'true'
530 done
531 test_done
533 check_sub_test_lib_test run-basic-neg <<-\\EOF
534 > ok 1 - passing test #1
535 > ok 2 - passing test #2
536 > ok 4 - passing test #4
537 > ok 5 - passing test #5
538 > ok 6 - passing test #6
539 > # run passed all 6 test(s)
540 > 1..6
544 test_expect_success '--run with two negations' "
545 run_sub_test_lib_test run-two-neg \
546 '--run with two negations' --run='"'!3 !6'"' <<-\\EOF &&
547 test_plan 6
548 for i in 1 2 3 4 5 6
550 test_expect_success \"passing test #\$i\" 'true'
551 done
552 test_done
554 check_sub_test_lib_test run-two-neg <<-\\EOF
555 > ok 1 - passing test #1
556 > ok 2 - passing test #2
557 > ok 4 - passing test #4
558 > ok 5 - passing test #5
559 > # run passed all 6 test(s)
560 > 1..6
564 test_expect_success '--run a range and negation' "
565 run_sub_test_lib_test run-range-and-neg \
566 '--run a range and negation' --run='"'-4 !2'"' --no-quiet <<-\\EOF &&
567 test_plan 6
568 for i in 1 2 3 4 5 6
570 test_expect_success \"passing test #\$i\" 'true'
571 done
572 test_done
574 check_sub_test_lib_test run-range-and-neg <<-\\EOF
575 > 1..6
576 > ok 1 - passing test #1
577 > ok 2 # skip passing test #2 (--run)
578 > ok 3 - passing test #3
579 > ok 4 - passing test #4
580 > ok 5 # skip passing test #5 (--run)
581 > ok 6 # skip passing test #6 (--run)
582 > # run passed all 6 test(s)
586 test_expect_success '--run range negation' "
587 run_sub_test_lib_test run-range-neg \
588 '--run range negation' --run='"'!1-3'"' <<-\\EOF &&
589 test_plan 6
590 for i in 1 2 3 4 5 6
592 test_expect_success \"passing test #\$i\" 'true'
593 done
594 test_done
596 check_sub_test_lib_test run-range-neg <<-\\EOF
597 > ok 4 - passing test #4
598 > ok 5 - passing test #5
599 > ok 6 - passing test #6
600 > # run passed all 6 test(s)
601 > 1..6
605 test_expect_success '--run include, exclude and include' "
606 run_sub_test_lib_test run-inc-neg-inc \
607 '--run include, exclude and include' \
608 --run='"'1-5 !1-3 2'"' <<-\\EOF &&
609 test_plan 6
610 for i in 1 2 3 4 5 6
612 test_expect_success \"passing test #\$i\" 'true'
613 done
614 test_done
616 check_sub_test_lib_test run-inc-neg-inc <<-\\EOF
617 > ok 2 - passing test #2
618 > ok 4 - passing test #4
619 > ok 5 - passing test #5
620 > # run passed all 6 test(s)
621 > 1..6
625 test_expect_success '--run include, exclude and include, comma separated' "
626 run_sub_test_lib_test run-inc-neg-inc-comma \
627 '--run include, exclude and include, comma separated' \
628 --run=1-5,\!1-3,2 --no-quiet <<-\\EOF &&
629 test_plan 6
630 for i in 1 2 3 4 5 6
632 test_expect_success \"passing test #\$i\" 'true'
633 done
634 test_done
636 check_sub_test_lib_test run-inc-neg-inc-comma <<-\\EOF
637 > 1..6
638 > ok 1 # skip passing test #1 (--run)
639 > ok 2 - passing test #2
640 > ok 3 # skip passing test #3 (--run)
641 > ok 4 - passing test #4
642 > ok 5 - passing test #5
643 > ok 6 # skip passing test #6 (--run)
644 > # run passed all 6 test(s)
648 test_expect_success '--run exclude and include' "
649 run_sub_test_lib_test run-neg-inc \
650 '--run exclude and include' \
651 --run='"'!3- 5'"' <<-\\EOF &&
652 test_plan 6
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-neg-inc <<-\\EOF
660 > ok 1 - passing test #1
661 > ok 2 - passing test #2
662 > ok 5 - passing test #5
663 > # run passed all 6 test(s)
664 > 1..6
668 test_expect_success '--run empty selectors' "
669 run_sub_test_lib_test run-empty-sel \
670 '--run empty selectors' \
671 --run='1,,3,,,5' --no-quiet <<-\\EOF &&
672 test_plan 6
673 for i in 1 2 3 4 5 6
675 test_expect_success \"passing test #\$i\" 'true'
676 done
677 test_done
679 check_sub_test_lib_test run-empty-sel <<-\\EOF
680 > 1..6
681 > ok 1 - passing test #1
682 > ok 2 # skip passing test #2 (--run)
683 > ok 3 - passing test #3
684 > ok 4 # skip passing test #4 (--run)
685 > ok 5 - passing test #5
686 > ok 6 # skip passing test #6 (--run)
687 > # run passed all 6 test(s)
691 test_expect_success '--run invalid range start' "
692 run_sub_test_lib_test_err run-inv-range-start \
693 '--run invalid range start' \
694 --run='a-5' <<-\\EOF &&
695 test_expect_success \"passing test #1\" 'true'
696 test_done
698 check_sub_test_lib_test_err run-inv-range-start \
699 <<-\\EOF_OUT 3<<-\\EOF_ERR
700 > FATAL: Unexpected exit with code 1
701 EOF_OUT
702 > error: --run: invalid non-numeric in range start: 'a-5'
703 EOF_ERR
706 test_expect_success '--run invalid range end' "
707 run_sub_test_lib_test_err run-inv-range-end \
708 '--run invalid range end' \
709 --run='1-z' <<-\\EOF &&
710 test_expect_success \"passing test #1\" 'true'
711 test_done
713 check_sub_test_lib_test_err run-inv-range-end \
714 <<-\\EOF_OUT 3<<-\\EOF_ERR
715 > FATAL: Unexpected exit with code 1
716 EOF_OUT
717 > error: --run: invalid non-numeric in range end: '1-z'
718 EOF_ERR
721 test_expect_success '--run invalid selector' "
722 run_sub_test_lib_test_err run-inv-selector \
723 '--run invalid selector' \
724 --run='1?' <<-\\EOF &&
725 test_expect_success \"passing test #1\" 'true'
726 test_done
728 check_sub_test_lib_test_err run-inv-selector \
729 <<-\\EOF_OUT 3<<-\\EOF_ERR
730 > FATAL: Unexpected exit with code 1
731 EOF_OUT
732 > error: --run: invalid non-numeric in test selector: '1?'
733 EOF_ERR
737 test_set_prereq HAVEIT
738 haveit=no
739 echo "$haveit" >haveit
740 test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
741 test_have_prereq HAVEIT &&
742 haveit=yes &&
743 echo "$haveit" >haveit
745 donthaveit=yes
746 echo "$donthaveit" >donthaveit
747 test_expect_success DONTHAVEIT 'unmet prerequisite causes test to be skipped' '
748 donthaveit=no &&
749 echo "$donthaveit" >donthaveit
751 haveitf="$(cat haveit)"
752 donthaveitf="$(cat donthaveit)"
753 if test $haveit$donthaveit != noyes || test $haveitf$donthaveitf != yesyes
754 then
755 say "bug in test framework: prerequisite tags do not work reliably"
756 exit 1
759 # Stop with the fussing with files
760 TESTLIB_TEST_NO_SUBSHELL=1
762 test_set_prereq HAVETHIS
763 haveit=no
764 test_expect_success HAVETHIS,HAVEIT 'test runs if prerequisites are satisfied' '
765 test_have_prereq HAVEIT &&
766 test_have_prereq HAVETHIS &&
767 haveit=yes
769 donthaveit=yes
770 test_expect_success HAVEIT,DONTHAVEIT 'unmet prerequisites causes test to be skipped' '
771 donthaveit=no
773 donthaveiteither=yes
774 test_expect_success DONTHAVEIT,HAVEIT 'unmet prerequisites causes test to be skipped' '
775 donthaveiteither=no
777 if test $haveit$donthaveit$donthaveiteither != yesyesyes
778 then
779 say "bug in test framework: multiple prerequisite tags do not work reliably"
780 exit 1
783 test_lazy_prereq LAZY_TRUE true
784 havetrue=no
785 test_expect_success LAZY_TRUE 'test runs if lazy prereq is satisfied' '
786 havetrue=yes
788 donthavetrue=yes
789 test_expect_success !LAZY_TRUE 'missing lazy prereqs skip tests' '
790 donthavetrue=no
793 if test "$havetrue$donthavetrue" != yesyes
794 then
795 say 'bug in test framework: lazy prerequisites do not work'
796 exit 1
799 test_lazy_prereq LAZY_FALSE false
800 nothavefalse=no
801 test_expect_success !LAZY_FALSE 'negative lazy prereqs checked' '
802 nothavefalse=yes
804 havefalse=yes
805 test_expect_success LAZY_FALSE 'missing negative lazy prereqs will skip' '
806 havefalse=no
809 if test "$nothavefalse$havefalse" != yesyes
810 then
811 say 'bug in test framework: negative lazy prerequisites do not work'
812 exit 1
815 # Mostly done with the no subshell tests
816 sane_unset TESTLIB_TEST_NO_SUBSHELL
818 clean=no
819 test_expect_success 'tests clean up after themselves' '
820 test_when_finished clean=yes
823 if test $clean != yes
824 then
825 say "bug in test framework: basic cleanup command does not work reliably"
826 exit 1
829 test_expect_success 'tests clean up even on failures' "
830 test_must_fail run_sub_test_lib_test \
831 failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
832 test_plan 2
833 test_expect_success 'tests clean up even after a failure' '
834 touch clean-after-failure &&
835 test_when_finished rm clean-after-failure &&
836 (exit 1)
838 test_expect_success 'failure to clean up causes the test to fail' '
839 test_when_finished eval \"(exit 2)\"
841 test_done
843 check_sub_test_lib_test failing-cleanup <<-\\EOF
844 > 1..2
845 > not ok 1 - tests clean up even after a failure
846 > # failed: failing-cleanup.sh: 1 - tests clean up even after a failure
848 > # touch clean-after-failure &&
849 > # test_when_finished rm clean-after-failure &&
850 > # (exit 1)
852 > not ok 2 - failure to clean up causes the test to fail
853 > # failed: failing-cleanup.sh: 2 - failure to clean up causes the test to fail
855 > # test_when_finished eval \"(exit 2)\"
857 > # failing failed 2 among 2 test(s)
861 ################################################################
862 # Basics of the basics
864 test_v_git_mt ZERO_OID null
866 test_asv_cache '
867 # These are some common invalid and partial object IDs used in tests.
868 001 sha1 0000000000000000000000000000000000000001
869 001 sha256 0000000000000000000000000000000000000000000000000000000000000001
870 002 sha1 0000000000000000000000000000000000000002
871 002 sha256 0000000000000000000000000000000000000000000000000000000000000002
872 003 sha1 0000000000000000000000000000000000000003
873 003 sha256 0000000000000000000000000000000000000000000000000000000000000003
874 004 sha1 0000000000000000000000000000000000000004
875 004 sha256 0000000000000000000000000000000000000000000000000000000000000004
876 005 sha1 0000000000000000000000000000000000000005
877 005 sha256 0000000000000000000000000000000000000000000000000000000000000005
880 test_asv_cache '
881 path0f sha1 f87290f8eb2cbbea7857214459a0739927eab154
882 path0f sha256 638106af7c38be056f3212cbd7ac65bc1bac74f420ca5a436ff006a9d025d17d
884 path0s sha1 15a98433ae33114b085f3eb3bb03b832b3180a01
885 path0s sha256 3a24cc53cf68edddac490bbf94a418a52932130541361f685df685e41dd6c363
887 path2f sha1 3feff949ed00a62d9f7af97c15cd8a30595e7ac7
888 path2f sha256 2a7f36571c6fdbaf0e3f62751a0b25a3f4c54d2d1137b3f4af9cb794bb498e5f
890 path2s sha1 d8ce161addc5173867a3c3c730924388daedbc38
891 path2s sha256 18fd611b787c2e938ddcc248fabe4d66a150f9364763e9ec133dd01d5bb7c65a
893 path2d sha1 58a09c23e2ca152193f2786e06986b7b6712bdbe
894 path2d sha256 00e4b32b96e7e3d65d79112dcbea53238a22715f896933a62b811377e2650c17
896 path3f sha1 0aa34cae68d0878578ad119c86ca2b5ed5b28376
897 path3f sha256 09f58616b951bd571b8cb9dc76d372fbb09ab99db2393f5ab3189d26c45099ad
899 path3s sha1 8599103969b43aff7e430efea79ca4636466794f
900 path3s sha256 fce1aed087c053306f3f74c32c1a838c662bbc4551a7ac2420f5d6eb061374d0
902 path3d sha1 21ae8269cacbe57ae09138dcc3a2887f904d02b3
903 path3d sha256 9b60497be959cb830bf3f0dc82bcc9ad9e925a24e480837ade46b2295e47efe1
905 subp3f sha1 00fb5908cb97c2564a9783c0c64087333b3b464f
906 subp3f sha256 a1a9e16998c988453f18313d10375ee1d0ddefe757e710dcae0d66aa1e0c58b3
908 subp3s sha1 6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c
909 subp3s sha256 81759d9f5e93c6546ecfcadb560c1ff057314b09f93fe8ec06e2d8610d34ef10
911 subp3d sha1 3c5e5399f3a333eddecce7a9b9465b63f65f51e2
912 subp3d sha256 76b4ef482d4fa1c754390344cf3851c7f883b27cf9bc999c6547928c46aeafb7
914 root sha1 087704a96baf1c2d1c869a8b084481e121c88b5b
915 root sha256 9481b52abab1b2ffeedbf9de63ce422b929f179c1b98ff7bee5f8f1bc0710751
917 simpletree sha1 7bb943559a305bdd6bdee2cef6e5df2413c3d30a
918 simpletree sha256 1710c07a6c86f9a3c7376364df04c47ee39e5a5e221fcdd84b743bc9bb7e2bc5
921 # updating a new file without --add should fail.
922 test_expect_success 'git update-index without --add should fail adding' '
923 test_must_fail git update-index should-be-empty
926 # and with --add it should succeed, even if it is empty (it used to fail).
927 test_expect_success 'git update-index with --add should succeed' '
928 git update-index --add should-be-empty
931 TESTLIB_TEST_NO_SUBSHELL=1
932 test_expect_success 'writing tree out with git write-tree' '
933 tree=$(git write-tree)
935 sane_unset TESTLIB_TEST_NO_SUBSHELL
937 # we know the shape and contents of the tree and know the object ID for it.
938 test_expect_success 'validate object ID of a known tree' '
939 test "$tree" = "$(test_asv simpletree)"
942 # Removing paths.
943 test_expect_success 'git update-index without --remove should fail removing' '
944 rm -f should-be-empty full-of-directories &&
945 test_must_fail git update-index should-be-empty
948 test_expect_success 'git update-index with --remove should be able to remove' '
949 git update-index --remove should-be-empty
952 # Empty tree can be written with recent write-tree.
953 TESTLIB_TEST_NO_SUBSHELL=1
954 test_tolerate_failure 'git write-tree should be able to write an empty tree' '
955 tree=$(git write-tree)
957 sane_unset TESTLIB_TEST_NO_SUBSHELL
958 test_v_git_mt EMPTY_TREE tree
960 if test -n "$tree"
961 then
962 test_result='test_expect_success'
963 else
964 test_result='test_tolerate_failure'
966 $test_result 'validate object ID of a known tree' '
967 test "$tree" = $EMPTY_TREE
970 # Various types of objects
972 test_expect_success 'adding various types of objects with git update-index --add' '
973 mkdir path2 path3 path3/subp3 &&
974 paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
976 for p in $paths
978 echo "hello $p" >$p || exit 1
979 test_ln_s_add "hello $p" ${p}sym || exit 1
980 done
981 ) &&
982 find path* ! -type d -print | xargs git update-index --add
985 # Show them and see that matches what we expect.
986 test_expect_success 'showing stage with git ls-files --stage' '
987 git ls-files --stage >current
990 test_expect_success 'validate git ls-files output for a known tree' '
991 cat >expected <<-EOF &&
992 100644 $(test_asv path0f) 0 path0
993 120000 $(test_asv path0s) 0 path0sym
994 100644 $(test_asv path2f) 0 path2/file2
995 120000 $(test_asv path2s) 0 path2/file2sym
996 100644 $(test_asv path3f) 0 path3/file3
997 120000 $(test_asv path3s) 0 path3/file3sym
998 100644 $(test_asv subp3f) 0 path3/subp3/file3
999 120000 $(test_asv subp3s) 0 path3/subp3/file3sym
1001 test_cmp expected current
1004 TESTLIB_TEST_NO_SUBSHELL=1
1005 test_expect_success 'writing tree out with git write-tree' '
1006 tree=$(git write-tree)
1008 sane_unset TESTLIB_TEST_NO_SUBSHELL
1010 test_expect_success 'validate object ID for a known tree' '
1011 test "$tree" = "$(test_asv root)"
1014 test_expect_success 'showing tree with git ls-tree' '
1015 git ls-tree $tree >current
1018 test_expect_success 'git ls-tree output for a known tree' '
1019 cat >expected <<-EOF &&
1020 100644 blob $(test_asv path0f) path0
1021 120000 blob $(test_asv path0s) path0sym
1022 040000 tree $(test_asv path2d) path2
1023 040000 tree $(test_asv path3d) path3
1025 test_cmp expected current
1028 # This changed in ls-tree pathspec change -- recursive does
1029 # not show tree nodes anymore.
1030 test_expect_success 'showing tree with git ls-tree -r' '
1031 git ls-tree -r $tree >current
1034 test_expect_success 'git ls-tree -r output for a known tree' '
1035 cat >expected <<-EOF &&
1036 100644 blob $(test_asv path0f) path0
1037 120000 blob $(test_asv path0s) path0sym
1038 100644 blob $(test_asv path2f) path2/file2
1039 120000 blob $(test_asv path2s) path2/file2sym
1040 100644 blob $(test_asv path3f) path3/file3
1041 120000 blob $(test_asv path3s) path3/file3sym
1042 100644 blob $(test_asv subp3f) path3/subp3/file3
1043 120000 blob $(test_asv subp3s) path3/subp3/file3sym
1045 test_cmp expected current
1048 # But with -r -t we can have both.
1049 test_expect_success 'showing tree with git ls-tree -r -t' '
1050 git ls-tree -r -t $tree >current
1053 test_expect_success 'git ls-tree -r output for a known tree' '
1054 cat >expected <<-EOF &&
1055 100644 blob $(test_asv path0f) path0
1056 120000 blob $(test_asv path0s) path0sym
1057 040000 tree $(test_asv path2d) path2
1058 100644 blob $(test_asv path2f) path2/file2
1059 120000 blob $(test_asv path2s) path2/file2sym
1060 040000 tree $(test_asv path3d) path3
1061 100644 blob $(test_asv path3f) path3/file3
1062 120000 blob $(test_asv path3s) path3/file3sym
1063 040000 tree $(test_asv subp3d) path3/subp3
1064 100644 blob $(test_asv subp3f) path3/subp3/file3
1065 120000 blob $(test_asv subp3s) path3/subp3/file3sym
1067 test_cmp expected current
1070 TESTLIB_TEST_NO_SUBSHELL=1
1071 test_expect_success 'writing partial tree out with git write-tree --prefix' '
1072 ptree=$(git write-tree --prefix=path3)
1074 sane_unset TESTLIB_TEST_NO_SUBSHELL
1076 test_expect_success 'validate object ID for a known tree' '
1077 test "$ptree" = $(test_asv path3d)
1080 TESTLIB_TEST_NO_SUBSHELL=1
1081 test_expect_success 'writing partial tree out with git write-tree --prefix' '
1082 ptree=$(git write-tree --prefix=path3/subp3)
1084 sane_unset TESTLIB_TEST_NO_SUBSHELL
1086 test_expect_success 'validate object ID for a known tree' '
1087 test "$ptree" = $(test_asv subp3d)
1090 test_expect_success 'put invalid objects into the index' '
1091 rm -f .git/index &&
1092 suffix=$(echo $ZERO_OID | sed -e "s/^.//") &&
1093 cat >badobjects <<-EOF &&
1094 100644 blob $(test_asv 001) dir/file1
1095 100644 blob $(test_asv 002) dir/file2
1096 100644 blob $(test_asv 003) dir/file3
1097 100644 blob $(test_asv 004) dir/file4
1098 100644 blob $(test_asv 005) dir/file5
1100 git update-index --index-info <badobjects
1103 test_expect_success 'writing this tree without --missing-ok' '
1104 test_must_fail git write-tree
1107 test_expect_success 'writing this tree with --missing-ok' '
1108 git write-tree --missing-ok
1112 ################################################################
1113 test_expect_success 'git read-tree followed by write-tree should be idempotent' '
1114 rm -f .git/index &&
1115 git read-tree $tree &&
1116 test -f .git/index &&
1117 newtree=$(git write-tree) &&
1118 test "$newtree" = "$tree"
1121 test_expect_success 'validate git diff-files output for a known cache/work tree state' '
1122 cat >expected <<EOF &&
1123 :100644 100644 $(test_asv path0f) $ZERO_OID M path0
1124 :120000 120000 $(test_asv path0s) $ZERO_OID M path0sym
1125 :100644 100644 $(test_asv path2f) $ZERO_OID M path2/file2
1126 :120000 120000 $(test_asv path2s) $ZERO_OID M path2/file2sym
1127 :100644 100644 $(test_asv path3f) $ZERO_OID M path3/file3
1128 :120000 120000 $(test_asv path3s) $ZERO_OID M path3/file3sym
1129 :100644 100644 $(test_asv subp3f) $ZERO_OID M path3/subp3/file3
1130 :120000 120000 $(test_asv subp3s) $ZERO_OID M path3/subp3/file3sym
1132 git diff-files >current &&
1133 test_cmp current expected
1136 test_expect_success 'git update-index --refresh should succeed' '
1137 git update-index --refresh
1140 test_expect_success 'no diff after checkout and git update-index --refresh' '
1141 git diff-files >current &&
1142 cmp -s current /dev/null
1145 ################################################################
1146 P=$(test_asv root)
1148 TESTLIB_TEST_NO_SUBSHELL=1
1149 test_expect_success 'git commit-tree records the correct tree in a commit' '
1150 commit0=$(echo NO | git commit-tree $P) &&
1151 tree=$(git show --pretty=raw $commit0 |
1152 sed -n -e "s/^tree //p" -e "/^author /q") &&
1153 test "z$tree" = "z$P"
1155 sane_unset TESTLIB_TEST_NO_SUBSHELL
1157 test_expect_success 'git commit-tree records the correct parent in a commit' '
1158 commit1=$(echo NO | git commit-tree $P -p $commit0) &&
1159 parent=$(git show --pretty=raw $commit1 |
1160 sed -n -e "s/^parent //p" -e "/^author /q") &&
1161 test "z$commit0" = "z$parent"
1164 test_expect_success 'git commit-tree omits duplicated parent in a commit' '
1165 commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
1166 parent=$(git show --pretty=raw $commit2 |
1167 sed -n -e "s/^parent //p" -e "/^author /q" |
1168 sort -u) &&
1169 test "z$commit0" = "z$parent" &&
1170 numparent=$(git show --pretty=raw $commit2 |
1171 sed -n -e "s/^parent //p" -e "/^author /q" |
1172 wc -l) &&
1173 test $numparent = 1
1176 test_expect_success 'update-index D/F conflict' '
1177 mv path0 tmp &&
1178 mv path2 path0 &&
1179 mv tmp path2 &&
1180 git update-index --add --replace path2 path0/file2 &&
1181 numpath0=$(git ls-files path0 | wc -l) &&
1182 test $numpath0 = 1
1185 test_expect_success 'very long name in the index handled sanely' '
1187 a=a && # 1
1188 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
1189 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
1190 a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
1191 a=${a}q &&
1193 >path4 &&
1194 git update-index --add path4 &&
1196 git ls-files -s path4 |
1197 sed -e "s/ .*/ /" |
1198 tr -d "\012" &&
1199 echo "$a"
1200 ) | git update-index --index-info &&
1201 len=$(git ls-files "a*" | wc -c) &&
1202 test $len = 4098
1205 # git_add_config "some.var=value"
1206 # every ' in value must be replaced with the 4-character sequence '\'' before
1207 # calling this function or Git will barf. Will not be effective unless running
1208 # Git version 1.7.3 or later.
1209 git_add_config() {
1210 GIT_CONFIG_PARAMETERS="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }'$1'" &&
1211 export GIT_CONFIG_PARAMETERS
1214 git_sane_get_config() {
1215 git config --get "$1" || :
1218 test_expect_success 'GIT_CONFIG_PARAMETERS works as expected' '
1219 val1="$(git_sane_get_config testgcp.val1)" &&
1220 val2="$(git_sane_get_config testgcp.val2)" &&
1221 test -z "$val1" && test -z "$val2" &&
1222 git_add_config "testgcp.val1=value1" &&
1223 val1="$(git_sane_get_config testgcp.val1)" &&
1224 val2="$(git_sane_get_config testgcp.val2)" &&
1225 test x"$val1" = x"value1" && test -z "$val2" &&
1226 git_add_config "testgcp.val2=value too" &&
1227 val1="$(git_sane_get_config testgcp.val1)" &&
1228 val2="$(git_sane_get_config testgcp.val2)" &&
1229 test x"$val1" = x"value1" && test x"$val2" = x"value too"
1232 test_done