Skip to content

Commit 565b6fa

Browse files
aspiersgitster
authored andcommitted
tests: refactor mechanics of testing in a sub test-lib
This will allow us to test the test framework more thoroughly without disrupting the top-level test metrics. Signed-off-by: Adam Spiers <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0a6d475 commit 565b6fa

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

t/t0000-basic.sh

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -55,39 +55,53 @@ test_expect_failure 'pretend we have a known breakage' '
5555
false
5656
'
5757

58-
test_expect_success 'pretend we have fixed a known breakage (run in sub test-lib)' "
59-
mkdir passing-todo &&
60-
(cd passing-todo &&
61-
cat >passing-todo.sh <<-EOF &&
62-
#!$SHELL_PATH
63-
64-
test_description='A passing TODO test
65-
66-
This is run in a sub test-lib so that we do not get incorrect
67-
passing metrics
68-
'
69-
70-
# Point to the t/test-lib.sh, which isn't in ../ as usual
71-
TEST_DIRECTORY=\"$TEST_DIRECTORY\"
72-
. \"\$TEST_DIRECTORY\"/test-lib.sh
58+
run_sub_test_lib_test () {
59+
name="$1" descr="$2" # stdin is the body of the test code
60+
mkdir "$name" &&
61+
(
62+
cd "$name" &&
63+
cat >"$name.sh" <<-EOF &&
64+
#!$SHELL_PATH
65+
66+
test_description='$descr (run in sub test-lib)
67+
68+
This is run in a sub test-lib so that we do not get incorrect
69+
passing metrics
70+
'
71+
72+
# Point to the t/test-lib.sh, which isn't in ../ as usual
73+
. "\$TEST_DIRECTORY"/test-lib.sh
74+
EOF
75+
cat >>"$name.sh" &&
76+
chmod +x "$name.sh" &&
77+
export TEST_DIRECTORY &&
78+
./"$name.sh" >out 2>err
79+
)
80+
}
7381

74-
test_expect_failure 'pretend we have fixed a known breakage' '
75-
:
76-
'
82+
check_sub_test_lib_test () {
83+
name="$1" # stdin is the expected output from the test
84+
(
85+
cd "$name" &&
86+
! test -s err &&
87+
sed -e 's/^> //' -e 's/Z$//' >expect &&
88+
test_cmp expect out
89+
)
90+
}
7791

92+
test_expect_success 'pretend we have fixed a known breakage' "
93+
run_sub_test_lib_test passing-todo 'A passing TODO test' <<-\\EOF &&
94+
test_expect_failure 'pretend we have fixed a known breakage' 'true'
7895
test_done
7996
EOF
80-
chmod +x passing-todo.sh &&
81-
./passing-todo.sh >out 2>err &&
82-
! test -s err &&
83-
sed -e 's/^> //' >expect <<-\\EOF &&
97+
check_sub_test_lib_test passing-todo <<-\\EOF
8498
> ok 1 - pretend we have fixed a known breakage # TODO known breakage
8599
> # fixed 1 known breakage(s)
86100
> # passed all 1 test(s)
87101
> 1..1
88102
EOF
89-
test_cmp expect out)
90103
"
104+
91105
test_set_prereq HAVEIT
92106
haveit=no
93107
test_expect_success HAVEIT 'test runs if prerequisite is satisfied' '
@@ -137,19 +151,8 @@ then
137151
fi
138152

139153
test_expect_success 'tests clean up even on failures' "
140-
mkdir failing-cleanup &&
141-
(
142-
cd failing-cleanup &&
143-
144-
cat >failing-cleanup.sh <<-EOF &&
145-
#!$SHELL_PATH
146-
147-
test_description='Failing tests with cleanup commands'
148-
149-
# Point to the t/test-lib.sh, which isn't in ../ as usual
150-
TEST_DIRECTORY=\"$TEST_DIRECTORY\"
151-
. \"\$TEST_DIRECTORY\"/test-lib.sh
152-
154+
test_must_fail run_sub_test_lib_test \
155+
failing-cleanup 'Failing tests with cleanup commands' <<-\\EOF &&
153156
test_expect_success 'tests clean up even after a failure' '
154157
touch clean-after-failure &&
155158
test_when_finished rm clean-after-failure &&
@@ -159,14 +162,8 @@ test_expect_success 'tests clean up even on failures' "
159162
test_when_finished \"(exit 2)\"
160163
'
161164
test_done
162-
163165
EOF
164-
165-
chmod +x failing-cleanup.sh &&
166-
test_must_fail ./failing-cleanup.sh >out 2>err &&
167-
! test -s err &&
168-
! test -f \"trash directory.failing-cleanup/clean-after-failure\" &&
169-
sed -e 's/Z$//' -e 's/^> //' >expect <<-\\EOF &&
166+
check_sub_test_lib_test failing-cleanup <<-\\EOF
170167
> not ok 1 - tests clean up even after a failure
171168
> # Z
172169
> # touch clean-after-failure &&
@@ -180,8 +177,6 @@ test_expect_success 'tests clean up even on failures' "
180177
> # failed 2 among 2 test(s)
181178
> 1..2
182179
EOF
183-
test_cmp expect out
184-
)
185180
"
186181

187182
################################################################

0 commit comments

Comments
 (0)