diff mbox series

[02/20] t: refactor environment sanitization to not use Perl

Message ID 20250320-b4-pks-t-perlless-v1-2-b1eefe27ac55@pks.im (mailing list archive)
State Superseded
Headers show
Series t: drop Perl as a mandatory prerequisite | expand

Commit Message

Patrick Steinhardt March 20, 2025, 9:35 a.m. UTC
Before executing tests we first sanitize the environment. Part of the
sanitization is to unset a couple of environment variables that we know
will change the behaviour of Git. This is done with a small Perl script,
which has the consequence that having a Perl interpreter available is a
strict requirement for running our unit tests.

The logic itself isn't particularly involved: we simply unset every
environment variable whose key starts with 'GIT_', but then explicitly
allow a subset of these.

Refactor the logic to instead use sed(1) so that it becomes possible to
execute our tests without Perl.

Based-on-patch-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 t/test-lib.sh | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)

Comments

Karthik Nayak March 21, 2025, 9:52 a.m. UTC | #1
Patrick Steinhardt <ps@pks.im> writes:

> Before executing tests we first sanitize the environment. Part of the
> sanitization is to unset a couple of environment variables that we know
> will change the behaviour of Git. This is done with a small Perl script,
> which has the consequence that having a Perl interpreter available is a
> strict requirement for running our unit tests.
>
> The logic itself isn't particularly involved: we simply unset every
> environment variable whose key starts with 'GIT_', but then explicitly
> allow a subset of these.
>
> Refactor the logic to instead use sed(1) so that it becomes possible to
> execute our tests without Perl.
>
> Based-on-patch-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  t/test-lib.sh | 32 ++++++++++++++------------------
>  1 file changed, 14 insertions(+), 18 deletions(-)
>
> diff --git a/t/test-lib.sh b/t/test-lib.sh
> index 1ce3b32fcac..a62699d6c79 100644
> --- a/t/test-lib.sh
> +++ b/t/test-lib.sh
> @@ -499,24 +499,20 @@ EDITOR=:
>  # /usr/xpg4/bin/sh and /bin/ksh to bail out.  So keep the unsets
>  # deriving from the command substitution clustered with the other
>  # ones.
> -unset VISUAL EMAIL LANGUAGE $("$PERL_PATH" -e '
> -	my @env = keys %ENV;
> -	my $ok = join("|", qw(
> -		TRACE
> -		DEBUG
> -		TEST
> -		.*_TEST
> -		PROVE
> -		VALGRIND
> -		UNZIP
> -		PERF_
> -		CURL_VERBOSE
> -		TRACE_CURL
> -		BUILD_DIR
> -	));
> -	my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
> -	print join("\n", @vars);
> -')
> +unset VISUAL EMAIL LANGUAGE $(env | sed -n \
> +	-e '/^GIT_TRACE/d' \
> +	-e '/^GIT_DEBUG/d' \
> +	-e '/^GIT_TEST/d' \
> +	-e '/^GIT_.*_TEST/d' \
> +	-e '/^GIT_PROVE/d' \
> +	-e '/^GIT_VALGRIND/d' \
> +	-e '/^GIT_UNZIP/d' \
> +	-e '/^GIT_PERF_/d' \
> +	-e '/^GIT_CURL_VERBOSE/d' \
> +	-e '/^GIT_TRACE_CURL/d' \
> +	-e '/^GIT_BUILD_DIR/d' \

So we delete all of the following from the stream of env.

> +	-e 's/^\(GIT_[^=]*\)=.*/\1/p'

But any other `GIT_*` env variable is printed to be `unset`. Ok makes
sense. This is much more readable than the perl version!

> +)
>  unset XDG_CACHE_HOME
>  unset XDG_CONFIG_HOME
>  unset GITPERLLIB
>
> --
> 2.49.0.472.ge94155a9ec.dirty
diff mbox series

Patch

diff --git a/t/test-lib.sh b/t/test-lib.sh
index 1ce3b32fcac..a62699d6c79 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -499,24 +499,20 @@  EDITOR=:
 # /usr/xpg4/bin/sh and /bin/ksh to bail out.  So keep the unsets
 # deriving from the command substitution clustered with the other
 # ones.
-unset VISUAL EMAIL LANGUAGE $("$PERL_PATH" -e '
-	my @env = keys %ENV;
-	my $ok = join("|", qw(
-		TRACE
-		DEBUG
-		TEST
-		.*_TEST
-		PROVE
-		VALGRIND
-		UNZIP
-		PERF_
-		CURL_VERBOSE
-		TRACE_CURL
-		BUILD_DIR
-	));
-	my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
-	print join("\n", @vars);
-')
+unset VISUAL EMAIL LANGUAGE $(env | sed -n \
+	-e '/^GIT_TRACE/d' \
+	-e '/^GIT_DEBUG/d' \
+	-e '/^GIT_TEST/d' \
+	-e '/^GIT_.*_TEST/d' \
+	-e '/^GIT_PROVE/d' \
+	-e '/^GIT_VALGRIND/d' \
+	-e '/^GIT_UNZIP/d' \
+	-e '/^GIT_PERF_/d' \
+	-e '/^GIT_CURL_VERBOSE/d' \
+	-e '/^GIT_TRACE_CURL/d' \
+	-e '/^GIT_BUILD_DIR/d' \
+	-e 's/^\(GIT_[^=]*\)=.*/\1/p'
+)
 unset XDG_CACHE_HOME
 unset XDG_CONFIG_HOME
 unset GITPERLLIB