Skip to content

Commit 45fbdf5

Browse files
committed
Merge branch 'mt/test-lib-bundled-short-options'
Minor test usability improvement. * mt/test-lib-bundled-short-options: test-lib: allow short options to be bundled
2 parents 8633f21 + 78dc088 commit 45fbdf5

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

t/README

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ You can also run each test individually from command line, like this:
6969

7070
You can pass --verbose (or -v), --debug (or -d), and --immediate
7171
(or -i) command line argument to the test, or by setting GIT_TEST_OPTS
72-
appropriately before running "make".
72+
appropriately before running "make". Short options can be bundled, i.e.
73+
'-d -v' is the same as '-dv'.
7374

7475
-v::
7576
--verbose::

t/test-lib.sh

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,23 @@ then
7878
exit 1
7979
fi
8080

81-
# Parse options while taking care to leave $@ intact, so we will still
82-
# have all the original command line options when executing the test
83-
# script again for '--tee' and '--verbose-log' below.
8481
store_arg_to=
85-
prev_opt=
86-
for opt
87-
do
88-
if test -n "$store_arg_to"
82+
opt_required_arg=
83+
# $1: option string
84+
# $2: name of the var where the arg will be stored
85+
mark_option_requires_arg () {
86+
if test -n "$opt_required_arg"
8987
then
90-
eval $store_arg_to=\$opt
91-
store_arg_to=
92-
prev_opt=
93-
continue
88+
echo "error: options that require args cannot be bundled" \
89+
"together: '$opt_required_arg' and '$1'" >&2
90+
exit 1
9491
fi
92+
opt_required_arg=$1
93+
store_arg_to=$2
94+
}
95+
96+
parse_option () {
97+
local opt="$1"
9598

9699
case "$opt" in
97100
-d|--d|--de|--deb|--debu|--debug)
@@ -101,7 +104,7 @@ do
101104
-l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
102105
GIT_TEST_LONG=t; export GIT_TEST_LONG ;;
103106
-r)
104-
store_arg_to=run_list
107+
mark_option_requires_arg "$opt" run_list
105108
;;
106109
--run=*)
107110
run_list=${opt#--*=} ;;
@@ -185,12 +188,42 @@ do
185188
*)
186189
echo "error: unknown test option '$opt'" >&2; exit 1 ;;
187190
esac
191+
}
192+
193+
# Parse options while taking care to leave $@ intact, so we will still
194+
# have all the original command line options when executing the test
195+
# script again for '--tee' and '--verbose-log' later.
196+
for opt
197+
do
198+
if test -n "$store_arg_to"
199+
then
200+
eval $store_arg_to=\$opt
201+
store_arg_to=
202+
opt_required_arg=
203+
continue
204+
fi
188205

189-
prev_opt=$opt
206+
case "$opt" in
207+
--*|-?)
208+
parse_option "$opt" ;;
209+
-?*)
210+
# bundled short options must be fed separately to parse_option
211+
opt=${opt#-}
212+
while test -n "$opt"
213+
do
214+
extra=${opt#?}
215+
this=${opt%$extra}
216+
opt=$extra
217+
parse_option "-$this"
218+
done
219+
;;
220+
*)
221+
echo "error: unknown test option '$opt'" >&2; exit 1 ;;
222+
esac
190223
done
191224
if test -n "$store_arg_to"
192225
then
193-
echo "error: $prev_opt requires an argument" >&2
226+
echo "error: $opt_required_arg requires an argument" >&2
194227
exit 1
195228
fi
196229

0 commit comments

Comments
 (0)