Skip to content

Commit f21ac36

Browse files
newrengitster
authored andcommitted
test-lib: allow selecting tests by substring/glob with --run
Many of our test scripts have several "setup" tests. It's a lot easier to say ./t0050-filesystem.sh --run=setup,9 in order to run all the setup tests as well as test #9, than it is to track down what all the setup tests are and enter all their numbers in the list. Also, I often find myself wanting to run just one or a couple tests from the test file, but I don't know the numbering of any of the tests -- to get it I either have to first run the whole test file (or start counting by hand or figure out some other clever but non-obvious tricks). It's really convenient to be able to just look at the test description(s) and then run ./t6416-recursive-corner-cases.sh --run=symlink or ./t6402-merge-rename.sh --run='setup,unnecessary update' Add such an ability to test selection which relies on merely matching against the test description. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d4a3924 commit f21ac36

File tree

3 files changed

+74
-47
lines changed

3 files changed

+74
-47
lines changed

t/README

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,26 +258,28 @@ For an individual test suite --run could be used to specify that
258258
only some tests should be run or that some tests should be
259259
excluded from a run.
260260

261-
The argument for --run is a list of individual test numbers or
262-
ranges with an optional negation prefix that define what tests in
263-
a test suite to include in the run. A range is two numbers
264-
separated with a dash and matches a range of tests with both ends
265-
been included. You may omit the first or the second number to
266-
mean "from the first test" or "up to the very last test"
267-
respectively.
268-
269-
Optional prefix of '!' means that the test or a range of tests
270-
should be excluded from the run.
261+
The argument for --run, <test-selector>, is a list of description
262+
substrings or globs or individual test numbers or ranges with an
263+
optional negation prefix (of '!') that define what tests in a test
264+
suite to include (or exclude, if negated) in the run. A range is two
265+
numbers separated with a dash and matches a range of tests with both
266+
ends been included. You may omit the first or the second number to
267+
mean "from the first test" or "up to the very last test" respectively.
268+
269+
The argument to --run is split on commas into separate strings,
270+
numbers, and ranges, and picks all tests that match any of the
271+
individual selection criteria. If the substring of the description
272+
text that you want to match includes a comma, use the glob character
273+
'?' instead. For example --run='rebase,merge?cherry-pick' would match
274+
on all tests that match either the glob *rebase* or the glob
275+
*merge?cherry-pick*.
271276

272277
If --run starts with an unprefixed number or range the initial
273278
set of tests to run is empty. If the first item starts with '!'
274279
all the tests are added to the initial set. After initial set is
275280
determined every test number or range is added or excluded from
276281
the set one by one, from left to right.
277282

278-
Individual numbers or ranges could be separated either by a space
279-
or a comma.
280-
281283
For example, to run only tests up to a specific test (21), one
282284
could do this:
283285

@@ -290,25 +292,25 @@ or this:
290292
Common case is to run several setup tests (1, 2, 3) and then a
291293
specific test (21) that relies on that setup:
292294

293-
$ sh ./t9200-git-cvsexport-commit.sh --run='1 2 3 21'
295+
$ sh ./t9200-git-cvsexport-commit.sh --run='1,2,3,21'
294296

295297
or:
296298

297299
$ sh ./t9200-git-cvsexport-commit.sh --run=1,2,3,21
298300

299301
or:
300302

301-
$ sh ./t9200-git-cvsexport-commit.sh --run='-3 21'
303+
$ sh ./t9200-git-cvsexport-commit.sh --run='-3,21'
302304

303305
As noted above, the test set is built by going through the items
304306
from left to right, so this:
305307

306-
$ sh ./t9200-git-cvsexport-commit.sh --run='1-4 !3'
308+
$ sh ./t9200-git-cvsexport-commit.sh --run='1-4,!3'
307309

308310
will run tests 1, 2, and 4. Items that come later have higher
309311
precedence. It means that this:
310312

311-
$ sh ./t9200-git-cvsexport-commit.sh --run='!3 1-4'
313+
$ sh ./t9200-git-cvsexport-commit.sh --run='!3,1-4'
312314

