aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/env/tests/env_test.sh
blob: da238caaf7fa59abc0ea0d38510f43d17b6e174b (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
#
# 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_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 -o match:"^magic=words$" \
		  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_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
}