test-lib tests: split up "write and run" into two functions

Refactor the function to write and run tests of the test-lib.sh output into two functions. When this was added back in 565b6fa87bb (tests: refactor mechanics of testing in a sub test-lib, 2012-12-16) there was no reason to do this, but since we started supporting test arguments in 517cd55fd51 (test-lib: self-test that --verbose works, 2013-06-23) we've started to write out duplicate tests simply to test different arguments, now we'll be able to re-use them. This change doesn't consolidate any of those tests yet, it just makes it possible to do so. All the changes in t0000-basic.sh are a simple search-replacement. Since the _run_sub_test_lib_test_common() function doesn't handle running the test anymore we can do away with the sub-shell, which was used to scope an "unset" and "export" shell variables. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Sep 22, 2021 at 13:19 UTC 9f0a45208dbc488da790d0f2ef1491eb2aa858c2
2 files changed +67 -49
t/lib-subtest.sh
+30 -12
@@ -1,3 +1,19 @@
1 +write_sub_test_lib_test () {
2 + name="$1" descr="$2" # stdin is the body of the test code
3 + mkdir "$name" &&
4 + write_script "$name/$name.sh" "$TEST_SHELL_PATH" <<-EOF &&
5 + test_description='$descr (run in sub test-lib)
6 +
7 + This is run in a sub test-lib so that we do not get incorrect
8 + passing metrics
9 + '
10 +
11 + # Point to the t/test-lib.sh, which isn't in ../ as usual
12 + . "\$TEST_DIRECTORY"/test-lib.sh
13 + EOF
14 + cat >>"$name/$name.sh"
15 +}
16 +
17 _run_sub_test_lib_test_common () {
18 neg="$1" name="$2" descr="$3" # stdin is the body of the test code
19 shift 3
@@ -18,25 +34,15 @@ _run_sub_test_lib_test_common () {
34 esac
35 done
36
21 - mkdir "$name" &&
37 (
38 + cd "$name" &&
39 +
40 # Pretend we're not running under a test harness, whether we
41 # are or not. The test-lib output depends on the setting of
42 # this variable, so we need a stable setting under which to run
43 # the sub-test.
44 sane_unset HARNESS_ACTIVE &&
28 - cd "$name" &&
29 - write_script "$name.sh" "$TEST_SHELL_PATH" <<-EOF &&
30 - test_description='$descr (run in sub test-lib)
31 -
32 - This is run in a sub test-lib so that we do not get incorrect
33 - passing metrics
34 - '
45
36 - # Point to the t/test-lib.sh, which isn't in ../ as usual
37 - . "\$TEST_DIRECTORY"/test-lib.sh
38 - EOF
39 - cat >>"$name.sh" &&
46 export TEST_DIRECTORY &&
47 # The child test re-sources GIT-BUILD-OPTIONS and may thus
48 # override the test output directory. We thus pass it as an
@@ -55,6 +61,18 @@ _run_sub_test_lib_test_common () {
61 )
62 }
63
64 +write_and_run_sub_test_lib_test () {
65 + name="$1" descr="$2" # stdin is the body of the test code
66 + write_sub_test_lib_test "$@" || return 1
67 + _run_sub_test_lib_test_common '' "$@"
68 +}
69 +
70 +write_and_run_sub_test_lib_test_err () {
71 + name="$1" descr="$2" # stdin is the body of the test code
72 + write_sub_test_lib_test "$@" || return 1
73 + _run_sub_test_lib_test_common '!' "$@"
74 +}
75 +
76 run_sub_test_lib_test () {
77 _run_sub_test_lib_test_common '' "$@"
78 }
t/t0000-basic.sh
+37 -37
@@ -68,7 +68,7 @@ test_expect_success 'success is reported like this' '
68 '
69
70 test_expect_success 'pretend we have a fully passing test suite' '
71 - run_sub_test_lib_test full-pass "3 passing tests" <<-\EOF &&
71 + write_and_run_sub_test_lib_test full-pass "3 passing tests" <<-\EOF &&
72 for i in 1 2 3
73 do
74 test_expect_success "passing test #$i" "true"
@@ -85,7 +85,7 @@ test_expect_success 'pretend we have a fully passing test suite' '
85 '
86
87 test_expect_success 'pretend we have a partially passing test suite' '
88 - run_sub_test_lib_test_err \
88 + write_and_run_sub_test_lib_test_err \
89 partial-pass "2/3 tests passing" <<-\EOF &&
90 test_expect_success "passing test #1" "true"
91 test_expect_success "failing test #2" "false"
@@ -103,7 +103,7 @@ test_expect_success 'pretend we have a partially passing test suite' '
103 '
104
105 test_expect_success 'pretend we have a known breakage' '
106 - run_sub_test_lib_test failing-todo "A failing TODO test" <<-\EOF &&
106 + write_and_run_sub_test_lib_test failing-todo "A failing TODO test" <<-\EOF &&
107 test_expect_success "passing test" "true"
108 test_expect_failure "pretend we have a known breakage" "false"
109 test_done
@@ -118,7 +118,7 @@ test_expect_success 'pretend we have a known breakage' '
118 '
119
120 test_expect_success 'pretend we have fixed a known breakage' '
121 - run_sub_test_lib_test passing-todo "A passing TODO test" <<-\EOF &&
121 + write_and_run_sub_test_lib_test passing-todo "A passing TODO test" <<-\EOF &&
122 test_expect_failure "pretend we have fixed a known breakage" "true"
123 test_done
124 EOF
@@ -130,7 +130,7 @@ test_expect_success 'pretend we have fixed a known breakage' '
130 '
131
132 test_expect_success 'pretend we have fixed one of two known breakages (run in sub test-lib)' '
133 - run_sub_test_lib_test partially-passing-todos \
133 + write_and_run_sub_test_lib_test partially-passing-todos \
134 "2 TODO tests, one passing" <<-\EOF &&
135 test_expect_failure "pretend we have a known breakage" "false"
136 test_expect_success "pretend we have a passing test" "true"
@@ -149,7 +149,7 @@ test_expect_success 'pretend we have fixed one of two known breakages (run in su
149 '
150
151 test_expect_success 'pretend we have a pass, fail, and known breakage' '
152 - run_sub_test_lib_test_err \
152 + write_and_run_sub_test_lib_test_err \
153 mixed-results1 "mixed results #1" <<-\EOF &&
154 test_expect_success "passing test" "true"
155 test_expect_success "failing test" "false"
@@ -168,7 +168,7 @@ test_expect_success 'pretend we have a pass, fail, and known breakage' '
168 '
169
170 test_expect_success 'pretend we have a mix of all possible results' '
171 - run_sub_test_lib_test_err \
171 + write_and_run_sub_test_lib_test_err \
172 mixed-results2 "mixed results #2" <<-\EOF &&
173 test_expect_success "passing test" "true"
174 test_expect_success "passing test" "true"
@@ -204,7 +204,7 @@ test_expect_success 'pretend we have a mix of all possible results' '
204 '
205
206 test_expect_success 'test --verbose' '
207 - run_sub_test_lib_test_err \
207 + write_and_run_sub_test_lib_test_err \
208 t1234-verbose "test verbose" --verbose <<-\EOF &&
209 test_expect_success "passing test" true
210 test_expect_success "test with output" "echo foo"
@@ -231,7 +231,7 @@ test_expect_success 'test --verbose' '
231 '
232
233 test_expect_success 'test --verbose-only' '
234 - run_sub_test_lib_test_err \
234 + write_and_run_sub_test_lib_test_err \
235 t2345-verbose-only-2 "test verbose-only=2" \
236 --verbose-only=2 <<-\EOF &&
237 test_expect_success "passing test" true
@@ -255,7 +255,7 @@ test_expect_success 'test --verbose-only' '
255
256 test_expect_success 'GIT_SKIP_TESTS' '
257 (
258 - run_sub_test_lib_test git-skip-tests-basic \
258 + write_and_run_sub_test_lib_test git-skip-tests-basic \
259 "GIT_SKIP_TESTS" \
260 --skip="git.2" <<-\EOF &&
261 for i in 1 2 3
@@ -276,7 +276,7 @@ test_expect_success 'GIT_SKIP_TESTS' '
276
277 test_expect_success 'GIT_SKIP_TESTS several tests' '
278 (
279 - run_sub_test_lib_test git-skip-tests-several \
279 + write_and_run_sub_test_lib_test git-skip-tests-several \
280 "GIT_SKIP_TESTS several tests" \
281 --skip="git.2 git.5" <<-\EOF &&
282 for i in 1 2 3 4 5 6
@@ -300,7 +300,7 @@ test_expect_success 'GIT_SKIP_TESTS several tests' '
300
301 test_expect_success 'GIT_SKIP_TESTS sh pattern' '
302 (
303 - run_sub_test_lib_test git-skip-tests-sh-pattern \
303 + write_and_run_sub_test_lib_test git-skip-tests-sh-pattern \
304 "GIT_SKIP_TESTS sh pattern" \
305 --skip="git.[2-5]" <<-\EOF &&
306 for i in 1 2 3 4 5 6
@@ -324,7 +324,7 @@ test_expect_success 'GIT_SKIP_TESTS sh pattern' '
324
325 test_expect_success 'GIT_SKIP_TESTS entire suite' '
326 (
327 - run_sub_test_lib_test git-skip-tests-entire-suite \
327 + write_and_run_sub_test_lib_test git-skip-tests-entire-suite \
328 "GIT_SKIP_TESTS entire suite" \
329 --skip="git" <<-\EOF &&
330 for i in 1 2 3
@@ -341,7 +341,7 @@ test_expect_success 'GIT_SKIP_TESTS entire suite' '
341
342 test_expect_success 'GIT_SKIP_TESTS does not skip unmatched suite' '
343 (
344 - run_sub_test_lib_test git-skip-tests-unmatched-suite \
344 + write_and_run_sub_test_lib_test git-skip-tests-unmatched-suite \
345 "GIT_SKIP_TESTS does not skip unmatched suite" \
346 --skip="notgit" <<-\EOF &&
347 for i in 1 2 3
@@ -361,7 +361,7 @@ test_expect_success 'GIT_SKIP_TESTS does not skip unmatched suite' '
361 '
362
363 test_expect_success '--run basic' '
364 - run_sub_test_lib_test run-basic \
364 + write_and_run_sub_test_lib_test run-basic \
365 "--run basic" --run="1,3,5" <<-\EOF &&
366 for i in 1 2 3 4 5 6
367 do
@@ -382,7 +382,7 @@ test_expect_success '--run basic' '
382 '
383
384 test_expect_success '--run with a range' '
385 - run_sub_test_lib_test run-range \
385 + write_and_run_sub_test_lib_test run-range \
386 "--run with a range" --run="1-3" <<-\EOF &&
387 for i in 1 2 3 4 5 6
388 do
@@ -403,7 +403,7 @@ test_expect_success '--run with a range' '
403 '
404
405 test_expect_success '--run with two ranges' '
406 - run_sub_test_lib_test run-two-ranges \
406 + write_and_run_sub_test_lib_test run-two-ranges \
407 "--run with two ranges" --run="1-2,5-6" <<-\EOF &&
408 for i in 1 2 3 4 5 6
409 do
@@ -424,7 +424,7 @@ test_expect_success '--run with two ranges' '
424 '
425
426 test_expect_success '--run with a left open range' '
427 - run_sub_test_lib_test run-left-open-range \
427 + write_and_run_sub_test_lib_test run-left-open-range \
428 "--run with a left open range" --run="-3" <<-\EOF &&
429 for i in 1 2 3 4 5 6
430 do
@@ -445,7 +445,7 @@ test_expect_success '--run with a left open range' '
445 '
446
447 test_expect_success '--run with a right open range' '
448 - run_sub_test_lib_test run-right-open-range \
448 + write_and_run_sub_test_lib_test run-right-open-range \
449 "--run with a right open range" --run="4-" <<-\EOF &&
450 for i in 1 2 3 4 5 6
451 do
@@ -466,7 +466,7 @@ test_expect_success '--run with a right open range' '
466 '
467
468 test_expect_success '--run with basic negation' '
469 - run_sub_test_lib_test run-basic-neg \
469 + write_and_run_sub_test_lib_test run-basic-neg \
470 "--run with basic negation" --run="!3" <<-\EOF &&
471 for i in 1 2 3 4 5 6
472 do
@@ -487,7 +487,7 @@ test_expect_success '--run with basic negation' '
487 '
488
489 test_expect_success '--run with two negations' '
490 - run_sub_test_lib_test run-two-neg \
490 + write_and_run_sub_test_lib_test run-two-neg \
491 "--run with two negations" --run="!3,!6" <<-\EOF &&
492 for i in 1 2 3 4 5 6
493 do
@@ -508,7 +508,7 @@ test_expect_success '--run with two negations' '
508 '
509
510 test_expect_success '--run a range and negation' '
511 - run_sub_test_lib_test run-range-and-neg \
511 + write_and_run_sub_test_lib_test run-range-and-neg \
512 "--run a range and negation" --run="-4,!2" <<-\EOF &&
513 for i in 1 2 3 4 5 6
514 do
@@ -529,7 +529,7 @@ test_expect_success '--run a range and negation' '
529 '
530
531 test_expect_success '--run range negation' '
532 - run_sub_test_lib_test run-range-neg \
532 + write_and_run_sub_test_lib_test run-range-neg \
533 "--run range negation" --run="!1-3" <<-\EOF &&
534 for i in 1 2 3 4 5 6
535 do
@@ -550,7 +550,7 @@ test_expect_success '--run range negation' '
550 '
551
552 test_expect_success '--run include, exclude and include' '
553 - run_sub_test_lib_test run-inc-neg-inc \
553 + write_and_run_sub_test_lib_test run-inc-neg-inc \
554 "--run include, exclude and include" \
555 --run="1-5,!1-3,2" <<-\EOF &&
556 for i in 1 2 3 4 5 6
@@ -572,7 +572,7 @@ test_expect_success '--run include, exclude and include' '
572 '
573
574 test_expect_success '--run include, exclude and include, comma separated' '
575 - run_sub_test_lib_test run-inc-neg-inc-comma \
575 + write_and_run_sub_test_lib_test run-inc-neg-inc-comma \
576 "--run include, exclude and include, comma separated" \
577 --run=1-5,!1-3,2 <<-\EOF &&
578 for i in 1 2 3 4 5 6
@@ -594,7 +594,7 @@ test_expect_success '--run include, exclude and include, comma separated' '
594 '
595
596 test_expect_success '--run exclude and include' '
597 - run_sub_test_lib_test run-neg-inc \
597 + write_and_run_sub_test_lib_test run-neg-inc \
598 "--run exclude and include" \
599 --run="!3-,5" <<-\EOF &&
600 for i in 1 2 3 4 5 6
@@ -616,7 +616,7 @@ test_expect_success '--run exclude and include' '
616 '
617
618 test_expect_success '--run empty selectors' '
619 - run_sub_test_lib_test run-empty-sel \
619 + write_and_run_sub_test_lib_test run-empty-sel \
620 "--run empty selectors" \
621 --run="1,,3,,,5" <<-\EOF &&
622 for i in 1 2 3 4 5 6
@@ -638,7 +638,7 @@ test_expect_success '--run empty selectors' '
638 '
639
640 test_expect_success '--run substring selector' '
641 - run_sub_test_lib_test run-substring-selector \
641 + write_and_run_sub_test_lib_test run-substring-selector \
642 "--run empty selectors" \
643 --run="relevant" <<-\EOF &&
644 test_expect_success "relevant test" "true"
@@ -662,7 +662,7 @@ test_expect_success '--run substring selector' '
662 '
663
664 test_expect_success '--run keyword selection' '
665 - run_sub_test_lib_test_err run-inv-range-start \
665 + write_and_run_sub_test_lib_test_err run-inv-range-start \
666 "--run invalid range start" \
667 --run="a-5" <<-\EOF &&
668 test_expect_success "passing test #1" "true"
@@ -677,7 +677,7 @@ test_expect_success '--run keyword selection' '
677 '
678
679 test_expect_success '--run invalid range end' '
680 - run_sub_test_lib_test_err run-inv-range-end \
680 + write_and_run_sub_test_lib_test_err run-inv-range-end \
681 "--run invalid range end" \
682 --run="1-z" <<-\EOF &&
683 test_expect_success "passing test #1" "true"
@@ -692,7 +692,7 @@ test_expect_success '--run invalid range end' '
692 '
693
694 test_expect_success 'tests respect prerequisites' '
695 - run_sub_test_lib_test prereqs "tests respect prereqs" <<-\EOF &&
695 + write_and_run_sub_test_lib_test prereqs "tests respect prereqs" <<-\EOF &&
696
697 test_set_prereq HAVEIT
698 test_expect_success HAVEIT "prereq is satisfied" "true"
@@ -722,7 +722,7 @@ test_expect_success 'tests respect prerequisites' '
722 '
723
724 test_expect_success 'tests respect lazy prerequisites' '
725 - run_sub_test_lib_test lazy-prereqs "respect lazy prereqs" <<-\EOF &&
725 + write_and_run_sub_test_lib_test lazy-prereqs "respect lazy prereqs" <<-\EOF &&
726
727 test_lazy_prereq LAZY_TRUE true
728 test_expect_success LAZY_TRUE "lazy prereq is satisifed" "true"
@@ -746,7 +746,7 @@ test_expect_success 'tests respect lazy prerequisites' '
746 '
747
748 test_expect_success 'nested lazy prerequisites' '
749 - run_sub_test_lib_test nested-lazy "nested lazy prereqs" <<-\EOF &&
749 + write_and_run_sub_test_lib_test nested-lazy "nested lazy prereqs" <<-\EOF &&
750
751 test_lazy_prereq NESTED_INNER "
752 >inner &&
@@ -772,7 +772,7 @@ test_expect_success 'nested lazy prerequisites' '
772 '
773
774 test_expect_success 'lazy prereqs do not turn off tracing' '
775 - run_sub_test_lib_test lazy-prereq-and-tracing \
775 + write_and_run_sub_test_lib_test lazy-prereq-and-tracing \
776 "lazy prereqs and -x" -v -x <<-\EOF &&
777 test_lazy_prereq LAZY true
778
@@ -785,7 +785,7 @@ test_expect_success 'lazy prereqs do not turn off tracing' '
785 '
786
787 test_expect_success 'tests clean up after themselves' '
788 - run_sub_test_lib_test cleanup "test with cleanup" <<-\EOF &&
788 + write_and_run_sub_test_lib_test cleanup "test with cleanup" <<-\EOF &&
789 clean=no
790 test_expect_success "do cleanup" "
791 test_when_finished clean=yes
@@ -805,7 +805,7 @@ test_expect_success 'tests clean up after themselves' '
805 '
806
807 test_expect_success 'tests clean up even on failures' '
808 - run_sub_test_lib_test_err \
808 + write_and_run_sub_test_lib_test_err \
809 failing-cleanup "Failing tests with cleanup commands" <<-\EOF &&
810 test_expect_success "tests clean up even after a failure" "
811 touch clean-after-failure &&
@@ -834,7 +834,7 @@ test_expect_success 'tests clean up even on failures' '
834 '
835
836 test_expect_success 'test_atexit is run' '
837 - run_sub_test_lib_test_err \
837 + write_and_run_sub_test_lib_test_err \
838 atexit-cleanup "Run atexit commands" -i <<-\EOF &&
839 test_expect_success "tests clean up even after a failure" "
840 > ../../clean-atexit &&