Refactored environment expectations to support grabbing flags
parent
331a1161b2
commit
302977eb74
|
@ -24,27 +24,18 @@ __INC_EXPECT_ENV=1
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Expect that an environment variable is set to a particular value
|
# Expect that an environment variable is set to a particular value, or
|
||||||
|
# assert on flags
|
||||||
#
|
#
|
||||||
# The variable need not be exported.
|
__expect-env()
|
||||||
#
|
|
||||||
_expect--set()
|
|
||||||
{
|
{
|
||||||
local -ri shiftn="$2"
|
local -r flag="$1" var="$2" cmp="$3"
|
||||||
local -r envpath="$4"
|
shift 3
|
||||||
shift "$shiftn"
|
|
||||||
|
|
||||||
# ensure envpath is available
|
|
||||||
__chk-shiftn 4 "$shiftn"
|
|
||||||
|
|
||||||
local -r var="$1" cmp="$2"
|
|
||||||
shift 2
|
|
||||||
local -r expect="$@"
|
local -r expect="$@"
|
||||||
|
|
||||||
# TODO: support escaped newlines; use awk (do not source the file)
|
# TODO: support escaped newlines
|
||||||
local -r line="$( grep "^declare \.*-- $var=" "$envpath" )"
|
local flags val
|
||||||
local -r valq="${line##*=\"}"
|
__read-env-line "$var" flags val < "$envpath"
|
||||||
local -r val="${valq%%\"}"
|
|
||||||
|
|
||||||
# cannot quote regex without causing problems, and [[ syntax does not
|
# cannot quote regex without causing problems, and [[ syntax does not
|
||||||
# allow a variable comparison operator; further, argument order varies
|
# allow a variable comparison operator; further, argument order varies
|
||||||
|
@ -65,6 +56,42 @@ _expect--set()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
##
|
||||||
|
# Parse environment line (from `declare`) into flag and value variables
|
||||||
|
#
|
||||||
|
# Expected output is of the form:
|
||||||
|
# declare -flags? -- var="val"
|
||||||
|
#
|
||||||
|
__read-env-line()
|
||||||
|
{
|
||||||
|
local -r var="$1" destflag="$2" destval="$3"
|
||||||
|
|
||||||
|
read $destflag $destval < <(
|
||||||
|
awk '
|
||||||
|
match( $0, /^declare (-([a-z]+) )?-- '$var'="(.*)"$/, m ) {
|
||||||
|
print "-" m[2], m[3]
|
||||||
|
}'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
##
|
||||||
|
# Expect that an environment variable has been set to a certain value
|
||||||
|
#
|
||||||
|
_expect--set()
|
||||||
|
{
|
||||||
|
local -ri shiftn="$2"
|
||||||
|
local -r envpath="$4"
|
||||||
|
shift "$shiftn"
|
||||||
|
|
||||||
|
# ensure envpath is available
|
||||||
|
__chk-shiftn 4 "$shiftn"
|
||||||
|
|
||||||
|
# no flag expectation
|
||||||
|
__expect-env '' "$@"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Alias for `set`
|
# Alias for `set`
|
||||||
#
|
#
|
||||||
|
|
|
@ -33,6 +33,7 @@ function setchk()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# we have a few different expectations that have the same syntax
|
||||||
for name in set declare; do
|
for name in set declare; do
|
||||||
curchk=$name
|
curchk=$name
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue