Thread (8 messages) 8 messages, 4 authors, 2016-06-15

Re: tr/pty-all (Re: What's cooking in git.git (Dec 2011, #04; Tue, 13))

flat view

From: Jeff King <hidden>
Date: 2016-06-15 22:52:38
Subsystem: the rest · Maintainer: Linus Torvalds

On Wed, Dec 14, 2011 at 06:07:13PM -0500, Jeff King wrote:
On Wed, Dec 14, 2011 at 05:17:14PM +0100, Thomas Rast wrote:
quoted
quoted
I also find Jeff's patch [3] appealing.
Me too, though wonder whether feeding a file full of garbage wouldn't
be better, so as to trip up commands that try to read only from a
non-tty stdin.
Interesting idea. Instead of getting an immediate EOF from /dev/null,
they'll get some junk which they may or may not complain about. I played
around with this a bit, redirecting test stdin from a file with:
So here is what that patch looked like. As I mentioned, it doesn't
actually catch the shortlog problem, but it would fix
Michael's issue with an outer reading loop.

-- >8 --
Subject: [PATCH] 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 a known source to eliminate
variation. We could just connect it to /dev/null. However,
tests which accidentally read stdin would then see immediate
EOF, which may or may not cause them to notice the errror.
Instead, let's connect it to a file with random garbage in
it, in the hope that it will be more likely to trigger an
error in the mis-written test.

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 <redacted>
---
 t/stdin-garbage |    1 +
 t/test-lib.sh   |    4 +++-
 2 files changed, 4 insertions(+), 1 deletions(-)
 create mode 100644 t/stdin-garbage
diff --git a/t/stdin-garbage b/t/stdin-garbage
new file mode 100644
index 0000000..3a2ebc2
--- /dev/null
+++ b/t/stdin-garbage
@@ -0,0 +1 @@
+This is a garbage file that will be connected to the stdin of each test.
diff --git a/t/test-lib.sh b/t/test-lib.sh
index bdd9513..67eb078 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -198,6 +198,8 @@ else
 	exec 4>/dev/null 3>/dev/null
 fi
 
+exec 6<&0
+
 test_failure=0
 test_count=0
 test_fixed=0
@@ -469,7 +471,7 @@ test_debug () {
 test_eval_ () {
 	# This is a separate function because some tests use
 	# "return" to end a test_expect_success block early.
-	eval >&3 2>&4 "$*"
+	eval <"$TEST_DIRECTORY/stdin-garbage" >&3 2>&4 "$*"
 }
 
 test_run_ () {
-- 
1.7.8.rc2.30.g803b1a
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help