diff mbox series

[27/27] t: run tests with `set -o pipefail` on Bash

Message ID bc3f226edf284d96dc7fff15c188ecfbc32162d6.1573779466.git.liu.denton@gmail.com (mailing list archive)
State New, archived
Headers show
Series t: general test cleanup + `set -o pipefail` | expand

Commit Message

Denton Liu Nov. 15, 2019, 1:01 a.m. UTC
The current convention is to ensure that git commands are not placed in
the upstream of a pipe. If they are, they could fail in an undetectable
manner since a pipe's return code is the last command in the pipe.
However, many old tests are still written with git commands in the
upstream of a pipe.

In the spirit of catching these failures, run tests with
`set -o pipefail` if the underlying shell is Bash. This way, we can
catch failures of Git commands that may occur even in the middle of a
pipeline.

In the future, more shells that support `set -o pipefail` may have it
enabled but let's start small for now.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 t/README      |  4 ++++
 t/test-lib.sh | 12 ++++++++++++
 2 files changed, 16 insertions(+)
diff mbox series

Patch

diff --git a/t/README b/t/README
index 60d5b77bcc..ba96b6d113 100644
--- a/t/README
+++ b/t/README
@@ -415,6 +415,10 @@  GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=<boolean>, when true (which is
 the default when running tests), errors out when an abbreviated option
 is used.
 
+GIT_TEST_PIPEFAIL=<boolean>, when true, run 'set -o pipefail' to catch
+failures in commands that aren't the last in a pipe. Defaults to true on
+Bash and false otherwise.
+
 Naming Tests
 ------------
 
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 46c4440843..c0c43dfce9 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -64,6 +64,18 @@  then
 	export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
 fi
 
+# Use set -o pipefail on platforms that support it
+GIT_TEST_PIPEFAIL_DEFAULT=false
+# TODO: detect more platforms that support `set -o pipefail`
+if test -n "$BASH_VERSION"
+then
+	GIT_TEST_PIPEFAIL_DEFAULT=true
+fi
+if git env--helper --type=bool --default="$GIT_TEST_PIPEFAIL_DEFAULT" --exit-code GIT_TEST_PIPEFAIL
+then
+	set -o pipefail
+fi
+
 ################################################################
 # It appears that people try to run tests without building...
 "${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null