Web lists-archives.com

Re: [PATCH] t3900: add some more quotes




On Wed, Jan 10, 2018 at 08:02:09PM +0100, Johannes Sixt wrote:

> > diff --git a/t/t3900-i18n-commit.sh b/t/t3900-i18n-commit.sh
> > index 9e4e694d9..dc00db87b 100755
> > --- a/t/t3900-i18n-commit.sh
> > +++ b/t/t3900-i18n-commit.sh
> > @@ -40,7 +40,7 @@ test_expect_success 'UTF-16 refused because of NULs' '
> >   '
> >   test_expect_success 'UTF-8 invalid characters refused' '
> > -	test_when_finished "rm -f \"$HOME/stderr $HOME/invalid\"" &&
> > +	test_when_finished "rm -f \"$HOME/stderr\" \"$HOME/invalid\"" &&
> 
> Should that not better be
> 
> 	test_when_finished "rm -f \"\$HOME/stderr\" \"\$HOME/invalid\""
> 
> i.e., delay the expansion of $HOME to protect against double-quotes in the
> path?

Yeah. One of the reasons for both of the errors in this thread is the
nested double-quoting. Using single quotes is awkward because we're
already using them to delimit the whole snippet.  I've often wondered if
our tests would be more readable taking the snippet over stdin.
Something like:

diff --git a/t/t3900-i18n-commit.sh b/t/t3900-i18n-commit.sh
index 9e4e694d93..09ad4d8878 100755
--- a/t/t3900-i18n-commit.sh
+++ b/t/t3900-i18n-commit.sh
@@ -39,14 +39,14 @@ test_expect_success 'UTF-16 refused because of NULs' '
 	test_must_fail git commit -a -F "$TEST_DIRECTORY"/t3900/UTF-16.txt
 '
 
-test_expect_success 'UTF-8 invalid characters refused' '
-	test_when_finished "rm -f \"$HOME/stderr $HOME/invalid\"" &&
+test_expect_success 'UTF-8 invalid characters refused' - <<\EOT
+	test_when_finished 'rm -f "$HOME/stderr $HOME/invalid"' &&
 	echo "UTF-8 characters" >F &&
 	printf "Commit message\n\nInvalid surrogate:\355\240\200\n" \
 		>"$HOME/invalid" &&
 	git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
 	test_i18ngrep "did not conform" "$HOME"/stderr
-'
+EOT
 
 test_expect_success 'UTF-8 overlong sequences rejected' '
 	test_when_finished "rm -f \"$HOME/stderr $HOME/invalid\"" &&
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
index 1701fe2a06..be8a47d304 100644
--- a/t/test-lib-functions.sh
+++ b/t/test-lib-functions.sh
@@ -391,11 +391,32 @@ test_verify_prereq () {
 	error "bug in the test script: '$test_prereq' does not look like a prereq"
 }
 
+# Read from stdin into the variable given in $1.
+test_read_to_eof () {
+	# Bash's "read" is sufficiently flexible that we can skip the extra
+	# process.
+	if test -n "$BASH_VERSION"
+	then
+		# 64k should be enough for anyone...
+		read -N 65536 -r "$1"
+	else
+		# command substitution eats trailing whitespace, so we add
+		# and then remove a non-whitespace character.
+		eval "$1=\$(cat; printf x)"
+		eval "$1=\${$1%x}"
+	fi
+}
+
 test_expect_failure () {
 	test_start_
 	test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
 	test "$#" = 2 ||
 	error "bug in the test script: not 2 or 3 parameters to test-expect-failure"
+	if test "$2" = "-"
+	then
+		test_read_to_eof test_block
+		set -- "$1" "$test_block"
+	fi
 	test_verify_prereq
 	export test_prereq
 	if ! test_skip "$@"
@@ -416,6 +437,11 @@ test_expect_success () {
 	test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
 	test "$#" = 2 ||
 	error "bug in the test script: not 2 or 3 parameters to test-expect-success"
+	if test "$2" = "-"
+	then
+		test_read_to_eof test_block
+		set -- "$1" "$test_block"
+	fi
 	test_verify_prereq
 	export test_prereq
 	if ! test_skip "$@"