From 334af5e4131b21c658203635bf713d6a59846585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Mon, 7 Oct 2024 23:00:17 +0200 Subject: [PATCH] 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(+) create mode 100644 usr.bin/env/tests/Makefile create mode 100644 usr.bin/env/tests/env_test.sh 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 + PACKAGE= runtime PROG= env SRCS= env.c envopts.c LIBADD= util +HAS_TESTS= +SUBDIR.${MK_TESTS}= tests + .include 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..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 +}