From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1660889-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 931AA159C9B
	for <garchives@archives.gentoo.org>; Sun, 11 Aug 2024 10:11:14 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id C26932BC056;
	Sun, 11 Aug 2024 10:11:13 +0000 (UTC)
Received: from smtp.gentoo.org (woodpecker.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 97C632BC056
	for <gentoo-commits@lists.gentoo.org>; Sun, 11 Aug 2024 10:11:13 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id E04B3340BEF
	for <gentoo-commits@lists.gentoo.org>; Sun, 11 Aug 2024 10:11:12 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id 03A731EE0
	for <gentoo-commits@lists.gentoo.org>; Sun, 11 Aug 2024 10:11:10 +0000 (UTC)
From: "Sam James" <sam@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" <sam@gentoo.org>
Message-ID: <1723371057.1c8a0a340de9b4e9b69e3e373bb8f55e2a1f030b.sam@gentoo>
Subject: [gentoo-commits] proj/gentoo-functions:master commit in: /
X-VCS-Repository: proj/gentoo-functions
X-VCS-Files: test-functions
X-VCS-Directories: /
X-VCS-Committer: sam
X-VCS-Committer-Name: Sam James
X-VCS-Revision: 1c8a0a340de9b4e9b69e3e373bb8f55e2a1f030b
X-VCS-Branch: master
Date: Sun, 11 Aug 2024 10:11:10 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: c81d0530-d8b3-4a84-9a28-6783912bd8f1
X-Archives-Hash: 005a647cd95b55400d353093805f98be

commit:     1c8a0a340de9b4e9b69e3e373bb8f55e2a1f030b
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Wed Aug  7 22:00:36 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sun Aug 11 10:10:57 2024 +0000
URL:        https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=1c8a0a34

test-functions: test for simple commands persisting environmental changes

Some implementations allow for alterations made to the execution
environment to persist beyond the scope of a simple command. Consider
loksh as a case in point.

$ f() { :; }
$ unset LEAKED
$ LEAKED=1 /bin/true; echo "LEAKED = $LEAKED"
LEAKED =
$ LEAKED=1      cmd2; echo "LEAKED = $LEAKED"
LEAKED = 1

Strictly speaking, such behaviour is permitted. The Shell Command
Language specification states:

"""
If the command name is a function that is not a standard utility
implemented as a function, variable assignments shall affect the current
execution environment during the execution of the function. It is
unspecified:

- Whether or not the variable assignments persist after the completion
  of the function

- Whether or not the variables gain the export attribute during the
  execution of the function

- Whether or not export attributes gained as a result of the variable
  assignments persist after the completion of the function (if variable
  assignments persist after the completion of the function)
"""

Unfortunately, loksh elects not to be aligned with the practices of the
overwhelming majority of implementations in this regard. For now, have
test-functions detect and abort for shells that go against the grain. I
shall consider reviewing and adapting gentoo-functions to account for
such unspecified behaviour but it is not an immediate priority.

Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 test-functions | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/test-functions b/test-functions
index 487caa9..afc56eb 100755
--- a/test-functions
+++ b/test-functions
@@ -46,6 +46,19 @@ test_local() {
 	return "${retval}"
 }
 
+test_simple_command() {
+	f() { :; }
+	LEAKED=
+	LEAKED=1 f
+	retval=0
+	if [ "${LEAKED}" ]; then
+		printf 'not '
+		retval=1
+	fi
+	printf "ok %d - /bin/sh refrains from leaking environmental changes for simple commands\\n" "$((testnum += 1))"
+	return "${retval}"
+}
+
 test_chdir() {
 	set -- \
 		ge  1          ''  \
@@ -1071,7 +1084,7 @@ if [ "${PORTAGE_BIN_PATH}" ] && [ "${S}" ]; then
 	genfun_basedir=${S}
 fi
 
-if ! test_local; then
+if ! test_local || ! test_simple_command; then
 	rc=1
 elif ! GENFUN_MODULES="portage rc" . ./functions.sh; then
 	bailout "Couldn't source ./functions.sh"