313315
would just run tests from 1 to 4, including 3.
314316

@@ -317,6 +319,18 @@ test in the test suite except from 7 up to 11:
317319

318320
$ sh ./t9200-git-cvsexport-commit.sh --run='!7-11'
319321

322+
Sometimes there may be multiple tests with e.g. "setup" in their name
323+
that are needed and rather than figuring out the number for all of them
324+
we can just use "setup" as a substring/glob to match against the test
325+
description:
326+
327+
$ sh ./t0050-filesystem.sh --run=setup,9-11
328+
329+
or one could select both the setup tests and the rename ones (assuming all
330+
relevant tests had those words in their descriptions):
331+
332+
$ sh ./t0050-filesystem.sh --run=setup,rename
333+
320334
Some tests in a test suite rely on the previous tests performing
321335
certain actions, specifically some tests are designated as
322336
"setup" test, so you cannot _arbitrarily_ disable one test and

t/t0000-basic.sh

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ test_expect_success 'GIT_SKIP_TESTS does not skip unmatched suite' "
430430

431431
test_expect_success '--run basic' "
432432
run_sub_test_lib_test run-basic \
433-
'--run basic' --run='1 3 5' <<-\\EOF &&
433+
'--run basic' --run='1,3,5' <<-\\EOF &&
434434
for i in 1 2 3 4 5 6
435435
do
436436
test_expect_success \"passing test #\$i\" 'true'
@@ -472,7 +472,7 @@ test_expect_success '--run with a range' "
472472

473473
test_expect_success '--run with two ranges' "
474474
run_sub_test_lib_test run-two-ranges \
475-
'--run with two ranges' --run='1-2 5-6' <<-\\EOF &&
475+
'--run with two ranges' --run='1-2,5-6' <<-\\EOF &&
476476
for i in 1 2 3 4 5 6
477477
do
478478
test_expect_success \"passing test #\$i\" 'true'
@@ -556,7 +556,7 @@ test_expect_success '--run with basic negation' "
556556

557557
test_expect_success '--run with two negations' "
558558
run_sub_test_lib_test run-two-neg \
559-
'--run with two negations' --run='"'!3 !6'"' <<-\\EOF &&
559+
'--run with two negations' --run='"'!3,!6'"' <<-\\EOF &&
560560
for i in 1 2 3 4 5 6
561561
do
562562
test_expect_success \"passing test #\$i\" 'true'
@@ -577,7 +577,7 @@ test_expect_success '--run with two negations' "
577577

578578
test_expect_success '--run a range and negation' "
579579
run_sub_test_lib_test run-range-and-neg \
580-
'--run a range and negation' --run='"'-4 !2'"' <<-\\EOF &&
580+
'--run a range and negation' --run='"'-4,!2'"' <<-\\EOF &&
581581
for i in 1 2 3 4 5 6
582582
do
583583
test_expect_success \"passing test #\$i\" 'true'
@@ -620,7 +620,7 @@ test_expect_success '--run range negation' "
620620
test_expect_success '--run include, exclude and include' "
621621
run_sub_test_lib_test run-inc-neg-inc \
622622
'--run include, exclude and include' \
623-
--run='"'1-5 !1-3 2'"' <<-\\EOF &&
623+
--run='"'1-5,!1-3,2'"' <<-\\EOF &&
624624
for i in 1 2 3 4 5 6
625625
do
626626
test_expect_success \"passing test #\$i\" 'true'
@@ -664,7 +664,7 @@ test_expect_success '--run include, exclude and include, comma separated' "
664664
test_expect_success '--run exclude and include' "
665665
run_sub_test_lib_test run-neg-inc \
666666
'--run exclude and include' \
667-
--run='"'!3- 5'"' <<-\\EOF &&
667+
--run='"'!3-,5'"' <<-\\EOF &&
668668
for i in 1 2 3 4 5 6
669669
do
670670
test_expect_success \"passing test #\$i\" 'true'
@@ -705,7 +705,31 @@ test_expect_success '--run empty selectors' "
705705
EOF
706706
"
707707

