git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Jeff King <peff@peff.net>
Date
Dec 14, 2011, 23:21 UTC
Message-ID
<20111214232151.GB13128@sigill.intra.peff.net>
In-Reply-To
<20111214230713.GA13128@sigill.intra.peff.net>
On Wed, Dec 14, 2011 at 06:07:13PM -0500, Jeff King wrote:
Show 11 quoted lines
> On Wed, Dec 14, 2011 at 05:17:14PM +0100, Thomas Rast wrote:
> 
> > > 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 <peff@peff.net>
---
 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
Previous: Jeff KingNext: Jonathan Nieder
Message 6 of 12 in “What's cooking in git.git (Dec 2011, #04; Tue, 13)”
  1. Junio C HamanoDec 14, 2011
  2. tr/pty-all (Re: What's cooking in git.git (Dec 2011, #04; Tue, 13))Jonathan Nieder, Dec 14, 2011
  3. Thomas RastDec 14, 2011
  4. Junio C HamanoDec 14, 2011
  5. Jeff KingDec 14, 2011
  6. Jeff KingDec 14, 2011
  7. Jonathan NiederDec 14, 2011
  8. Jeff KingDec 15, 2011
  9. Jeff KingDec 15, 2011
  10. Junio C HamanoDec 15, 2011
  11. Jeff KingDec 15, 2011
  12. Junio C HamanoDec 15, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.