Commit a66c367c authored by Paul Cammish's avatar Paul Cammish
Browse files

Fix output to properly support --cron, -q and -v

parent c383e213
...@@ -17,6 +17,7 @@ _debug() { ...@@ -17,6 +17,7 @@ _debug() {
_warn() { _warn() {
echo -e "\033[1m\033[33m WARN:\033[0m $@" echo -e "\033[1m\033[33m WARN:\033[0m $@"
_log " WARN: $@"
} }
_error() { _error() {
...@@ -31,13 +32,11 @@ _stop() { ...@@ -31,13 +32,11 @@ _stop() {
} }
_echo() { _echo() {
if [ $DEBUG ]; then _debug " INFO: ($VERBOSITY) $@"
echo -e " INFO: $@" if [ $VERBOSITY -gt 2 ]; then
elif [ $VERBOSE ]; then
echo -e "$@" echo -e "$@"
else fi
_log "$@" _log "$@"
fi
} }
_log() { _log() {
...@@ -45,61 +44,91 @@ _log() { ...@@ -45,61 +44,91 @@ _log() {
} }
_pass() { _pass() {
if [ $QUIET ] || [ $QUIETQUIET ]; then if [ $VERBOSITY -eq 1 ] || [ $VERBOSITY -eq 2 ] ; then
echo -ne "\033[32m.\033[0m" echo -ne "\033[32m.\033[0m"
_centre_logonly - "\033[1m\033[32m$script\033[0m"
_log "$( cat /dev/shm/sympl-tests_stdout )"
_centre_logonly - "\033[1m\033[32mPASS\033[0m\033[2m"
else else
_centre - "\033[1m\033[32m$script\033[0m"
_echo "$( cat /dev/shm/sympl-tests_stdout )" _echo "$( cat /dev/shm/sympl-tests_stdout )"
_centre - "\033[1m\033[32mPASS\033[0m\033[2m" _centre - "\033[1m\033[32mPASS\033[0m\033[2m"
fi fi
} }
_fail() { _fail() {
if [ $QUIETQUIET ]; then if [ $VERBOSITY -eq 1 ] ; then
echo -ne "\033[1m\033[31mF\033[0m" echo -ne "\033[1m\033[31mF\033[0m"
elif [ $QUIET ]; then _centre_logonly - "\033[1m\033[31m$script\033[0m"
echo _log "$( cat /dev/shm/sympl-tests_stdout )"
VERBOSE=true _centre_logonly - "\033[1m\033[31mFAIL\033[0m"
_centre - "$script"
cat /dev/shm/sympl-tests_stdout
_centre - "\033[1m\033[31mFAIL\033[0m"
unset VERBOSE
else else
cat /dev/shm/sympl-tests_stdout _loud
_centre - "\033[1m\033[31mFAIL\033[0m\033[2m" _echo
_centre - "\033[1m\033[31m$script\033[0m"
_echo "$( cat /dev/shm/sympl-tests_stdout )"
_centre - "\033[1m\033[31mFAIL\033[0m"
_quiet
fi fi
} }
_test_error() { _test_error() {
if [ $QUIETQUIET ]; then if [ $VERBOSITY -eq 1 ]; then
echo -ne "\033[1m\033[1m\033[33mX\033[0m" echo -ne "\033[1m\033[1m\033[33mX\033[0m"
elif [ $QUIET ]; then _centre_logonly - "\033[1m\033[33m$script\033[0m"
echo _log "$(cat /dev/shm/sympl-tests_stdout)"
VERBOSE=true _centre_logonly - "\033[1m\033[5m\033[33m[TEST ERROR]\033[0m"
_centre - "$script"
cat /dev/shm/sympl-tests_stdout
_centre - "\033[1m\033[5m\033[33m[TEST ERROR]\033[0m"
unset VERBOSE
else else
cat /dev/shm/sympl-tests_stdout _loud
_echo
_centre - "\033[1m\033[33m$script\033[0m"
_echo "$(cat /dev/shm/sympl-tests_stdout)"
_centre - "\033[1m\033[5m\033[33m[TEST ERROR]\033[0m" _centre - "\033[1m\033[5m\033[33m[TEST ERROR]\033[0m"
_quiet
fi fi
} }
_centre() { _centre() {
_debug "($VERBOSITY) Centre $1$1 $2 $1$1"
spacer="$1" spacer="$1"
line="$2" line="$2"
stripped_line="$( echo $line | sed 's|\\033[[0-9;]*m||g' )" stripped_line="$( echo $line | sed 's|\\033[[0-9;]*m||g' )"
if [ $VERBOSE ]; then if [ $VERBOSITY -gt 1 ]; then
# as cut can't ignore the control codes, we need to work out of we need to add anything to the output to make it line up
# in other cases we can just use cut to trim it down
if (( ${#stripped_line} % 2 )); then extra_char=$spacer ; else extra_char="" ; fi
col="$( tput cols )" || col=80 col="$( tput cols )" || col=80
col=$((col - 3)) col=$((col - 3))
len=$(( ( col / 2 ) - ( ( ${#stripped_line} + 1 ) / 2 ) )) len=$(( ( col / 2 ) - ( ( ${#stripped_line} + 1 ) / 2 ) ))
echo -e "\033[2m$(for each in $(seq 1 $len); do printf "$spacer"; done)\033[0m $line \033[2m$(for each in $(seq 1 $len); do printf "$spacer"; done)\033[0m" echo -e "\033[2m$(for each in $(seq 1 $len); do printf "$spacer"; done)\033[0m $line \033[2m$(for each in $(seq 1 $len); do printf "$spacer"; done)$extra_char\033[0m"
fi fi
col=78 col=78
len=$(( ( col / 2 ) - ( ( ${#stripped_line} + 1 ) / 2 ) )) len=$(( ( col / 2 ) - ( ( ${#stripped_line} + 1 ) / 2 ) ))
_log "$(for each in $(seq 1 $len); do printf "$spacer"; done) $stripped_line $(for each in $(seq 1 $len); do printf "$spacer"; done)" | cut -c 1-$((col)) _log "$(for each in $(seq 1 $len); do printf "$spacer"; done) $stripped_line $(for each in $(seq 1 $len); do printf "$spacer"; done)$spacer" | cut -c 1-$((col))
}
_centre_logonly() {
_debug "($VERBOSITY) Centre $1$1 $2 $1$1"
spacer="$1"
line="$2"
stripped_line="$( echo $line | sed 's|\\033[[0-9;]*m||g' )"
col=78
len=$(( ( col / 2 ) - ( ( ${#stripped_line} + 1 ) / 2 ) ))
_log "$(for each in $(seq 1 $len); do printf "$spacer"; done) $stripped_line $(for each in $(seq 1 $len); do printf "$spacer"; done)$spacer" | cut -c 1-$((col))
}
_loud() {
_debug turning up VERBOSITY from $VERBOSITY to $((VERBOSITY+10))
VERBOSITY=$((VERBOSITY+10))
}
_quiet() {
_debug turning down VERBOSITY from $VERBOSITY to $((VERBOSITY-10))
VERBOSITY=$((VERBOSITY-10))
} }
############################################################################# #############################################################################
# Set Defaults # # Set Defaults #
############################################################################# #############################################################################
...@@ -107,6 +136,16 @@ _centre() { ...@@ -107,6 +136,16 @@ _centre() {
TIMESTAMP="$(date +%s)" TIMESTAMP="$(date +%s)"
TEST_PATH="$(dirname $0)/tests.d" TEST_PATH="$(dirname $0)/tests.d"
VERBOSE=true VERBOSE=true
# VERBOSITY is now chatty stdout is:
# 3+ Verbose, output from each test
# 2 Quiet, output from failed/errored tests, dots for others
# 1 Very Quiet, output only dots and letters
# 0 Silent, no output, only log
# add & remove 10 to verbosity to make things loud when needed
VERBOSITY=3
LOG="./sympl-tests.log" LOG="./sympl-tests.log"
############################################################################# #############################################################################
...@@ -136,9 +175,11 @@ while [ $# -gt 0 ]; do ...@@ -136,9 +175,11 @@ while [ $# -gt 0 ]; do
;; ;;
--cron) --cron)
unset VERBOSE unset VERBOSE
VERBOSITY=0
shift shift
;; ;;
--quiet) --quiet|-q)
VERBOSITY=$((VERBOSITY-1))
unset VERBOSE unset VERBOSE
if [ $QUIET ]; then if [ $QUIET ]; then
QUIETQUIET=true QUIETQUIET=true
...@@ -147,7 +188,8 @@ while [ $# -gt 0 ]; do ...@@ -147,7 +188,8 @@ while [ $# -gt 0 ]; do
fi fi
shift shift
;; ;;
--verbose) --verbose|-v)
VERBOSITY=$((VERBOSITY+1))
VERBOSE=true VERBOSE=true
shift shift
;; ;;
...@@ -162,6 +204,7 @@ _debug "Command line variables: ...@@ -162,6 +204,7 @@ _debug "Command line variables:
------------------------------------------ ------------------------------------------
UNKNOWN |$PARAMETERS UNKNOWN |$PARAMETERS
VERBOSE |$VERBOSE VERBOSE |$VERBOSE
VERBOSITY |$VERBOSITY
LOG |$LOG LOG |$LOG
TEST_PATH |$TEST_PATH TEST_PATH |$TEST_PATH
DEBUG |$DEBUG DEBUG |$DEBUG
...@@ -174,18 +217,22 @@ TIMESTAMP="$(date +%s)" ...@@ -174,18 +217,22 @@ TIMESTAMP="$(date +%s)"
if [ -f "$LOG" ]; then rm "$LOG" ; fi if [ -f "$LOG" ]; then rm "$LOG" ; fi
#_echo "Starting run at $(date ; if [ "$TEST_PATH" != "./tests.d" ] ; then echo -n ", test path: '$TEST_PATH'" ; fi )" if [ $VERBOSITY -ne 1 ]; then
_echo "Starting run at $(date ; if [ "$TEST_PATH" != "./tests.d" ] ; then echo -n ", test path: '$TEST_PATH'" ; fi )"
fi
_centre = Starting _centre = Starting
TOTAL=$( find $TEST_PATH -type f -executable | wc -l ) TOTAL=$( find $TEST_PATH -type f -executable | wc -l )
for script in $( find $TEST_PATH -type f -executable | sort ) ; do for script in $( find $TEST_PATH -type f -executable | sort ) ; do
_echo _echo
_centre - "$script" # _centre - "$script"
echo -n > /dev/shm/sympl-tests_stdout echo -n > /dev/shm/sympl-tests_stdout
# trap "_error $script failed! ; cat /dev/shm/sympl-tests_stdout_$TIMESTAMP /dev/shm/sympl-tests_stderr_$TIMESTAMP" ERR # trap "_error $script failed! ; cat /dev/shm/sympl-tests_stdout_$TIMESTAMP /dev/shm/sympl-tests_stderr_$TIMESTAMP" ERR
$script > /dev/shm/sympl-tests_stdout 2>&1 $script > /dev/shm/sympl-tests_stdout 2>&1
exitcode=$? exitcode=$?
# _echo "$( cat /dev/shm/sympl-tests_stdout )"
if [ $exitcode == 0 ] ; then if [ $exitcode == 0 ] ; then
PASS=$((PASS+1)) PASS=$((PASS+1))
_pass _pass
...@@ -198,10 +245,31 @@ for script in $( find $TEST_PATH -type f -executable | sort ) ; do ...@@ -198,10 +245,31 @@ for script in $( find $TEST_PATH -type f -executable | sort ) ; do
fi fi
rm /dev/shm/sympl-tests_stdout rm /dev/shm/sympl-tests_stdout
done done
_echo
_centre = Summary
_echo "Test run completed at $(date)"
_echo "Passed: $PASS/$TOTAL $( printf %.2f%% "$((10**3 * 100 * $PASS/$TOTAL))e-3" ) Failed: $FAIL Errors: $ERROR"
_centre = "End"
#run-parts tests.d/00-system -a ~/git/sympl-tests/sympl-tests if [ $VERBOSITY -gt 1 ]; then
_loud
fi
_echo
_centre = Summary
_echo "Test run completed at $(date)"
_echo "Passed: \033[1m\033[32m$PASS\033[0m/$TOTAL (\033[1m\033[32m$( printf %.2f%% "$((10**3 * 100 * $PASS/$TOTAL))e-3" )\033[0m) Failed: \033[1m\033[31m$FAIL\033[0m Errors: \033[1m\033[33m$ERROR\033[0m"
_centre = "End"
if [ $VERBOSITY -eq 1 ]; then
echo
fi
if [ $ERROR -gt 0 ]; then
#grep output the fails and errors from the log to stdout
exit 255
elif [ $FAIL -gt 0 ]; then
#grep output the fails from the log to stdout
exit $FAIL
elif [ $PASS -eq $TOTAL ]; then
exit 0
else
exit 255
fi
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment