envtest.t revision 171525
1171195Sscf#!/bin/sh
2171195Sscf#
3171195Sscf# Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org>
4171195Sscf# All rights reserved.
5171195Sscf# 
6171195Sscf# Redistribution and use in source and binary forms, with or without
7171195Sscf# modification, are permitted provided that the following conditions
8171195Sscf# are met:
9171195Sscf# 1. Redistributions of source code must retain the above copyright
10171195Sscf#    notice, this list of conditions and the following disclaimer,
11171195Sscf#    without modification, immediately at the beginning of the file.
12171195Sscf# 2. Redistributions in binary form must reproduce the above copyright
13171195Sscf#    notice, this list of conditions and the following disclaimer in the
14171195Sscf#    documentation and/or other materials provided with the distribution.
15171195Sscf# 
16171195Sscf# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17171195Sscf# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18171195Sscf# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19171195Sscf# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20171195Sscf# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21171195Sscf# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22171195Sscf# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23171195Sscf# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24171195Sscf# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25171195Sscf# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26171195Sscf#
27171195Sscf# $FreeBSD: head/tools/regression/environ/envtest.t 171525 2007-07-20 23:30:13Z scf $
28171195Sscf
29171195Sscf
30171195Sscf# Initialization.
31171195Sscftestndx=0
32171195Sscf
33171195Sscf
34171195Sscf# Testing function.
35171195Sscfrun_test()
36171195Sscf{
37171195Sscf	lasttest="${@}"
38171195Sscf	result=`./envctl -t "${@}"`
39171195Sscf
40171195Sscf	if [ ${?} -ne 0 ]
41171195Sscf	then
42171195Sscf		echo "Test program failed" >&2
43171195Sscf		exit 1
44171195Sscf	fi
45171195Sscf
46171195Sscf	return
47171195Sscf}
48171195Sscf
49171195Sscf
50171195Sscf# Perform test on results.
51171195Sscfcheck_result()
52171195Sscf{
53171195Sscf	testndx=$((testndx + 1))
54171195Sscf
55171195Sscf	echo "${result}" | sed 's/[ \t]*$//' | grep -q "^${@}$"
56171195Sscf	if [ ${?} -eq 0 ]
57171195Sscf	then
58171195Sscf		echo "ok ${testndx}"
59171195Sscf	else
60171195Sscf		echo "not ok ${testndx} - '${lasttest}'"
61171195Sscf	fi
62171195Sscf
63171195Sscf	return
64171195Sscf}
65171195Sscf
66171195Sscf
67171195Sscf#
68171195Sscf# Regression tests
69171195Sscf#
70171195Sscf
71171195Sscf# Setup environment for tests.
72171195Sscfreadonly BAR="bar"
73171195Sscfreadonly NEWBAR="newbar"
74171195Sscfexport FOO=${BAR}
75171195Sscf
76171195Sscf
77171195Sscf# Gets from environ.
78171195Sscfrun_test -g FOO
79171195Sscfcheck_result "${FOO}"
80171195Sscf
81171195Sscfrun_test -c -g FOO
82171195Sscfcheck_result ""
83171195Sscf
84171195Sscfrun_test -g FOOBAR
85171195Sscfcheck_result ""
86171195Sscf
87171195Sscfrun_test -c -g FOOBAR
88171195Sscfcheck_result ""
89171195Sscf
90171195Sscfrun_test -G
91171195Sscfcheck_result ""
92171195Sscf
93171195Sscf
94171195Sscf# Sets.
95171195Sscfrun_test -s FOO ${NEWBAR} 0 -g FOO
96171195Sscfcheck_result "0 0 ${BAR}"
97171195Sscf
98171195Sscfrun_test -s FOO ${NEWBAR} 1 -g FOO
99171195Sscfcheck_result "0 0 ${NEWBAR}"
100171195Sscf
101171195Sscfrun_test -c -s FOO ${NEWBAR} 0 -g FOO
102171195Sscfcheck_result "0 0 ${NEWBAR}"
103171195Sscf
104171195Sscfrun_test -c -s FOO ${NEWBAR} 1 -g FOO
105171195Sscfcheck_result "0 0 ${NEWBAR}"
106171195Sscf
107171195Sscfrun_test -s "FOO=" ${NEWBAR} 1 -g FOO
108171195Sscfcheck_result "-1 22 ${BAR}"
109171195Sscf
110171195Sscfrun_test -s "=FOO" ${NEWBAR} 1
111171195Sscfcheck_result "-1 22"
112171195Sscf
113171195Sscfrun_test -s "=" ${NEWBAR} 1
114171195Sscfcheck_result "-1 22"
115171195Sscf
116171195Sscfrun_test -s "" ${NEWBAR} 1
117171195Sscfcheck_result "-1 22"
118171195Sscf
119171195Sscfrun_test -S ${NEWBAR} 1
120171195Sscfcheck_result "-1 22"
121171195Sscf
122171195Sscfrun_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -g FOO
123171195Sscfcheck_result "0 0 0 0 ${BAR}"
124171195Sscf
125171195Sscfrun_test -c -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -g FOO
126171195Sscfcheck_result "0 0 0 0 ${BAR}"
127171195Sscf
128171195Sscfrun_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -s FOO ${NEWBAR} 1 -g FOO
129171195Sscfcheck_result "0 0 0 0 0 0 ${NEWBAR}"
130171195Sscf
131171195Sscfrun_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1\
132171195Sscf	-g FOO
133171195Sscfcheck_result "0 0 0 0 0 0 0 0 ${BAR}"
134171195Sscf
135171525Sscfrun_test -c -s FOO ${BAR} 1 -g FOO -c -s FOO ${NEWBAR} 1 -g FOO
136171525Sscfcheck_result "0 0 ${BAR} 0 0 ${NEWBAR}"
137171195Sscf
138171525Sscf
139171195Sscf# Unsets.
140171195Sscfrun_test -u FOO -g FOO
141171195Sscfcheck_result "0 0"
142171195Sscf
143171195Sscfrun_test -c -u FOO -g FOO
144171195Sscfcheck_result "0 0"
145171195Sscf
146171195Sscfrun_test -U
147171195Sscfcheck_result "-1 22"
148171195Sscf
149171195Sscfrun_test -u ""
150171195Sscfcheck_result "-1 22"
151171195Sscf
152171195Sscfrun_test -u "=${BAR}"
153171195Sscfcheck_result "-1 22"
154171195Sscf
155171195Sscfrun_test -c -s FOO ${NEWBAR} 1 -g FOO -u FOO -g FOO
156171195Sscfcheck_result "0 0 ${NEWBAR} 0 0"
157171195Sscf
158171525Sscfrun_test -c -u FOO -s FOO ${BAR} 1 -g FOO -u FOO -g FOO -c -u FOO\
159171525Sscf	-s FOO ${NEWBAR} 1 -g FOO
160171525Sscfcheck_result "0 0 0 0 ${BAR} 0 0  0 0 0 0 ${NEWBAR}"
161171195Sscf
162171525Sscf
163171195Sscf# Puts.
164171195Sscfrun_test -p FOO=${NEWBAR} -g FOO
165171195Sscfcheck_result "0 0 ${NEWBAR}"
166171195Sscf
167171195Sscfrun_test -c -p FOO=${NEWBAR} -g FOO
168171195Sscfcheck_result "0 0 ${NEWBAR}"
169171195Sscf
170171195Sscfrun_test -p FOO -g FOO
171171195Sscfcheck_result "-1 22 ${BAR}"
172171195Sscf
173171195Sscfrun_test -p FOO=${BAR} -p FOO=${NEWBAR} -g FOO
174171195Sscfcheck_result "0 0 0 0 ${NEWBAR}"
175171195Sscf
176171195Sscfrun_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -g FOO
177171195Sscfcheck_result "0 0 0 0 ${NEWBAR}"
178171195Sscf
179171195Sscfrun_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -g FOO
180171195Sscfcheck_result "0 0 0 0 ${BAR}"
181171195Sscf
182171195Sscfrun_test -p FOO=${BAR} -u FOO
183171195Sscfcheck_result "0 0 0 0"
184171195Sscf
185171195Sscfrun_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -u FOO
186171195Sscfcheck_result "0 0 0 0 0 0"
187171195Sscf
188171195Sscfrun_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -u FOO
189171195Sscfcheck_result "0 0 0 0 0 0"
190171525Sscf
191171525Sscfrun_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -c -g FOO -p FOO=${NEWBAR} -g FOO
192171525Sscfcheck_result "0 0 0 0  0 0 ${NEWBAR}"
193171525Sscf
194171525Sscfrun_test -c -p FOO=${BAR} -g FOO -c -p FOO=${NEWBAR} -g FOO
195171525Sscfcheck_result "0 0 ${BAR} 0 0 ${NEWBAR}"
196171525Sscf
197171525Sscf
198171525Sscf# environ replacements.
199171525Sscfrun_test -r -g FOO -s FOO ${BAR} 1 -g FOO -u FOO -g FOO
200171525Sscfcheck_result "${BAR} 0 0 ${BAR} 0 0"
201171525Sscf
202171525Sscfrun_test -r -g FOO -u FOO -g FOO -s FOO ${BAR} 1 -g FOO
203171525Sscfcheck_result "${BAR} 0 0  0 0 ${BAR}"
204