test-lib tests: move "run_sub_test" to a new lib-subtest.sh

Move the "check_sub_test_lib_test()" and its sister functions to a new lib-subtest.sh. In the future (not in this series) I'd like to test test-lib's output in a more targeted and smaller test, and I'll need these functions to do that. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Jul 22, 2021 at 00:57 UTC 866a3014de34d415300bdd488f2ccd7ae5fb2d65
2 files changed +87 -87
t/lib-subtest.sh new
+86
@@ -0,0 +1,86 @@
1 +_run_sub_test_lib_test_common () {
2 + neg="$1" name="$2" descr="$3" # stdin is the body of the test code
3 + shift 3
4 +
5 + # intercept pseudo-options at the front of the argument list that we
6 + # will not pass to child script
7 + skip=
8 + while test $# -gt 0
9 + do
10 + case "$1" in
11 + --skip=*)
12 + skip=${1#--*=}
13 + shift
14 + ;;
15 + *)
16 + break
17 + ;;
18 + esac
19 + done
20 +
21 + mkdir "$name" &&
22 + (
23 + # Pretend we're not running under a test harness, whether we
24 + # are or not. The test-lib output depends on the setting of
25 + # this variable, so we need a stable setting under which to run
26 + # the sub-test.
27 + 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 + '
35 +
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" &&
40 + export TEST_DIRECTORY &&
41 + # The child test re-sources GIT-BUILD-OPTIONS and may thus
42 + # override the test output directory. We thus pass it as an
43 + # explicit override to the child.
44 + TEST_OUTPUT_DIRECTORY_OVERRIDE=$(pwd) &&
45 + export TEST_OUTPUT_DIRECTORY_OVERRIDE &&
46 + GIT_SKIP_TESTS=$skip &&
47 + export GIT_SKIP_TESTS &&
48 + sane_unset GIT_TEST_FAIL_PREREQS &&
49 + if test -z "$neg"
50 + then
51 + ./"$name.sh" "$@" >out 2>err
52 + else
53 + ! ./"$name.sh" "$@" >out 2>err
54 + fi
55 + )
56 +}
57 +
58 +run_sub_test_lib_test () {
59 + _run_sub_test_lib_test_common '' "$@"
60 +}
61 +
62 +run_sub_test_lib_test_err () {
63 + _run_sub_test_lib_test_common '!' "$@"
64 +}
65 +
66 +check_sub_test_lib_test () {
67 + name="$1" # stdin is the expected output from the test
68 + (
69 + cd "$name" &&
70 + test_must_be_empty err &&
71 + sed -e 's/^> //' -e 's/Z$//' >expect &&
72 + test_cmp expect out
73 + )
74 +}
75 +
76 +check_sub_test_lib_test_err () {
77 + name="$1" # stdin is the expected output from the test
78 + # expected error output is in descriptor 3
79 + (
80 + cd "$name" &&
81 + sed -e 's/^> //' -e 's/Z$//' >expect.out &&
82 + test_cmp expect.out out &&
83 + sed -e 's/^> //' -e 's/Z$//' <&3 >expect.err &&
84 + test_cmp expect.err err
85 + )
86 +}
t/t0000-basic.sh
+1 -87
@@ -19,6 +19,7 @@ modification *should* take notice and update the test vectors here.
19 '
20
21 . ./test-lib.sh
22 +. "$TEST_DIRECTORY"/lib-subtest.sh
23
24 try_local_xy () {
25 local x="local" y="alsolocal" &&
@@ -66,93 +67,6 @@ test_expect_success 'success is reported like this' '
67 :
68 '
69
69 -_run_sub_test_lib_test_common () {
70 - neg="$1" name="$2" descr="$3" # stdin is the body of the test code
71 - shift 3
72 -
73 - # intercept pseudo-options at the front of the argument list that we
74 - # will not pass to child script
75 - skip=
76 - while test $# -gt 0
77 - do
78 - case "$1" in
79 - --skip=*)
80 - skip=${1#--*=}
81 - shift
82 - ;;
83 - *)
84 - break
85 - ;;
86 - esac
87 - done
88 -
89 - mkdir "$name" &&
90 - (
91 - # Pretend we're not running under a test harness, whether we
92 - # are or not. The test-lib output depends on the setting of
93 - # this variable, so we need a stable setting under which to run
94 - # the sub-test.
95 - sane_unset HARNESS_ACTIVE &&
96 - cd "$name" &&
97 - write_script "$name.sh" "$TEST_SHELL_PATH" <<-EOF &&
98 - test_description='$descr (run in sub test-lib)
99 -
100 - This is run in a sub test-lib so that we do not get incorrect
101 - passing metrics
102 - '
103 -
104 - # Point to the t/test-lib.sh, which isn't in ../ as usual
105 - . "\$TEST_DIRECTORY"/test-lib.sh
106 - EOF
107 - cat >>"$name.sh" &&
108 - export TEST_DIRECTORY &&
109 - # The child test re-sources GIT-BUILD-OPTIONS and may thus
110 - # override the test output directory. We thus pass it as an
111 - # explicit override to the child.
112 - TEST_OUTPUT_DIRECTORY_OVERRIDE=$(pwd) &&
113 - export TEST_OUTPUT_DIRECTORY_OVERRIDE &&
114 - GIT_SKIP_TESTS=$skip &&
115 - export GIT_SKIP_TESTS &&
116 - sane_unset GIT_TEST_FAIL_PREREQS &&
117 - if test -z "$neg"
118 - then
119 - ./"$name.sh" "$@" >out 2>err
120 - else
121 - ! ./"$name.sh" "$@" >out 2>err
122 - fi
123 - )
124 -}
125 -
126 -run_sub_test_lib_test () {
127 - _run_sub_test_lib_test_common '' "$@"
128 -}
129 -
130 -run_sub_test_lib_test_err () {
131 - _run_sub_test_lib_test_common '!' "$@"
132 -}
133 -
134 -check_sub_test_lib_test () {
135 - name="$1" # stdin is the expected output from the test
136 - (
137 - cd "$name" &&
138 - test_must_be_empty err &&
139 - sed -e 's/^> //' -e 's/Z$//' >expect &&
140 - test_cmp expect out
141 - )
142 -}
143 -
144 -check_sub_test_lib_test_err () {
145 - name="$1" # stdin is the expected output from the test
146 - # expected error output is in descriptor 3
147 - (
148 - cd "$name" &&
149 - sed -e 's/^> //' -e 's/Z$//' >expect.out &&
150 - test_cmp expect.out out &&
151 - sed -e 's/^> //' -e 's/Z$//' <&3 >expect.err &&
152 - test_cmp expect.err err
153 - )
154 -}
155 -
70 test_expect_success 'pretend we have a fully passing test suite' '
71 run_sub_test_lib_test full-pass "3 passing tests" <<-\EOF &&
72 for i in 1 2 3