From 658fd29e0d3f5cc1807c00184ff53338885abc23 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 10 Jun 2014 23:33:05 -0400 Subject: [PATCH] Added `exports` expectation --- src/expect/env.sh | 26 +++++++++++++++++++++++++- test/test-expect-env | 20 ++++++++++++++------ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/expect/env.sh b/src/expect/env.sh index e5879f8..252c7f8 100644 --- a/src/expect/env.sh +++ b/src/expect/env.sh @@ -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 "$@" +} + diff --git a/test/test-expect-env b/test/test-expect-env index 9e2aa8b..935f23b 100644 --- a/test/test-expect-env +++ b/test/test-expect-env @@ -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