Skip to content

Commit 9f0a452

Browse files
avargitster
authored andcommitted
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 565b6fa (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 517cd55 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 866a301 commit 9f0a452

File tree

2 files changed

+67
-49
lines changed

2 files changed

+67
-49
lines changed

t/lib-subtest.sh

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
117
_run_sub_test_lib_test_common () {
218
neg="$1" name="$2" descr="$3" # stdin is the body of the test code
319
shift 3
@@ -18,25 +34,15 @@ _run_sub_test_lib_test_common () {
1834
esac
1935
done
2036

21-
mkdir "$name" &&
2237
(
38+
cd "$name" &&
39+
2340
# Pretend we're not running under a test harness, whether we
2441
# are or not. The test-lib output depends on the setting of
2542
# this variable, so we need a stable setting under which to run
2643
# the sub-test.
2744
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-
'
3545

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" &&
4046
export TEST_DIRECTORY &&
4147
# The child test re-sources GIT-BUILD-OPTIONS and may thus
4248
# override the test output directory. We thus pass it as an
@@ -55,6 +61,18 @@ _run_sub_test_lib_test_common () {
5561
)
5662
}
5763

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+
5876
run_sub_test_lib_test () {
5977
_run_sub_test_lib_test_common '' "$@"
6078
}

t/t0000-basic.sh

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ test_expect_success 'success is reported like this' '
6868
'
6969

7070
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 &&
7272
for i in 1 2 3
7373
do
7474
test_expect_success "passing test #$i" "true"
@@ -85,7 +85,7 @@ test_expect_success 'pretend we have a fully passing test suite' '
8585
'
8686

8787
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 \
8989
partial-pass "2/3 tests passing" <<-\EOF &&
9090
test_expect_success "passing test #1" "true"
9191
test_expect_success "failing test #2" "false"
@@ -103,7 +103,7 @@ test_expect_success 'pretend we have a partially passing test suite' '
103103
'
104104

105105
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 &&
107107
test_expect_success "passing test" "true"
108108
test_expect_failure "pretend we have a known breakage" "false"
109109
test_done
@@ -118,7 +118,7 @@ test_expect_success 'pretend we have a known breakage' '
118118
'
119119

120120
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 &&
122122
test_expect_failure "pretend we have fixed a known breakage" "true"
123123
test_done
124124
EOF
@@ -130,7 +130,7 @@ test_expect_success 'pretend we have fixed a known breakage' '
130130
'
131131

132132
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 \
134134
"2 TODO tests, one passing" <<-\EOF &&
135135
test_expect_failure "pretend we have a known breakage" "false"
136136
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
149149
'
150150

151151
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 \
153153
mixed-results1 "mixed results #1" <<-\EOF &&
154154
test_expect_success "passing test" "true"
155155
test_expect_success "failing test" "false"
@@ -168,7 +168,7 @@ test_expect_success 'pretend we have a pass, fail, and known breakage' '
168168
'
169169

170170
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 \
172172
mixed-results2 "mixed results #2" <<-\EOF &&
173173
test_expect_success "passing test" "true"
174174
test_expect_success "passing test" "true"
@@ -204,7 +204,7 @@ test_expect_success 'pretend we have a mix of all possible results' '
204204
'
205205

206206
test_expect_success 'test --verbose' '
207-
run_sub_test_lib_test_err \
207+
write_and_run_sub_test_lib_test_err \
208208
t1234-verbose "test verbose" --verbose <<-\EOF &&
209209
test_expect_success "passing test" true
210210
test_expect_success "test with output" "echo foo"
@@ -231,7 +231,7 @@ test_expect_success 'test --verbose' '
231231
'
232232

233233
test_expect_success 'test --verbose-only' '
234-
run_sub_test_lib_test_err \
234+
write_and_run_sub_test_lib_test_err \
235235
t2345-verbose-only-2 "test verbose-only=2" \
236236
--verbose-only=2 <<-\EOF &&
237237
test_expect_success "passing test" true
@@ -255,7 +255,7 @@ test_expect_success 'test --verbose-only' '
255255

256256
test_expect_success 'GIT_SKIP_TESTS' '
257257
(
258-
run_sub_test_lib_test git-skip-tests-basic \
258+
write_and_run_sub_test_lib_test git-skip-tests-basic \
259259
"GIT_SKIP_TESTS" \
260260
--skip="git.2" <<-\EOF &&
261261
for i in 1 2 3
@@ -276,7 +276,7 @@ test_expect_success 'GIT_SKIP_TESTS' '
276276

277277
test_expect_success 'GIT_SKIP_TESTS several tests' '
278278
(
279-
run_sub_test_lib_test git-skip-tests-several \
279+
write_and_run_sub_test_lib_test git-skip-tests-several \
280280
"GIT_SKIP_TESTS several tests" \
281281
--skip="git.2 git.5" <<-\EOF &&
282282
for i in 1 2 3 4 5 6
@@ -300,7 +300,7 @@ test_expect_success 'GIT_SKIP_TESTS several tests' '
300300

301301
test_expect_success 'GIT_SKIP_TESTS sh pattern' '
302302
(
303-
run_sub_test_lib_test git-skip-tests-sh-pattern \
303+
write_and_run_sub_test_lib_test git-skip-tests-sh-pattern \
304304
"GIT_SKIP_TESTS sh pattern" \
305305
--skip="git.[2-5]" <<-\EOF &&
306306
for i in 1 2 3 4 5 6
@@ -324,7 +324,7 @@ test_expect_success 'GIT_SKIP_TESTS sh pattern' '
324324

325325
test_expect_success 'GIT_SKIP_TESTS entire suite' '
326326
(
327-
run_sub_test_lib_test git-skip-tests-entire-suite \
327+
write_and_run_sub_test_lib_test git-skip-tests-entire-suite \
328328
"GIT_SKIP_TESTS entire suite" \
329329
--skip="git" <<-\EOF &&
330330
for i in 1 2 3
@@ -341,7 +341,7 @@ test_expect_success 'GIT_SKIP_TESTS entire suite' '
341341

342342
test_expect_success 'GIT_SKIP_TESTS does not skip unmatched suite' '
343343
(
344-
run_sub_test_lib_test git-skip-tests-unmatched-suite \
344+
write_and_run_sub_test_lib_test git-skip-tests-unmatched-suite \
345345
"GIT_SKIP_TESTS does not skip unmatched suite" \
346346
--skip="notgit" <<-\EOF &&
347347
for i in 1 2 3
@@ -361,7 +361,7 @@ test_expect_success 'GIT_SKIP_TESTS does not skip unmatched suite' '
361361
'
362362

363363
test_expect_success '--run basic' '
364-
run_sub_test_lib_test run-basic \
364+
write_and_run_sub_test_lib_test run-basic \
365365
"--run basic" --run="1,3,5" <<-\EOF &&
366366
for i in 1 2 3 4 5 6
367367
do
@@ -382,7 +382,7 @@ test_expect_success '--run basic' '
382382
'
383383

384384
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 \
386386
"--run with a range" --run="1-3" <<-\EOF &&
387387
for i in 1 2 3 4 5 6
388388
do
@@ -403,7 +403,7 @@ test_expect_success '--run with a range' '
403403
'
404404

405405
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 \
407407
"--run with two ranges" --run="1-2,5-6" <<-\EOF &&
408408
for i in 1 2 3 4 5 6
409409
do
@@ -424,7 +424,7 @@ test_expect_success '--run with two ranges' '
424424
'
425425

426426
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 \
428428
"--run with a left open range" --run="-3" <<-\EOF &&
429429
for i in 1 2 3 4 5 6
430430
do
@@ -445,7 +445,7 @@ test_expect_success '--run with a left open range' '
445445
'
446446

447447
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 \
449449
"--run with a right open range" --run="4-" <<-\EOF &&
450450
for i in 1 2 3 4 5 6
451451
do
@@ -466,7 +466,7 @@ test_expect_success '--run with a right open range' '
466466
'
467467

468468
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 \
470470
"--run with basic negation" --run="!3" <<-\EOF &&
471471
for i in 1 2 3 4 5 6
472472
do
@@ -487,7 +487,7 @@ test_expect_success '--run with basic negation' '
487487
'
488488

489489
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 \
491491
"--run with two negations" --run="!3,!6" <<-\EOF &&
492492
for i in 1 2 3 4 5 6
493493
do
@@ -508,7 +508,7 @@ test_expect_success '--run with two negations' '
508508
'
509509

510510
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 \
512512
"--run a range and negation" --run="-4,!2" <<-\EOF &&
513513
for i in 1 2 3 4 5 6
514514
do
@@ -529,7 +529,7 @@ test_expect_success '--run a range and negation' '
529529
'
530530

531531
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 \
533533
"--run range negation" --run="!1-3" <<-\EOF &&
534534
for i in 1 2 3 4 5 6
535535
do
@@ -550,7 +550,7 @@ test_expect_success '--run range negation' '
550550
'
551551

552552
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 \
554554
"--run include, exclude and include" \
555555
--run="1-5,!1-3,2" <<-\EOF &&
556556
for i in 1 2 3 4 5 6
@@ -572,7 +572,7 @@ test_expect_success '--run include, exclude and include' '
572572
'
573573

574574
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 \
576576
"--run include, exclude and include, comma separated" \
577577
--run=1-5,!1-3,2 <<-\EOF &&
578578
for i in 1 2 3 4 5 6
@@ -594,7 +594,7 @@ test_expect_success '--run include, exclude and include, comma separated' '
594594
'
595595

596596
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 \
598598
"--run exclude and include" \
599599
--run="!3-,5" <<-\EOF &&
600600
for i in 1 2 3 4 5 6
@@ -616,7 +616,7 @@ test_expect_success '--run exclude and include' '
616616
'
617617

618618
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 \
620620
"--run empty selectors" \
621621
--run="1,,3,,,5" <<-\EOF &&
622622
for i in 1 2 3 4 5 6
@@ -638,7 +638,7 @@ test_expect_success '--run empty selectors' '
638638
'
639639

640640
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 \
642642
"--run empty selectors" \
643643
--run="relevant" <<-\EOF &&
644644
test_expect_success "relevant test" "true"
@@ -662,7 +662,7 @@ test_expect_success '--run substring selector' '
662662
'
663663

664664
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 \
666666
"--run invalid range start" \
667667
--run="a-5" <<-\EOF &&
668668
test_expect_success "passing test #1" "true"
@@ -677,7 +677,7 @@ test_expect_success '--run keyword selection' '
677677
'
678678

679679
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 \
681681
"--run invalid range end" \
682682
--run="1-z" <<-\EOF &&
683683
test_expect_success "passing test #1" "true"
@@ -692,7 +692,7 @@ test_expect_success '--run invalid range end' '
692692
'
693693

694694
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 &&
696696
697697
test_set_prereq HAVEIT
698698
test_expect_success HAVEIT "prereq is satisfied" "true"
@@ -722,7 +722,7 @@ test_expect_success 'tests respect prerequisites' '
722722
'
723723

724724
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 &&
726726
727727
test_lazy_prereq LAZY_TRUE true
728728
test_expect_success LAZY_TRUE "lazy prereq is satisifed" "true"
@@ -746,7 +746,7 @@ test_expect_success 'tests respect lazy prerequisites' '
746746
'
747747

748748
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 &&
750750
751751
test_lazy_prereq NESTED_INNER "
752752
>inner &&
@@ -772,7 +772,7 @@ test_expect_success 'nested lazy prerequisites' '
772772
'
773773

774774
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 \
776776
"lazy prereqs and -x" -v -x <<-\EOF &&
777777
test_lazy_prereq LAZY true
778778
@@ -785,7 +785,7 @@ test_expect_success 'lazy prereqs do not turn off tracing' '
785785
'
786786

787787
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 &&
789789
clean=no
790790
test_expect_success "do cleanup" "
791791
test_when_finished clean=yes
@@ -805,7 +805,7 @@ test_expect_success 'tests clean up after themselves' '
805805
'
806806

807807
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 \
809809
failing-cleanup "Failing tests with cleanup commands" <<-\EOF &&
810810
test_expect_success "tests clean up even after a failure" "
811811
touch clean-after-failure &&
@@ -834,7 +834,7 @@ test_expect_success 'tests clean up even on failures' '
834834
'
835835

836836
test_expect_success 'test_atexit is run' '
837-
run_sub_test_lib_test_err \
837+
write_and_run_sub_test_lib_test_err \
838838
atexit-cleanup "Run atexit commands" -i <<-\EOF &&
839839
test_expect_success "tests clean up even after a failure" "
840840
> ../../clean-atexit &&

0 commit comments

Comments
 (0)