Skip to content

Instantly share code, notes, and snippets.

@codeskyblue
Last active January 3, 2016 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeskyblue/8417123 to your computer and use it in GitHub Desktop.
Save codeskyblue/8417123 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
#
Echo(){
echo ">>> $(date "+%Y-%m-%d %H:%M:%S")" "$@"
}
Die(){ Echo "$@"; exit 1; }
TMPSTDOUT=$(mktemp)
TMPSTDERR=$(mktemp)
DumpTrace(){
echo ">>> trace STDOUT"
cat "$TMPSTDOUT" [37/1663]
echo ">>> trace STDERR"
cat "$TMPSTDERR"
echo ">>> DONE"
}
# key item1 item2 ...
IsInArray(){
local key=$1; shift
for item in "$@"
do
[[ "X${item}" == "X${key}" ]] && return 0
done
return 1
}
Expect(){
test "X$2" = "X-" && return
if test "$1" != "$2"
then
Die "[${3:-""}]: expect '$2' but receive '$1'"
fi
}
Assert(){
local OPTS=`getopt -n $0 -l "stdout:,stderr:,ret:" -- "" "$@"`
test $? -ne 0 && Die "unexpected in Assert"
eval set -- "$OPTS"
expect_stdout='-' expect_stderr='-' expect_exit='0'
while test $# -ne 0
do
case "$1" in
--stdout) shift
expect_stdout=$1 ;;
--stderr) shift
expect_stderr=$1;;
--ret) shift
expect_exit=$1 ;;
--) shift
break ;;
*) Die "invalied option '$1'" ;;
esac
shift
done
trap "DumpTrace; /bin/rm $TMPSTDOUT $TMPSTDERR" EXIT
Echo COMMAND $@
"$@" 1>$TMPSTDOUT 2>$TMPSTDERR
ret=$?
stdout=$(cat $TMPSTDOUT)
stderr=$(cat $TMPSTDERR)
Expect "$ret" "${expect_exit:=0}" "exitcode"
Expect "$stdout" "$expect_stdout" "stdout"
Expect "$stderr" "$expect_stderr" "stderr"
echo "Assert [PASS]"
}
AssertProgram(){
local PARAMS
while test $# -ne 0
do
if test -n "$PARAMS"; then
PARAMS=("${PARAMS[@]}" "$1")
else
PARAMS=("$1")
fi
test "X$1" = "X--" && break
shift
done
shift # shift out --
local ARGUS
ARGUS=("${PARAMS[@]}" ./execute -c "$@")
Assert "${ARGUS[@]}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment