git: 334af5e4131b - main - env: Add a handful of test cases.

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Mon, 07 Oct 2024 21:01:33 UTC
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=334af5e4131b21c658203635bf713d6a59846585

commit 334af5e4131b21c658203635bf713d6a59846585
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-10-07 21:00:17 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-10-07 21:01:09 +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
---
 etc/mtree/BSD.tests.dist      |   2 +
 usr.bin/env/Makefile          |   5 +++
 usr.bin/env/tests/Makefile    |   6 +++
 usr.bin/env/tests/env_test.sh | 100 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 113 insertions(+)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index 67dc71fbfda4..8cac5e8d55e2 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -1097,6 +1097,8 @@
         ..
         du
         ..
+        env
+        ..
         factor
         ..
         file
diff --git a/usr.bin/env/Makefile b/usr.bin/env/Makefile
index 81f18e58f80e..f284770af5bb 100644
--- a/usr.bin/env/Makefile
+++ b/usr.bin/env/Makefile
@@ -1,7 +1,12 @@
+.include <src.opts.mk>
+
 PACKAGE=	runtime
 PROG=	env
 SRCS=	env.c envopts.c
 
 LIBADD=	util
 
+HAS_TESTS=
+SUBDIR.${MK_TESTS}=	tests
+
 .include <bsd.prog.mk>
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 <bsd.test.mk>
diff --git a/usr.bin/env/tests/env_test.sh b/usr.bin/env/tests/env_test.sh
new file mode 100644
index 000000000000..7568f81ab603
--- /dev/null
+++ b/usr.bin/env/tests/env_test.sh
@@ -0,0 +1,100 @@
+#
+# 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_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
+}