envtest.t revision 171195
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 171195 2007-07-04 00:00:41Z 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
135171195Sscf
136171195Sscf# Unsets.
137171195Sscfrun_test -u FOO -g FOO
138171195Sscfcheck_result "0 0"
139171195Sscf
140171195Sscfrun_test -c -u FOO -g FOO
141171195Sscfcheck_result "0 0"
142171195Sscf
143171195Sscfrun_test -U
144171195Sscfcheck_result "-1 22"
145171195Sscf
146171195Sscfrun_test -u ""
147171195Sscfcheck_result "-1 22"
148171195Sscf
149171195Sscfrun_test -u "=${BAR}"
150171195Sscfcheck_result "-1 22"
151171195Sscf
152171195Sscfrun_test -c -s FOO ${NEWBAR} 1 -g FOO -u FOO -g FOO
153171195Sscfcheck_result "0 0 ${NEWBAR} 0 0"
154171195Sscf
155171195Sscf
156171195Sscf# Puts.
157171195Sscfrun_test -p FOO=${NEWBAR} -g FOO
158171195Sscfcheck_result "0 0 ${NEWBAR}"
159171195Sscf
160171195Sscfrun_test -c -p FOO=${NEWBAR} -g FOO
161171195Sscfcheck_result "0 0 ${NEWBAR}"
162171195Sscf
163171195Sscfrun_test -p FOO -g FOO
164171195Sscfcheck_result "-1 22 ${BAR}"
165171195Sscf
166171195Sscfrun_test -p FOO=${BAR} -p FOO=${NEWBAR} -g FOO
167171195Sscfcheck_result "0 0 0 0 ${NEWBAR}"
168171195Sscf
169171195Sscfrun_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -g FOO
170171195Sscfcheck_result "0 0 0 0 ${NEWBAR}"
171171195Sscf
172171195Sscfrun_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -g FOO
173171195Sscfcheck_result "0 0 0 0 ${BAR}"
174171195Sscf
175171195Sscfrun_test -p FOO=${BAR} -u FOO
176171195Sscfcheck_result "0 0 0 0"
177171195Sscf
178171195Sscfrun_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -u FOO
179171195Sscfcheck_result "0 0 0 0 0 0"
180171195Sscf
181171195Sscfrun_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -u FOO
182171195Sscfcheck_result "0 0 0 0 0 0"
183