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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 t/README      |  4 ++++
 t/test-lib.sh | 12 ++++++++++++
 2 files changed, 16 insertions(+)

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
-- 
2.24.0.399.gf8350c9437




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux