blob: f26c3baff419218cc9347bcead634e4d25347066 [file] [log] [blame]
#!/system/bin/sh
#
# Copyright 2015 Nest Labs, Inc. All rights reserved.
if [ -z "$PACKAGE" ] ; then
PACKAGE="faux-folly-tests"
fi
if [ -z "$TEST_ROOT" ] ; then
TEST_ROOT="$(dirname $0)"
grep -vE "^/" "$TEST_ROOT" &> /dev/null
if [ $? -eq 0 ] ; then
TEST_ROOT="$(pwd)/${TEST_ROOT}"
fi
fi
THIS_SCRIPT="$(basename $0)"
###
### GLOBAL VARIABLES
###
### only export variables that mutate
###
# Increment when a test passes
export GLOBAL_PASS=0
# Increment when a test fails
export GLOBAL_FAIL=0
# The directory where helper test programs are installed
TEST_BIN="${TEST_ROOT}/bin"
# The directory where helper test data is installed
TEST_DATA="${TEST_ROOT}/data"
# The temporary directory where raw program output is placed
TMPDIR=/storage
# The log file with all raw program output
LOGFILE=$(mktemp --tmpdir="${TMPDIR}" ${PACKAGE}-log.XXXXXXXX)
# A temporary file, usually used for staging raw program output for grep
TMPFILE=$(mktemp --tmpdir="${TMPDIR}" ${PACKAGE}-tmp.XXXXXXXX)
# The scripts need their PATH set, too
export PATH="${TEST_BIN}:${PATH}"
# run_test
#
# This is a /bin/sh port of add_gflags_test that is implemented by
# cmake/utils.cmake and cmake/execute_test.cmake
#
# Usage: run_test <test-name> <expected-return-code> <expected-text-output> <unexpected-text-output> <cmd> [args...]
#
# test-name: A symbolic name for the test for logging
#
# expected-return-code: the number passed back to the operating system. default: 0
# if a blank string is given.
#
# expected-text-output: if a non-empty string, it is expected that the command will
# output this string. It must be seen in order to pass.
#
# unexpected-text-output: if a non-empty string, it is expected that the command
# will NOT output this string. If it is seen, the test will fail.
#
# cmd: executable to run (full path or in $PATH)
#
# args: all arguments that need to be passed on to the executable.
#
run_test()
{
TEST_NAME="$1"
shift
EXPECTED_RC="$1"
shift
EXPECTED_OUTPUT="$1"
shift
UNEXPECTED_OUTPUT="$1"
shift
if [ $# -eq 0 ] ; then
GLOBAL_RETURN_CODE=1
echo "FATAL ERROR: invalid arguments passed to run_test for test='$TEST_NAME'"
return 1
fi
if [ -z "$EXPECTED_RC" ] ; then
EXPECTED_RC="0"
fi
VERDICT="pass"
REASON="default"
echo "RUNNING TEST '${TEST_NAME}' >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" > "$TMPFILE"
echo "CMDLINE: $@" >> "$TMPFILE"
"$@" &>> "$TMPFILE"
RC=$?
echo "RC=$RC" >> "$TMPFILE"
echo "END OF TEST '${TEST_NAME}' <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" >> "$TMPFILE"
EXPECTED_FOUND=1
UNEXPECTED_FOUND=0
if [ -n "$EXPECTED_OUTPUT" ] ; then
grep -- "$EXPECTED_OUTPUT" "$TMPFILE" &> /dev/null
if [ $? -ne 0 ] ; then
EXPECTED_FOUND=0
fi
fi
if [ -n "$UNEXPECTED_OUTPUT" ] ; then
grep -- "$UNEXPECTED_OUTPUT" "$TMPFILE" &> /dev/null
if [ $? -eq 0 ] ; then
UNEXPECTED_FOUND=1
fi
fi
if [ "$RC" != "$EXPECTED_RC" ] ; then
VERDICT="fail"
REASON="unexpected return code (got ${RC}, expected ${EXPECTED_RC})"
elif [ "$EXPECTED_FOUND" = "0" ] ; then
VERDICT="fail"
REASON="missing expected output, expected '${EXPECTED_OUTPUT}', see '$LOGFILE'"
elif [ "$UNEXPECTED_FOUND" = "1" ] ; then
VERDICT="fail"
REASON="Unexpected output detected: '${UNEXPECTED_OUTPUT}', see '$LOGFILE'"
fi
cat "$TMPFILE" >> "$LOGFILE"
if [ "$VERDICT" = "pass" ] ; then
echo "${TEST_NAME}: pass"
GLOBAL_PASS=$(($GLOBAL_PASS + 1))
return 0
else
echo "${TEST_NAME}: fail ($REASON)"
GLOBAL_FAIL=$(($GLOBAL_FAIL + 1))
return 1
fi
}
# Tests must be executed from here:
cd "$TEST_ROOT"
###
### TEST CASES
###
# Test this script
run_test "the_truth" 0 "" "" true
run_test "the_lie" 1 "" "" false
run_test "the_word" "" "really unlikely" "" echo "really unlikely"
for T in "${TEST_BIN}"/* ; do
NAME=$(basename "${T}")
if [ "x${NAME}" == "x${THIS_SCRIPT}" ] ; then
continue
fi
if [ ! -x "${T}" ] ; then
echo "Skipping '${NAME}' because it doesn't have execute permission"
continue
fi
if [ ! -f "${T}" ] ; then
echo "Skipping '${NAME}' because it is not a file"
continue
fi
run_test "$NAME" 0 "" "" "$T"
done
###
### RESULTS
###
if [ $GLOBAL_FAIL -ne 0 ] ; then
RETURN_CODE=1
else
RETURN_CODE=0
fi
TOTAL_TESTS=$(($GLOBAL_PASS + $GLOBAL_FAIL))
echo "${GLOBAL_PASS}/${TOTAL_TESTS} passed. See '$LOGFILE'"
rm "$TMPFILE"
exit $RETURN_CODE