Make add info() log_warn_die.sh and make the subs respect OPT_VERBOSE. Add more test harness subs in util/test-bash-functions.

This commit is contained in:
Daniel Nichter
2013-01-24 09:17:19 -07:00
parent 9693f73e5e
commit 98ced1a035
3 changed files with 125 additions and 7 deletions

View File

@@ -27,24 +27,34 @@ set -u
PTFUNCNAME="" PTFUNCNAME=""
PTDEBUG="${PTDEBUG:-""}" PTDEBUG="${PTDEBUG:-""}"
EXIT_STATUS=0 EXIT_STATUS=0
OPT_VERBOSE=${OPT_VERBOSE:-3}
log() { _print() {
TS=$(date +%F-%T | tr ':-' '_'); TS=$(date +%F-%T | tr ':-' '_')
echo "$TS $*" echo "$TS $*"
} }
info() {
[ ${OPT_VERBOSE:-0} -ge 3 ] && _print "$*"
}
log() {
[ ${OPT_VERBOSE:-0} -ge 2 ] && _print "$*"
}
warn() { warn() {
log "$*" >&2 [ ${OPT_VERBOSE:-0} -ge 1 ] && _print "$*" >&2
EXIT_STATUS=1 EXIT_STATUS=1
} }
die() { die() {
warn "$*" _print "$*" >&2
EXIT_STATUS=1
exit 1 exit 1
} }
_d () { _d () {
[ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(log "$*")" >&2 [ "$PTDEBUG" ] && echo "# $PTFUNCNAME: $(_print "$*")" >&2
} }
# ########################################################################### # ###########################################################################

View File

@@ -34,6 +34,80 @@ is \
"1" \ "1" \
"Exit status 1" "Exit status 1"
OPT_VERBOSE=1
info "Hello world 1!" > $TEST_PT_TMPDIR/log
file_is_empty \
$TEST_PT_TMPDIR/log \
"verbose=1 info"
log "Hello world 2!" > $TEST_PT_TMPDIR/log
file_is_empty \
$TEST_PT_TMPDIR/log \
"verbose=1 log"
warn "Hello world 3!" > $TEST_PT_TMPDIR/log 2>&1
file_contains \
$TEST_PT_TMPDIR/log \
"Hello world 3!" \
"verbose=1 warn"
OPT_VERBOSE=2
info "Hello world 4!" > $TEST_PT_TMPDIR/log
file_is_empty \
$TEST_PT_TMPDIR/log \
"verbose=2 info"
log "Hello world 5!" > $TEST_PT_TMPDIR/log
file_contains \
$TEST_PT_TMPDIR/log \
"Hello world 5!" \
"verbose=2 log"
warn "Hello world 6!" > $TEST_PT_TMPDIR/log 2>&1
file_contains \
$TEST_PT_TMPDIR/log \
"Hello world 6!" \
"verbose=2 warn"
OPT_VERBOSE=3
info "Hello world 7!" > $TEST_PT_TMPDIR/log
file_contains \
$TEST_PT_TMPDIR/log \
"Hello world 7!" \
"verbose=3 info"
log "Hello world 8!" > $TEST_PT_TMPDIR/log
file_contains \
$TEST_PT_TMPDIR/log \
"Hello world 8!" \
"verbose=3 log"
warn "Hello world 9!" > $TEST_PT_TMPDIR/log 2>&1
file_contains \
$TEST_PT_TMPDIR/log \
"Hello world 9!" \
"verbose=3 warn"
OPT_VERBOSE=0
info "Hello world 10!" > $TEST_PT_TMPDIR/log
file_is_empty \
$TEST_PT_TMPDIR/log \
"verbose=0 info"
log "Hello world 11!" > $TEST_PT_TMPDIR/log
file_is_empty \
$TEST_PT_TMPDIR/log \
"verbose=0 log"
warn "Hello world 12!" > $TEST_PT_TMPDIR/log 2>&1
file_is_empty \
$TEST_PT_TMPDIR/log \
"verbose=0 warn"
# ########################################################################### # ###########################################################################
# Done # Done
# ########################################################################### # ###########################################################################

View File

@@ -127,13 +127,11 @@ plan() {
pass() { pass() {
local reason="${1:-""}" local reason="${1:-""}"
result 0 "$reason" result 0 "$reason"
} }
fail() { fail() {
local reason="${1:-""}" local reason="${1:-""}"
result 1 "$reason" result 1 "$reason"
} }
@@ -177,6 +175,42 @@ is() {
result $? "$test_name" result $? "$test_name"
} }
file_is_empty() {
local file=$1
local test_name=${2:-""}
test_command="-s $file"
if [ ! -f "$file" ]; then
echo "$file does not exist" > $TEST_PT_TMPDIR/failed_result
result 1 "$test_name"
fi
if [ -s "$file" ]; then
echo "$file is not empty:" > $TEST_PT_TMPDIR/failed_result
cat "$file" >> $TEST_PT_TMPDIR/failed_result
result 1 "$test_name"
else
result 0 "$test_name"
fi
}
file_contains() {
local file="$1"
local pat="$2"
local test_name=${3:-""}
test_command="grep -q '$pat' '$file'"
if [ ! -f "$file" ]; then
echo "$file does not exist" > $TEST_PT_TMPDIR/failed_result
result 1 "$test_name"
fi
grep -q "$pat" $file
if [ $? -ne 0 ]; then
echo "$file does not contain '$pat':" > $TEST_PT_TMPDIR/failed_result
cat "$file" >> $TEST_PT_TMPDIR/failed_result
result 1 "$test_name"
else
result 0 "$test_name"
fi
}
cmd_ok() { cmd_ok() {
local test_command=$1 local test_command=$1
local test_name=${2:-""} local test_name=${2:-""}