xref: /freebsd/usr.bin/env/tests/env_test.sh (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
1#
2# Copyright (c) 2024 Klara, Inc.
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6
7magic_words="Squeamish $$ Ossifrage"
8
9atf_test_case basic
10basic_head()
11{
12	atf_set "descr" "Basic test case"
13}
14basic_body()
15{
16	atf_check -o match:"^magic_words=${magic_words}\$" \
17		  env magic_words="${magic_words}"
18	export MAGIC_WORDS="${magic_words}"
19	atf_check -o match:"^MAGIC_WORDS=${magic_words}\$" \
20		  env
21	unset MAGIC_WORDS
22}
23
24atf_test_case unset
25unset_head()
26{
27	atf_set "descr" "Unset a variable"
28}
29unset_body()
30{
31	export MAGIC_WORDS="${magic_words}"
32	atf_check -o not-match:"^MAGIC_WORDS=" \
33		  env -u MAGIC_WORDS
34	unset MAGIC_WORDS
35}
36
37atf_test_case empty
38empty_head()
39{
40	atf_set "descr" "Empty environment"
41}
42empty_body()
43{
44	atf_check env -i
45}
46
47atf_test_case true
48true_head()
49{
50	atf_set "descr" "Run true"
51}
52true_body()
53{
54	atf_check env true
55}
56
57atf_test_case false
58false_head()
59{
60	atf_set "descr" "Run false"
61}
62false_body()
63{
64	atf_check -s exit:1 env false
65}
66
67atf_test_case false
68false_head()
69{
70	atf_set "descr" "Run false"
71}
72false_body()
73{
74	atf_check -s exit:1 env false
75}
76
77atf_test_case altpath
78altpath_head()
79{
80	atf_set "descr" "Use alternate path"
81}
82altpath_body()
83{
84	echo "echo ${magic_words}" >magic_words
85	chmod 0755 magic_words
86	atf_check -s exit:125 -e match:"must specify command" \
87		  env -P "${PWD}"
88	atf_check -s exit:127 -e match:"No such file" \
89		  env magic_words
90	atf_check -o inline:"${magic_words}\n" \
91		  env -P "${PWD}" magic_words
92}
93
94atf_test_case equal
95equal_head()
96{
97	atf_set "descr" "Command name contains equal sign"
98}
99equal_body()
100{
101	echo "echo ${magic_words}" >"magic=words"
102	chmod 0755 "magic=words"
103	atf_check -o match:"^${PWD}/magic=words$" \
104		  env "${PWD}/magic=words"
105	atf_check -s exit:125 -e match:"must specify command" \
106		  env -P "${PATH}:${PWD}" "magic=words"
107	atf_check -o inline:"${magic_words}\n" \
108		  env command "${PWD}/magic=words"
109	atf_check -o inline:"${magic_words}\n" \
110		  env PATH="${PATH}:${PWD}" command "magic=words"
111}
112
113atf_test_case chdir
114chdir_head()
115{
116	atf_set "descr" "Change working directory"
117}
118chdir_body()
119{
120	local subdir="dir.$$"
121	atf_check -o inline:"${PWD}\n" \
122		  env pwd
123	atf_check -s exit:125 -e match:"must specify command" \
124		  env -C "${subdir}"
125	atf_check -s exit:125 \
126		  -e match:"cannot change directory to '${subdir}':" \
127		  env -C "${subdir}" pwd
128	atf_check mkdir "${subdir}"
129	atf_check -o inline:"${PWD}/${subdir}\n" \
130		  env -C "${subdir}" pwd
131}
132
133atf_test_case stdout
134stdout_head()
135{
136	atf_set descr "Failure to write to stdout"
137}
138stdout_body()
139{
140	(
141		trap "" PIPE
142		env 2>stderr
143		echo $? >result
144	) | true
145	atf_check -o inline:"1\n" cat result
146	atf_check -o match:"stdout" cat stderr
147}
148
149atf_init_test_cases()
150{
151	atf_add_test_case basic
152	atf_add_test_case unset
153	atf_add_test_case empty
154	atf_add_test_case true
155	atf_add_test_case false
156	atf_add_test_case altpath
157	atf_add_test_case equal
158	atf_add_test_case chdir
159	atf_add_test_case stdout
160}
161