Skip to content

Commit 781f76b

Browse files
peffgitster
authored andcommitted
test-lib: redirect stdin of tests
We want to run tests in a predictable, sterile environment so we can get repeatable results. They should take as little input as possible from the environment outside the test script. We already sanitize environment variables, but leave stdin untouched. This means that scripts can accidentally be impacted by content on stdin, or whether stdin isatty(). Furthermore, scripts reading from stdin can be annoying to outer loops which care about their stdin offset, like: while read sha1; do make test done A test which accidentally reads stdin would soak up all of the rest of the input intended for the outer shell loop. Let's redirect stdin from /dev/null, which solves both of these problems. It won't detect tests accidentally reading from stdin, but since doing so now gives a deterministic result, we don't need to consider that an error. We'll also leave file descriptor 6 as a link to the original stdin. Tests shouldn't need to look at this, but it can be convenient for inserting interactive commands while debugging tests (e.g., you could insert "bash <&6 >&3 2>&4" to run interactive commands in the environment of the test script). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 703f05a commit 781f76b

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

t/test-lib.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ then
191191
fi
192192

193193
exec 5>&1
194+
exec 6<&0
194195
if test "$verbose" = "t"
195196
then
196197
exec 4>&2 3>&1
@@ -469,7 +470,7 @@ test_debug () {
469470
test_eval_ () {
470471
# This is a separate function because some tests use
471472
# "return" to end a test_expect_success block early.
472-
eval >&3 2>&4 "$*"
473+
eval </dev/null >&3 2>&4 "$*"
473474
}
474475

475476
test_run_ () {

0 commit comments

Comments
 (0)