Added env var value expectation
parent
61cedf54f0
commit
7597677086
|
@ -23,6 +23,7 @@
|
||||||
__INC_EXPECT_CORE=1
|
__INC_EXPECT_CORE=1
|
||||||
|
|
||||||
source expect/output.sh
|
source expect/output.sh
|
||||||
|
source expect/env.sh
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# Environment expectations
|
||||||
|
#
|
||||||
|
# Copyright (C) 2014 Mike Gewitz
|
||||||
|
#
|
||||||
|
# This file is part of shspec.
|
||||||
|
#
|
||||||
|
# shspec is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
##
|
||||||
|
|
||||||
|
[ -z $__INC_EXPECT_ENV ] || return
|
||||||
|
__INC_EXPECT_ENV=1
|
||||||
|
|
||||||
|
|
||||||
|
##
|
||||||
|
# Expect that an environment variable is set to a particular value
|
||||||
|
#
|
||||||
|
# The variable need not be exported.
|
||||||
|
#
|
||||||
|
_expect--set()
|
||||||
|
{
|
||||||
|
local -ri shiftn="$2"
|
||||||
|
local -r envpath="$4"
|
||||||
|
shift "$shiftn"
|
||||||
|
|
||||||
|
# ensure envpath is available
|
||||||
|
__chk-shiftn 4 "$shiftn"
|
||||||
|
|
||||||
|
local -r var="$1" cmp="$2"
|
||||||
|
shift 2
|
||||||
|
local -r expect="$@"
|
||||||
|
|
||||||
|
# TODO: support escaped newlines; use awk (do not source the file)
|
||||||
|
local -r line="$( grep "^declare \.*-- $var=" "$envpath" )"
|
||||||
|
local -r valq="${line##*=\"}"
|
||||||
|
local -r val="${valq%%\"}"
|
||||||
|
|
||||||
|
# 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
|
||||||
|
# prevent oddities
|
||||||
|
case "$cmp" in
|
||||||
|
=~)
|
||||||
|
[[ "$val" =~ $expect ]];;
|
||||||
|
|
||||||
|
-[nz])
|
||||||
|
test "$cmp" "$val";;
|
||||||
|
|
||||||
|
=|==|!=|-eq|-ne|-lt|-le|-gt|-ge)
|
||||||
|
test "$val" $cmp "$expect";;
|
||||||
|
|
||||||
|
*) false;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
11
src/spec.sh
11
src/spec.sh
|
@ -33,7 +33,7 @@ source specstack.sh
|
||||||
source expect.sh
|
source expect.sh
|
||||||
|
|
||||||
# number of internal arguments before remainder clause
|
# number of internal arguments before remainder clause
|
||||||
declare -ir __SHIFTN=3
|
declare -ir __SHIFTN=4
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -48,6 +48,9 @@ mktemp-shm()
|
||||||
# stderr file
|
# stderr file
|
||||||
declare -r __spec_errpath="$(mktemp-shm)"
|
declare -r __spec_errpath="$(mktemp-shm)"
|
||||||
|
|
||||||
|
# env dump file
|
||||||
|
declare -r __spec_envpath="$(mktemp-shm)"
|
||||||
|
|
||||||
# most recent expect result and its exit code
|
# most recent expect result and its exit code
|
||||||
declare __spec_result=
|
declare __spec_result=
|
||||||
declare -i __spec_rexit=0
|
declare -i __spec_rexit=0
|
||||||
|
@ -162,7 +165,8 @@ to()
|
||||||
_sstack-assert-follow :expect to $(caller)
|
_sstack-assert-follow :expect to $(caller)
|
||||||
_sstack-pop
|
_sstack-pop
|
||||||
|
|
||||||
__handle-to "$__spec_rexit" $__SHIFTN "$__spec_errpath" "$@" \
|
__handle-to "$__spec_rexit" $__SHIFTN \
|
||||||
|
"$__spec_errpath" "$__spec_envpath" "$@" \
|
||||||
|| fail "$*"
|
|| fail "$*"
|
||||||
|
|
||||||
__spec_caller=
|
__spec_caller=
|
||||||
|
@ -182,6 +186,7 @@ __handle-to()
|
||||||
local -ri rexit="$1"
|
local -ri rexit="$1"
|
||||||
local -ri shiftn="$2"
|
local -ri shiftn="$2"
|
||||||
local -r errpath="$( [ $shiftn -gt 2 ] && echo "$3" )"
|
local -r errpath="$( [ $shiftn -gt 2 ] && echo "$3" )"
|
||||||
|
local -r envpath="$( [ $shiftn -gt 3 ] && echo "$4" )"
|
||||||
shift "$shiftn"
|
shift "$shiftn"
|
||||||
|
|
||||||
local -r type="$1"
|
local -r type="$1"
|
||||||
|
@ -196,7 +201,7 @@ __handle-to()
|
||||||
# output file, and all remaining arguments are said remainder clause; the
|
# output file, and all remaining arguments are said remainder clause; the
|
||||||
# shift argument allows the implementation to vary without breaking BC so
|
# shift argument allows the implementation to vary without breaking BC so
|
||||||
# long as the meaning of the shifted arguments do not change
|
# long as the meaning of the shifted arguments do not change
|
||||||
$assert $rexit $__SHIFTN "$errpath" "$@" \
|
$assert $rexit $__SHIFTN "$errpath" "$envpath" "$@" \
|
||||||
< <( echo -n "$__spec_result" )
|
< <( echo -n "$__spec_result" )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,207 @@
|
||||||
|
#!/bin/bash
|
||||||
|
# Environment expectation tests
|
||||||
|
#
|
||||||
|
# Copyright (C) 2014 Mike Gewitz
|
||||||
|
#
|
||||||
|
# This file is part of shspec.
|
||||||
|
#
|
||||||
|
# shspec is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
##
|
||||||
|
|
||||||
|
declare -r stubenv='
|
||||||
|
declare -- var="val"
|
||||||
|
declare -- long="foo bar baz"
|
||||||
|
declare -- empty=""
|
||||||
|
declare -- one="1"'
|
||||||
|
|
||||||
|
|
||||||
|
function setchk()
|
||||||
|
{
|
||||||
|
_expect--set 0 4 <(:) <( echo "$stubenv" ) "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
describe set
|
||||||
|
describe = and == operators
|
||||||
|
it succeeds on string equality
|
||||||
|
expect setchk var = val
|
||||||
|
to succeed
|
||||||
|
|
||||||
|
expect setchk var == val
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it fails on string inequality
|
||||||
|
expect setchk var = unval
|
||||||
|
to fail
|
||||||
|
|
||||||
|
expect setchk var == unval
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe != operator
|
||||||
|
it succeeds on string inequality
|
||||||
|
expect setchk var != foo
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it fails on string equality
|
||||||
|
expect setchk var != val
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe =~ operator
|
||||||
|
it succeeds on a match
|
||||||
|
expect setchk \
|
||||||
|
long =~ fo+ ba. baz\$
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
# note that this also ensures that *all* arguments are part of the match
|
||||||
|
it fails on a mismatch
|
||||||
|
expect setchk \
|
||||||
|
long =~ fo+ baX baz\$
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe -n operator
|
||||||
|
it succeeds when string is non-empty
|
||||||
|
expect setchk var -n
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it fails when string is empty
|
||||||
|
expect setchk empty -n
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe -z operator
|
||||||
|
it succeeds when string is empty
|
||||||
|
expect setchk empty -z
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it fails when string is non-empty
|
||||||
|
expect setchk var -z
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe -eq operator
|
||||||
|
it succeeds on numeric equality
|
||||||
|
expect setchk one -eq 1
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it fails on numeric inequality
|
||||||
|
expect setchk one -eq 2
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe -gt operator
|
||||||
|
it succeeds when numerically greater
|
||||||
|
expect setchk one -gt 0
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it fails when not numerically greater
|
||||||
|
expect setchk one -gt 1
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe -ge operator
|
||||||
|
it succeeds when numerically greater
|
||||||
|
expect setchk one -ge 0
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it succeeds when numerically equal
|
||||||
|
expect setchk one -ge 1
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it fails when numerically less than
|
||||||
|
expect setchk one -ge 2
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe -lt operator
|
||||||
|
it succeeds when numerically less than
|
||||||
|
expect setchk one -lt 2
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it fails when not numerically less than
|
||||||
|
expect setchk one -lt 1
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe -le operator
|
||||||
|
it succeeds when numerically less than
|
||||||
|
expect setchk one -le 2
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it succeeds when numerically equal
|
||||||
|
expect setchk one -le 1
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it fails when numerically greater than
|
||||||
|
expect setchk one -le 0
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
describe -ne operator
|
||||||
|
it succeeds when numerically unequal
|
||||||
|
expect setchk one -ne 2
|
||||||
|
to succeed
|
||||||
|
end
|
||||||
|
|
||||||
|
it fails when numerically equal
|
||||||
|
expect setchk one -ne 1
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
# primarily for safety and strict documentation, but no other tests make
|
||||||
|
# sense at the moment
|
||||||
|
it fails on unrecognized operators
|
||||||
|
# shell injection (not that this is realistically a problem, because we
|
||||||
|
# can execute arbitrary shell code anyway)
|
||||||
|
expect setchk var "!= foo -a 1 -eq" 1
|
||||||
|
to fail
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue