blob: 1af070f90227c9fc1450654e99d89768d73027a7 [file] [log] [blame]
#!/system/bin/sh
#
# Copyright 2015 Nest Labs, Inc. All rights reserved.
PACKAGE="google-glog-tests"
TEST_ROOT="$(dirname $0)"
grep -vE "^/" "$TEST_ROOT" &> /dev/null
if [ $? -eq 0 ] ; then
TEST_ROOT="$(pwd)/${TEST_ROOT}"
fi
###
### 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"
export FLAGS_test_srcdir=$(pwd)
run_test logging_striplog_test_sh 0 "" "" /system/bin/sh ./bin/logging_striplog_test.sh
run_test demangle_unittest 0 "" "" demangle_unittest
run_test demangle_unittest_sh 0 "" "" /system/bin/sh ./bin/demangle_unittest.sh
run_test signalhandler_unittest 0 "" "" signalhandler_unittest
# Note: stack trace is broken on android for this test
# had to remove `main' and `DieInThread' stacktrace checks
run_test signalhandler_unittest_sh 0 "" "" /system/bin/sh ./bin/signalhandler_unittest.sh
run_test logging_unittest 0 "" "" logging_unittest
run_test stacktrace_unittest 0 "" "" stacktrace_unittest
run_test symbolize_unittest 0 "" "" symbolize_unittest
run_test stl_logging_unittest 0 "" "" stl_logging_unittest
run_test utilities_unittest 0 "" "" utilities_unittest
run_test mock_log_test 0 "" "" mock_log_test
###
### 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