From: Stefano Rivera <stefano@rivera.za.net>
Date: Tue, 16 Feb 2021 13:14:51 -0800
Subject: Re-useable test-runner script

run_coverage doesn't usefully report failures, and mandates running coverage reports.

This breaks it into run_tests and run_coverage. They all should now exit
non-zero if a test fails.

Forwarded: https://github.com/newren/git-filter-repo/pull/205
---
 Makefile       |  2 +-
 t/run_coverage | 14 +++++++++-----
 t/run_tests    | 27 +++++++++++++++++++++++++++
 3 files changed, 37 insertions(+), 6 deletions(-)
 create mode 100755 t/run_tests

diff --git a/Makefile b/Makefile
index 1b7b512..31f5e3a 100644
--- a/Makefile
+++ b/Makefile
@@ -12,7 +12,7 @@ build:
 	@echo Nothing to do: filter-repo is a script which needs no compilation.
 
 test:
-	cd t && time ./run_coverage
+	time t/run_coverage
 
 # fixup_locale might matter once we actually have translations, but right now
 # we don't.  It might not even matter then, because python has a fallback podir.
diff --git a/t/run_coverage b/t/run_coverage
index f72cdd5..5517459 100755
--- a/t/run_coverage
+++ b/t/run_coverage
@@ -1,5 +1,7 @@
 #!/bin/bash
 
+set -eu
+
 orig_dir=$(cd $(dirname $0) && pwd -P)
 tmpdir=$(mktemp -d)
 
@@ -16,12 +18,12 @@ EOF
 
 export COVERAGE_PROCESS_START=$tmpdir/.coveragerc
 export PYTHONPATH=$tmpdir:
-# We pretend filenames are unicode for two reasons: (1) because it exercises
-# more code, and (2) this setting will detect accidental use of unicode strings
-# for file/directory names when it should always be bytestrings.
-export PRETEND_UNICODE_ARGS=1
 
-ls t939*.sh | xargs -n 1 bash
+# Produce a coverage report, even if the tests fail
+set +e
+$orig_dir/run_tests
+exitcode=$?
+set -e
 
 cd $tmpdir
 coverage3 combine
@@ -29,3 +31,5 @@ coverage3 html -d $orig_dir/report
 coverage3 report -m
 cd $orig_dir
 rm -rf $tmpdir
+
+exit $exitcode
diff --git a/t/run_tests b/t/run_tests
new file mode 100755
index 0000000..752c315
--- /dev/null
+++ b/t/run_tests
@@ -0,0 +1,27 @@
+#!/bin/bash
+set -eu
+
+cd $(dirname $0)
+
+# Put git_filter_branch.py on the front of PYTHONPATH
+export PYTHONPATH=$PWD/..${PYTHONPATH:+:$PYTHONPATH}
+
+# We pretend filenames are unicode for two reasons: (1) because it exercises
+# more code, and (2) this setting will detect accidental use of unicode strings
+# for file/directory names when it should always be bytestrings.
+export PRETEND_UNICODE_ARGS=1
+
+export TEST_SHELL_PATH=/bin/sh
+
+failed=0
+
+for t in t[0-9]*.sh
+do
+  printf '\n\n== %s ==\n' "$t"
+  bash $t "$@" || failed=$(($failed+1))
+done
+
+if [ 0 -lt $failed ]
+then
+  exit 1
+fi
