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
This commit is contained in:
Dag-Erling Smørgrav 2024-10-07 23:00:17 +02:00
parent c59166e5b4
commit 334af5e413
4 changed files with 113 additions and 0 deletions

View File

@ -1097,6 +1097,8 @@
..
du
..
env
..
factor
..
file

View File

@ -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>

6
usr.bin/env/tests/Makefile vendored Normal file
View File

@ -0,0 +1,6 @@
PACKAGE= tests
ATF_TESTS_SH= env_test
BINDIR= ${TESTSDIR}
.include <bsd.test.mk>

100
usr.bin/env/tests/env_test.sh vendored Normal file
View File

@ -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
}