708-
test_expect_success '--run invalid range start' "
708+
test_expect_success '--run substring selector' "
709+
run_sub_test_lib_test run-substring-selector \
710+
'--run empty selectors' \
711+
--run='relevant' <<-\\EOF &&
712+
test_expect_success \"relevant test\" 'true'
713+
for i in 1 2 3 4 5 6
714+
do
715+
test_expect_success \"other test #\$i\" 'true'
716+
done
717+
test_done
718+
EOF
719+
check_sub_test_lib_test run-substring-selector <<-\\EOF
720+
> ok 1 - relevant test
721+
> ok 2 # skip other test #1 (--run)
722+
> ok 3 # skip other test #2 (--run)
723+
> ok 4 # skip other test #3 (--run)
724+
> ok 5 # skip other test #4 (--run)
725+
> ok 6 # skip other test #5 (--run)
726+
> ok 7 # skip other test #6 (--run)
727+
> # passed all 7 test(s)
728+
> 1..7
729+
EOF
730+
"
731+
732+
test_expect_success '--run keyword selection' "
709733
run_sub_test_lib_test_err run-inv-range-start \
710734
'--run invalid range start' \
711735
--run='a-5' <<-\\EOF &&
@@ -735,21 +759,6 @@ test_expect_success '--run invalid range end' "
735759
EOF_ERR
736760
"
737761

738-
test_expect_success '--run invalid selector' "
739-
run_sub_test_lib_test_err run-inv-selector \
740-
'--run invalid selector' \
741-
--run='1?' <<-\\EOF &&
742-
test_expect_success \"passing test #1\" 'true'
743-
test_done
744-
EOF
745-
check_sub_test_lib_test_err run-inv-selector \
746-
<<-\\EOF_OUT 3<<-\\EOF_ERR
747-
> FATAL: Unexpected exit with code 1
748-
EOF_OUT
749-
> error: --run: invalid non-numeric in test selector: '1?'
750-
EOF_ERR
751-
"
752-
753762

754763
test_set_prereq HAVEIT
755764
haveit=no

t/test-lib.sh

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -769,15 +769,17 @@ match_pattern_list () {
769769
}
770770

771771
match_test_selector_list () {
772+
operation="$1"
773+
shift
772774
title="$1"
773775
shift
774776
arg="$1"
775777
shift
776778
test -z "$1" && return 0
777779

778-
# Both commas and whitespace are accepted as separators.
780+
# Commas are accepted as separators.
779781
OLDIFS=$IFS
780-
IFS=' ,'
782+
IFS=','
781783
set -- $1
782784
IFS=$OLDIFS
783785

@@ -805,23 +807,25 @@ match_test_selector_list () {
805807
*-*)
806808
if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
807809
then
808-
echo "error: $title: invalid non-numeric in range" \
810+
echo "error: $operation: invalid non-numeric in range" \
809811
"start: '$orig_selector'" >&2
810812
exit 1
811813
fi
812814
if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
813815
then
814-
echo "error: $title: invalid non-numeric in range" \
816+
echo "error: $operation: invalid non-numeric in range" \
815817
"end: '$orig_selector'" >&2
816818
exit 1
817819
fi
818820
;;
819821
*)
820822
if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
821823
then
822-
echo "error: $title: invalid non-numeric in test" \
823-
"selector: '$orig_selector'" >&2
824-
exit 1
824+
case "$title" in *${selector}*)
825+
include=$positive
826+
;;
827+
esac
828+
continue
825829
fi
826830
esac
827831

@@ -1031,7 +1035,7 @@ test_skip () {
10311035
skipped_reason="GIT_SKIP_TESTS"
10321036
fi
10331037
if test -z "$to_skip" && test -n "$run_list" &&
1034-
! match_test_selector_list '--run' $test_count "$run_list"
1038+
! match_test_selector_list '--run' "$1" $test_count "$run_list"
10351039
then
10361040
to_skip=t
10371041
skipped_reason="--run"

0 commit comments

Comments
 (0)