Skip to contentSkip to conversation

test-lib: guard against being run as 'prove t*.sh'

PATCH In review Ævar Arnfjörð Bjarmason 2 participants 2 messages Mar 11, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 test-lib: guard against being run as 'prove t*.sh'

We have t/Makefile targets to properly run the tests with prove, however it's also just as valid to run them as e.g.:

    prove t[0-9]*.sh

But if they're run as:

prove t*.sh

The prove tool will optimistically try to parse and execute test-lib.sh and test-lib-functions.sh as Perl code, currently there are no bad results of doing this other than a wall of error messages, but let's be helpful and bail out early in this case.

Note that the shebang being added here doesn't mean anything to a POSIX OS because the files aren't executable, it's picked up by Perl's TAP tools. If the shebang wasn't here this would still be considered a Perl file for the purposes of those heuristics.

I could also use the "Bail out!" keyword here, see 614fe01521 (test-lib: bail out when "-v" used under "prove", 2016-10-22) for prior art. I think it's more gentle to just print a SKIP notice.

---
 t/test-lib-functions.sh | 9 +++++++++
 t/test-lib.sh           | 9 +++++++++
 2 files changed, 18 insertions(+)
t/test-lib-functions.sh +9 -0
index 6348e8d733..7ba04fe399 100644
@@ -1,3 +1,5 @@
+#!/bin/sh
+#
 # Library of functions shared by all tests scripts, included by
 # test-lib.sh.
 #
@@ -16,6 +18,13 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see http://www.gnu.org/licenses/ .
 
+# Early bailing out on 'prove t*.sh'
+if test -n "$HARNESS_ACTIVE" -a "$0" = "test-lib-functions.sh"
+then
+	echo "1..0 # SKIP accidentally invoked 'prove t*.sh'?"
+	exit 0
+fi
+
 # The semantics of the editor variables are that of invoking
 # sh -c "$EDITOR \"$@\"" files ...
 #
t/test-lib.sh +9 -0
index d3f6af6a65..7ceae9a38d 100644
@@ -1,3 +1,5 @@
+#!/bin/sh
+#
 # Test framework for git.  See t/README for usage.
 #
 # Copyright (c) 2005 Junio C Hamano
@@ -15,6 +17,13 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see http://www.gnu.org/licenses/ .
 
+# Early bailing out on 'prove t*.sh'
+if test -n "$HARNESS_ACTIVE" -a "$0" = "test-lib.sh"
+then
+	echo "1..0 # SKIP accidentally invoked 'prove t*.sh'?"
+	exit 0
+fi
+
 # Test the binaries we have just built.  The tests are kept in
 # t/ subdirectory and are run in 'trash directory' subdirectory.
 if test -z "$TEST_DIRECTORY"
Signature · 1 line
2.31.0.rc2.211.g1d0b8788b3
#1 lore raw
Quoted 8 lines · Ævar Arnfjörð Bjarmason

We have t/Makefile targets to properly run the tests with prove, however it's also just as valid to run them as e.g.:

    prove t[0-9]*.sh

But if they're run as:

prove t*.sh

I am not sure if the above is a good enough excuse to diverge from our convention not to add #! to signal that a scriptlet is not to be used standalone but is meant to be dot-sourced. At least to me, this change looks like making the codebase worse to protect from a mistake that we should not have to worry about in real life.

But that may be because my use of prove is limited to a more targetted and narrow set of tests (i.e. either use "t4???-*.sh" for an area, or a single test---anything broader I'd use "make").