git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] test-lib: fix non-portable pattern bracket expressions

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Feb 11, 2019, 19:58 UTC
Message-ID
<20190211195803.1682-1-szeder.dev@gmail.com>
In-Reply-To
<20190208115045.13256-1-szeder.dev@gmail.com>

Use a '!' character to start a non-matching pattern bracket expression, as specified by POSIX in Shell Command Language section 2.13.1 Patterns Matching a Single Character [1].

I used '^' instead in three places in the previous three commits, to verify that the arguments of the '--stress=' and '--stress-limit=' options and the values of various '*_PORT' environment variables are valid numbers. With certain shells, at least with dash (upstream and in Ubuntu 14.04) and mksh, this led to various undesired behaviors:

  # error message in case of a valid number
  $ ~/src/dash/src/dash ./t3903-stash.sh --stress=8
  error: --stress=<N> requires the number of jobs to run
  # not the expected error message
  $ ~/src/dash/src/dash ./t3903-stash.sh --stress=foo
  ./t3903-stash.sh: 238: test: Illegal number: foo
  # no error message at all?!
  $ mksh ./t3903-stash.sh --stress=foo
  $ echo $?
  0

Some other shells, e.g. Bash (even in posix mode), ksh, dash in Ubuntu 16.04 or later, are apparently happy to accept '^' just as well.

[1] http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_13
Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
Should go on top of 'sg/stress-test'.
 t/test-lib-functions.sh | 2 +-
 t/test-lib.sh           | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 92cf8f812c..969e2ba6da 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -1289,7 +1289,7 @@ test_set_port () {
 			port=$(($port + 10000))
 		fi
 		;;
-	*[^0-9]*|0*)
+	*[!0-9]*|0*)
 		error >&7 "invalid port number: $port"
 		;;
 	*)
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 77eff04c92..4e7cb52b57 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -144,7 +144,7 @@ do
 	--stress=*)
 		stress=${opt#--*=}
 		case "$stress" in
-		*[^0-9]*|0*|"")
+		*[!0-9]*|0*|"")
 			echo "error: --stress=<N> requires the number of jobs to run" >&2
 			exit 1
 			;;
@@ -155,7 +155,7 @@ do
 	--stress-limit=*)
 		stress_limit=${opt#--*=}
 		case "$stress_limit" in
-		*[^0-9]*|0*|"")
+		*[!0-9]*|0*|"")
 			echo "error: --stress-limit=<N> requires the number of repetitions" >&2
 			exit 1
 			;;
-- 
2.21.0.rc0.405.g6226d63c89
Previous: Jeff KingNext: Junio C Hamano
Message 8 of 13 in “test-lib: make '--stress' more bisect-friendly”
  1. test-lib: make '--stress' more bisect-friendlySZEDER Gábor, Feb 8, 2019
  2. Jeff KingFeb 8, 2019
  3. Jeff KingFeb 8, 2019
  4. SZEDER GáborFeb 8, 2019
  5. Jeff KingFeb 8, 2019
  6. SZEDER GáborFeb 8, 2019
  7. Jeff KingFeb 8, 2019
  8. test-lib: fix non-portable pattern bracket expressionsSZEDER Gábor, Feb 11, 2019
  9. Junio C HamanoFeb 11, 2019
  10. Jeff KingFeb 11, 2019
  11. SZEDER GáborFeb 12, 2019
  12. Jeff KingFeb 12, 2019
  13. Carlo ArenasFeb 12, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.