git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] test-lib: check Bash version for '-x' without using shell arrays
@ 2019-01-01 23:19 SZEDER Gábor
  2019-01-02  0:20 ` Johannes Sixt
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: SZEDER Gábor @ 2019-01-01 23:19 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Max Kirillov, Carlo Arenas, Jeff King, git, SZEDER Gábor

One of our test scripts, 't1510-repo-setup.sh' [1], still can't be
reliably run with '-x' tracing enabled, unless it's executed with a
Bash version supporting BASH_XTRACEFD (since v4.1).  We have a lengthy
condition to check the version of the shell running the test script,
and disable tracing if it's not executed with a suitable Bash version
[2].

This condition uses non-portable shell array accesses to easily get
Bash's major and minor version number.  This didn't seem to be
problematic, because the simple commands expanding those array
accesses are only executed when the test script is actually run with
Bash.  When run with Dash, the only shell I have at hand that doesn't
support shell arrays, there are no issues, as it apparently skips
right over the non-executed simple commands without noticing the
non-supported constructs.

Alas, it has been reported that NetBSD's /bin/sh does complain about
them:

  ./test-lib.sh: 327: Syntax error: Bad substitution

where line 327 contains the first ${BASH_VERSINFO[0]} array access.

To my understanding both shells are right and conform to POSIX,
because the standard allows both behavior by stating the following
under '2.8.1 Consequences of Shell Errors':

  "An expansion error is one that occurs when the shell expansions
  define in wordexp are carried out (for example, "${x!y}", because
  '!' is not a valid operator); an implementation may treat these as
  syntax errors if it is able to detect them during tokenization,
  rather than during expansion."

Avoid this issue with NetBSD's /bin/sh (and potentially with other,
less common shells) by using a couple of parameter expansions to
extract the major and minor version numbers from $BASH_VERSION.

[1] 5827506928 (t1510-repo-setup: mark as untraceable with '-x',
    2018-02-24)
[2] 5fc98e79fc (t: add means to disable '-x' tracing for individual
    test scripts, 2018-02-24)

Reported-by: Max Kirillov <max@max630.net>
Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---

This will certainly conflict with patches 3 and 6 in my stress testing
patch series at

  https://public-inbox.org/git/20181230191629.3232-1-szeder.dev@gmail.com/T/#u


 t/test-lib.sh | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/t/test-lib.sh b/t/test-lib.sh
index 0f1faa24b2..f47a191e3b 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -324,9 +324,12 @@ do
 		# isn't executed with a suitable Bash version.
 		if test -z "$test_untraceable" || {
 		     test -n "$BASH_VERSION" && {
-		       test ${BASH_VERSINFO[0]} -gt 4 || {
-			 test ${BASH_VERSINFO[0]} -eq 4 &&
-			 test ${BASH_VERSINFO[1]} -ge 1
+		       bash_major=${BASH_VERSION%%.*}
+		       bash_minor=${BASH_VERSION#*.}
+		       bash_minor=${bash_minor%%.*}
+		       test $bash_major -gt 4 || {
+			 test $bash_major -eq 4 &&
+			 test $bash_minor -ge 1
 		       }
 		     }
 		   }
-- 
2.20.1.151.gec613c4b75


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2019-01-04 20:05 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-01 23:19 [PATCH] test-lib: check Bash version for '-x' without using shell arrays SZEDER Gábor
2019-01-02  0:20 ` Johannes Sixt
2019-01-02 16:46   ` Junio C Hamano
2019-01-03 11:43     ` [PATCH v2] " SZEDER Gábor
2019-01-03 20:29       ` Junio C Hamano
2019-01-04  9:30         ` SZEDER Gábor
2019-01-04 11:25           ` Junio C Hamano
2019-01-04 12:38             ` SZEDER Gábor
2019-01-04 18:42               ` Carlo Arenas
2019-01-04 20:04                 ` Junio C Hamano
2019-01-03  4:52   ` [PATCH] " Jeff King
2019-01-02 18:05 ` Eric Sunshine
2019-01-02 18:31   ` Carlo Arenas
2019-01-03 11:36 ` SZEDER Gábor

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).