From nobody Tue Oct 15 06:30:28 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XSPNh38QMz5YQcQ; Tue, 15 Oct 2024 06:30:28 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XSPNh1SfFz4tT9; Tue, 15 Oct 2024 06:30:28 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728973828; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LE3pbPYlVJRzepWe9ADVkXroriKq6kEs33Pf6gwldns=; b=gEbbgl3FCY+5Rj3+d5VKGpV8JJRlxxyn23Kx4fb6KuArC8gcNQFGyrZ19+0/np4z7WkWE0 /7c9s0IdDiYM61TDYUJg1NfJddEJFkGKfIHf11ogYNsmg8yLhC6w1/ajmzKBVuFQgwJjgd DcG26cUh2O/b4MxeP+LgsNkogqZzOJD1rbJl40YYUMJ9qF2Bc2YV7PxJnTuFKMm7QLps8V lTesu6R2OuqlSEwoumFdR8QvW7TMb6Ap09f1cVlMLIWtWjDpc2j2SSCVYCRMSFW5EKjY99 6R/n2rYVvP0iopkAEQAzHPXqqPTqiPWaJ1VFQPeZEkaiIWxMWHjXh+2NR0tIkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1728973828; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=LE3pbPYlVJRzepWe9ADVkXroriKq6kEs33Pf6gwldns=; b=csLW1jVhKgaJ9/DlLCIPkJCG9ujHPCfzVOPxt+yHpWJNmFp33ftoOC6jTdhNE80LaaoNdb PwmY6LXcbczGqqF2XJPbeDfSiUeszrqOhe7aI0MwpdjlUfo/i+JknxtZyGF/wJdu8JxlMx FjthaNZNn8cDAc5sDZQ7NMesczJd5OHdyfrN5YXzHwqKRH6CyUrugp62KLYWCywEG9bHh7 GTZFI4U4E7T8LCuNRJk8OBKVgEgT+J8kp6SHuDH2yootKCnMSf9+o3Dw37nyGdkGwhw5g+ xnlWYVjP9+CZpBeuKFZ5pohcxK1XGGt5vKRGlrcWbOZ2SrBAup3GOx/jzvHDqg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1728973828; a=rsa-sha256; cv=none; b=wQ8jUQ+6budyQ1Rewp5HxFQ+gqMTYng8wbiFlTTAUM+db6Aw2/kDPk7TGE41Br5qIRrVRu UCYMknDX1iUm67pdhb11ct1kX2x2qNSBnuMoqhm2/+3S9I6LqvildxjW+JzLpm9J7/9HAh id8afYZtozBiESfrVHKC7qk7/Q9J/VqVEWeIgbqb9rQMfDmpLXWLFw5k7NxWIbQZyigf24 ICNMOqc0u2n4XogTzoYZraqntkog7sx1StL/2SihNH31g3x+YEJd0DDB6/lWGou82xYsWk nsBSrwkDJ90jpYMCXqL/QjrXx1GvR+mta159kcUqfkEjqZdcgWGoPNb0rbEHbA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XSPNh0ss0z19vH; Tue, 15 Oct 2024 06:30:28 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 49F6USi3016162; Tue, 15 Oct 2024 06:30:28 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 49F6USpt016159; Tue, 15 Oct 2024 06:30:28 GMT (envelope-from git) Date: Tue, 15 Oct 2024 06:30:28 GMT Message-Id: <202410150630.49F6USpt016159@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 80c9caa46b1e - stable/13 - env: Add a handful of test cases. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 80c9caa46b1ec89cca4138a9c8da4fb6b54fbbf7 Auto-Submitted: auto-generated The branch stable/13 has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=80c9caa46b1ec89cca4138a9c8da4fb6b54fbbf7 commit 80c9caa46b1ec89cca4138a9c8da4fb6b54fbbf7 Author: Dag-Erling Smørgrav AuthorDate: 2024-10-07 21:00:17 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-10-15 05:26:12 +0000 env: Add a handful of test cases. MFC after: 3 days Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D46996 (cherry picked from commit 334af5e4131b21c658203635bf713d6a59846585) env: Improve documentation. * The `env` utility's inability to run a command whose name contains an equal sign is a feature, not a bug, so move that paragraph up from the BUGS section to the DESCRIPTION section. * Mention that this can be worked around by prefixing the command name with `command`, and add an example of this to the EXAMPLE section. * Add a test case which verifies that `env` does not run a command with an equal sign in its name even if it exists, and also demonstrates the workaround. MFC after: 3 days Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D46997 (cherry picked from commit a0dfb0668b45506de97beb4c7acbe3fd1ba69fc8) env: Check the status of stdout. MFC after: 3 days Sponsored by: Klara, Inc. Reviewed by: markj Differential Revision: https://reviews.freebsd.org/D47009 (cherry picked from commit c2d93a803acef634bd0eede6673aeea59e90c277) --- etc/mtree/BSD.tests.dist | 2 + usr.bin/env/Makefile | 4 ++ usr.bin/env/env.1 | 28 ++++++--- usr.bin/env/env.c | 2 + usr.bin/env/tests/Makefile | 6 ++ usr.bin/env/tests/env_test.sh | 137 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 170 insertions(+), 9 deletions(-) diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index f4b70e543d7b..c2d99ae33e30 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -1029,6 +1029,8 @@ .. du .. + env + .. file2c .. file diff --git a/usr.bin/env/Makefile b/usr.bin/env/Makefile index af2e65dd5fc1..d7a34fce92aa 100644 --- a/usr.bin/env/Makefile +++ b/usr.bin/env/Makefile @@ -1,4 +1,5 @@ # From: @(#)Makefile 8.1 (Berkeley) 6/6/93 +.include PACKAGE= runtime PROG= env @@ -6,4 +7,7 @@ SRCS= env.c envopts.c LIBADD= util +HAS_TESTS= +SUBDIR.${MK_TESTS}= tests + .include diff --git a/usr.bin/env/env.1 b/usr.bin/env/env.1 index 8aa78e110505..6858a6465b97 100644 --- a/usr.bin/env/env.1 +++ b/usr.bin/env/env.1 @@ -30,7 +30,7 @@ .\" From @(#)printenv.1 8.1 (Berkeley) 6/6/93 .\" From FreeBSD: src/usr.bin/printenv/printenv.1,v 1.17 2002/11/26 17:33:35 ru Exp .\" -.Dd March 3, 2021 +.Dd October 7, 2024 .Dt ENV 1 .Os .Sh NAME @@ -173,6 +173,19 @@ Both and .Ar utility may not be specified together. +.Pp +The +.Nm +utility does not handle values of +.Ar utility +which have an equals sign +.Pq Ql = +in their name, for obvious reasons. +This can easily be worked around by interposing the +.Xr command 1 +utility, which simply executes its arguments; see +.Sx EXAMPLES +below. .\" .Ss Details of -S (split-string) processing The processing of the @@ -471,6 +484,11 @@ and options: .Pp .Dl "#!/usr/bin/env -S-P/usr/local/bin:/usr/bin:${PATH} perl" +.Pp +To execute a utility with an equal sign in its name: +.Bd -literal -offset indent +env name=value ... command foo=bar arg ... +.Ed .Sh COMPATIBILITY The .Nm @@ -516,14 +534,6 @@ options were added in .Sh BUGS The .Nm -utility does not handle values of -.Ar utility -which have an equals sign -.Pq Ql = -in their name, for obvious reasons. -.Pp -The -.Nm utility does not take multibyte characters into account when processing the .Fl S diff --git a/usr.bin/env/env.c b/usr.bin/env/env.c index 8f2f0369c56b..3b7627e534e4 100644 --- a/usr.bin/env/env.c +++ b/usr.bin/env/env.c @@ -215,6 +215,8 @@ main(int argc, char **argv) } for (ep = environ; *ep; ep++) (void)printf("%s%c", *ep, term); + if (fflush(stdout) != 0) + err(1, "stdout"); exit(0); } diff --git a/usr.bin/env/tests/Makefile b/usr.bin/env/tests/Makefile new file mode 100644 index 000000000000..3d2f77b34b67 --- /dev/null +++ b/usr.bin/env/tests/Makefile @@ -0,0 +1,6 @@ +PACKAGE= tests + +ATF_TESTS_SH= env_test +BINDIR= ${TESTSDIR} + +.include diff --git a/usr.bin/env/tests/env_test.sh b/usr.bin/env/tests/env_test.sh new file mode 100644 index 000000000000..a3e05c050216 --- /dev/null +++ b/usr.bin/env/tests/env_test.sh @@ -0,0 +1,137 @@ +# +# Copyright (c) 2024 Klara, Inc. +# +# SPDX-License-Identifier: BSD-2-Clause +# + +magic_words="Squeamish $$ Ossifrage" + +atf_test_case basic +basic_head() +{ + atf_set "descr" "Basic test case" +} +basic_body() +{ + atf_check -o match:"^magic_words=${magic_words}\$" \ + env magic_words="${magic_words}" + export MAGIC_WORDS="${magic_words}" + atf_check -o match:"^MAGIC_WORDS=${magic_words}\$" \ + env + unset MAGIC_WORDS +} + +atf_test_case unset +unset_head() +{ + atf_set "descr" "Unset a variable" +} +unset_body() +{ + export MAGIC_WORDS="${magic_words}" + atf_check -o not-match:"^MAGIC_WORDS=" \ + env -u MAGIC_WORDS + unset MAGIC_WORDS +} + +atf_test_case empty +empty_head() +{ + atf_set "descr" "Empty environment" +} +empty_body() +{ + atf_check env -i +} + +atf_test_case true +true_head() +{ + atf_set "descr" "Run true" +} +true_body() +{ + atf_check env true +} + +atf_test_case false +false_head() +{ + atf_set "descr" "Run false" +} +false_body() +{ + atf_check -s exit:1 env false +} + +atf_test_case false +false_head() +{ + atf_set "descr" "Run false" +} +false_body() +{ + atf_check -s exit:1 env false +} + +atf_test_case altpath +altpath_head() +{ + atf_set "descr" "Use alternate path" +} +altpath_body() +{ + echo "echo ${magic_words}" >magic_words + chmod 0755 magic_words + atf_check -s exit:127 -e match:"No such file" \ + env magic_words + atf_check -o inline:"${magic_words}\n" \ + env -P "${PWD}" magic_words +} + +atf_test_case equal +equal_head() +{ + atf_set "descr" "Command name contains equal sign" +} +equal_body() +{ + echo "echo ${magic_words}" >"magic=words" + chmod 0755 "magic=words" + atf_check -o match:"^${PWD}/magic=words$" \ + env "${PWD}/magic=words" + atf_check -o match:"^magic=words$" \ + env -P "${PATH}:${PWD}" "magic=words" + atf_check -o inline:"${magic_words}\n" \ + env command "${PWD}/magic=words" + atf_check -o inline:"${magic_words}\n" \ + env PATH="${PATH}:${PWD}" command "magic=words" +} + +atf_test_case stdout +stdout_head() +{ + atf_set descr "Failure to write to stdout" +} +stdout_body() +{ + ( + trap "" PIPE + env 2>stderr + echo $? >result + ) | true + atf_check -o inline:"1\n" cat result + atf_check -o match:"stdout" cat stderr +} + +atf_init_test_cases() +{ + atf_add_test_case basic + atf_add_test_case unset + atf_add_test_case empty + atf_add_test_case true + atf_add_test_case false + atf_add_test_case altpath + atf_add_test_case equal + atf_add_test_case stdout +}