Skip to content

Commit f469873

Browse files
sunshinecogitster
authored andcommitted
t/perf: fix test_export() failure with BSD sed
test_perf() runs each test in its own subshell which makes it difficult to persist variables between tests. test_export() addresses this shortcoming by grabbing the values of specified variables after a test runs but before the subshell exits, and writes those values to a file which is loaded into the environment of subsequent tests. To grab the values to be persisted, test_export() pipes the output of the shell's builtin `set` command through `sed` which plucks them out using a regular expression along the lines of `s/^(var1|var2)/.../p`. Unfortunately, though, this use of alternation is not portable. For instance, BSD-lineage `sed` (including macOS `sed`) does not support it in the default "basic regular expression" mode (BRE). It may be possible to enable "extended regular expression" mode (ERE) in some cases with `sed -E`, however, `-E` is neither portable nor part of POSIX. Fortunately, alternation is unnecessary in this case and can easily be avoided, so replace it with a series of simple expressions such as `s/^var1/.../p;s/^var2/.../p`. While at it, tighten the expressions so they match the variable names exactly rather than matching prefixes (i.e. use `s/^var1=/.../p`). If the requirements of test_export() become more complex in the future, then an alternative would be to replace `sed` with `perl` which supports alternation on all platforms, however, the simple elimination of alternation via multiple `sed` expressions suffices for the present. Reported-by: Sangeeta <[email protected]> Diagnosed-by: Philippe Blain <[email protected]> Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 898f807 commit f469873

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

t/perf/perf-lib.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,18 @@ test_run_perf_ () {
148148
. '"$TEST_DIRECTORY"/test-lib-functions.sh'
149149
test_export () {
150150
[ $# != 0 ] || return 0
151-
test_export_="$test_export_\\|$1"
151+
test_export_="$test_export_ $1"
152152
shift
153153
test_export "$@"
154154
}
155155
'"$1"'
156156
ret=$?
157-
set | sed -n "s'"/'/'\\\\''/g"';s/^\\($test_export_\\)/export '"'&'"'/p" >test_vars
157+
needles=
158+
for v in $test_export_
159+
do
160+
needles="$needles;s/^$v=/export $v=/p"
161+
done
162+
set | sed -n "s'"/'/'\\\\''/g"'$needles" >test_vars
158163
exit $ret' >&3 2>&4
159164
eval_ret=$?
160165

0 commit comments

Comments
 (0)