Added `exports` expectation

env
Mike Gerwitz 2014-06-10 23:33:05 -04:00
parent 7f62c27db0
commit 658fd29e0d
2 changed files with 39 additions and 7 deletions

View File

@ -29,7 +29,7 @@ __INC_EXPECT_ENV=1
#
__expect-env()
{
local -r flag="$1" var="$2" cmp="$3"
local -r expflags="$1" var="$2" cmp="$3"
shift 3
local -r expect="$@"
@ -37,6 +37,11 @@ __expect-env()
local flags val
__read-env-line "$var" flags val < "$envpath"
# perform flag assertion if requested
test -n "$expflags" && {
[[ "$flags" =~ [$expflags] ]] || return 1
}
# cannot quote regex without causing problems, and [[ syntax does not
# allow a variable comparison operator; further, argument order varies
# with certain operators; whitelist to explicitly document support and
@ -100,3 +105,22 @@ _expect--declare()
_expect--set "$@"
}
##
# Checks that a variable is exported with the given value
#
# Same syntax as `set`
#
_expect--export()
{
local -ri shiftn="$2"
local -r envpath="$4"
shift "$shiftn"
# ensure envpath is available
__chk-shiftn 4 "$shiftn"
# expect the -x flag, which denotes export
__expect-env x "$@"
}

View File

@ -20,10 +20,11 @@
##
declare -r stubenv='
declare -- var="val"
declare -- long="foo bar baz"
declare -- empty=""
declare -- one="1"'
declare -x -- var="val"
declare -x -- long="foo bar baz"
declare -x -- empty=""
declare -x -- one="1"
declare -- nonexport=""'
declare curchk
@ -34,10 +35,10 @@ function setchk()
# we have a few different expectations that have the same syntax
for name in set declare; do
for name in set declare export; do
curchk=$name
describe set
describe "$name"
describe = and == operators
it succeeds on string equality
expect setchk var = val
@ -209,6 +210,13 @@ for name in set declare; do
expect setchk var "!= foo -a 1 -eq" 1
to fail
end
if [ "$name" == export ]; then
it fails when variable is not exported
expect setchk nonexport -z
to fail
end
fi
end
done