{"thread":{"id":"55289","subject":"[PATCH] test-lib: guard against being run as 'prove t*.sh'","startedAt":"2021-03-11T12:43:23Z","lastAt":"2021-03-11T17:35:00Z","messageCount":2,"participants":["Ævar Arnfjörð Bjarmason","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"418838","messageId":"20210311124216.25036-1-avarab@gmail.com","threadId":"55289","inReplyTo":null,"subject":"[PATCH] test-lib: guard against being run as 'prove t*.sh'","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-03-11T12:42:16Z","receivedAt":"2021-03-11T12:43:23Z","isPatch":true,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"We have t/Makefile targets to properly run the tests with prove,\nhowever it's also just as valid to run them as e.g.:\n\n    prove t[0-9]*.sh\n\nBut if they're run as:\n\n    prove t*.sh\n\nThe prove tool will optimistically try to parse and execute\ntest-lib.sh and test-lib-functions.sh as Perl code, currently there\nare no bad results of doing this other than a wall of error messages,\nbut let's be helpful and bail out early in this case.\n\nNote that the shebang being added here doesn't mean anything to a\nPOSIX OS because the files aren't executable, it's picked up by Perl's\nTAP tools. If the shebang wasn't here this would still be considered a\nPerl file for the purposes of those heuristics.\n\nI could also use the \"Bail out!\" keyword here, see\n614fe01521 (test-lib: bail out when \"-v\" used under \"prove\",\n2016-10-22) for prior art. I think it's more gentle to just print a\nSKIP notice.\n\nSigned-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\n---\n t/test-lib-functions.sh | 9 +++++++++\n t/test-lib.sh           | 9 +++++++++\n 2 files changed, 18 insertions(+)\n\ndiff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh\nindex 6348e8d733..7ba04fe399 100644\n--- a/t/test-lib-functions.sh\n+++ b/t/test-lib-functions.sh\n@@ -1,3 +1,5 @@\n+#!/bin/sh\n+#\n # Library of functions shared by all tests scripts, included by\n # test-lib.sh.\n #\n@@ -16,6 +18,13 @@\n # You should have received a copy of the GNU General Public License\n # along with this program.  If not, see http://www.gnu.org/licenses/ .\n \n+# Early bailing out on 'prove t*.sh'\n+if test -n \"$HARNESS_ACTIVE\" -a \"$0\" = \"test-lib-functions.sh\"\n+then\n+\techo \"1..0 # SKIP accidentally invoked 'prove t*.sh'?\"\n+\texit 0\n+fi\n+\n # The semantics of the editor variables are that of invoking\n # sh -c \"$EDITOR \\\"$@\\\"\" files ...\n #\ndiff --git a/t/test-lib.sh b/t/test-lib.sh\nindex d3f6af6a65..7ceae9a38d 100644\n--- a/t/test-lib.sh\n+++ b/t/test-lib.sh\n@@ -1,3 +1,5 @@\n+#!/bin/sh\n+#\n # Test framework for git.  See t/README for usage.\n #\n # Copyright (c) 2005 Junio C Hamano\n@@ -15,6 +17,13 @@\n # You should have received a copy of the GNU General Public License\n # along with this program.  If not, see http://www.gnu.org/licenses/ .\n \n+# Early bailing out on 'prove t*.sh'\n+if test -n \"$HARNESS_ACTIVE\" -a \"$0\" = \"test-lib.sh\"\n+then\n+\techo \"1..0 # SKIP accidentally invoked 'prove t*.sh'?\"\n+\texit 0\n+fi\n+\n # Test the binaries we have just built.  The tests are kept in\n # t/ subdirectory and are run in 'trash directory' subdirectory.\n if test -z \"$TEST_DIRECTORY\"\n-- \n2.31.0.rc2.211.g1d0b8788b3\n\n"},{"id":"418883","messageId":"xmqqwnudegse.fsf@gitster.g","threadId":"55289","inReplyTo":"20210311124216.25036-1-avarab@gmail.com","subject":"Re: [PATCH] test-lib: guard against being run as 'prove t*.sh'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-03-11T17:33:53Z","receivedAt":"2021-03-11T17:35:00Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:\n\n> We have t/Makefile targets to properly run the tests with prove,\n> however it's also just as valid to run them as e.g.:\n>\n>     prove t[0-9]*.sh\n>\n> But if they're run as:\n>\n>     prove t*.sh\n\nI am not sure if the above is a good enough excuse to diverge from\nour convention not to add #! to signal that a scriptlet is not to\nbe used standalone but is meant to be dot-sourced.  At least to me,\nthis change looks like making the codebase worse to protect from a\nmistake that we should not have to worry about in real life.\n\nBut that may be because my use of prove is limited to a more\ntargetted and narrow set of tests (i.e. either use \"t4???-*.sh\" for\nan area, or a single test---anything broader I'd use \"make\").\n"}]}