aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/env/tests/env_test.sh
blob: 2dc8f1a4c911cc9bf32bc28ad9493b3ecba656c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#
# 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:125 -e match:"must specify command" \
		  env -P "${PWD}"
	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 -s exit:125 -e match:"must specify command" \
		  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 chdir
chdir_head()
{
	atf_set "descr" "Change working directory"
}
chdir_body()
{
	local subdir="dir.$$"
	atf_check -o inline:"${PWD}\n" \
		  env pwd
	atf_check -s exit:125 -e match:"must specify command" \
		  env -C "${subdir}"
	atf_check -s exit:125 \
		  -e match:"cannot change directory to '${subdir}':" \
		  env -C "${subdir}" pwd
	atf_check mkdir "${subdir}"
	atf_check -o inline:"${PWD}/${subdir}\n" \
		  env -C "${subdir}" pwd
}

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 chdir
	atf_add_test_case stdout
}