Lines Matching +full:enable +full:- +full:golden +full:- +full:tests
9 # http://www.apache.org/licenses/LICENSE-2.0
17 # Common utility file for Bazel shell tests
23 # ------------------------------------------------------------------------
43 # ------------------------------------------------------------------------
55 # The user can pass --test_arg to bazel test to select specific tests
56 # to run. Specifying --test_arg multiple times allows to select several
57 # tests to be run in the given order. Additionally the user may define
58 # TESTS=(test_foo test_bar ...) to specify a subset of test functions to
71 # important that tests clean up after themselves.
80 [ -n "$BASH_VERSION" ] ||
87 # This function may be called by testenv.sh or a test suite to enable errexit
90 set -o errtrace
91 set -eu
98 trap - ERR
103 # Enable errexit with pretty stack traces.
109 local status_code=${2:-1}
111 if [ "$CAPTURED_STD_ERR" -ne 0 -o "$CAPTURED_STD_OUT" -ne 0 ]; then
113 if [ "$CAPTURED_STD_OUT" -ne 0 ]; then
117 if [ "$CAPTURED_STD_ERR" -ne 0 ]; then
123 if [ -n "${1-}" ] ; then
126 if [ -n "${BASH-}" ]; then
128 while [ $caller_n -lt 4 ] && caller_out=$(caller $caller_n 2>/dev/null); do
129 test $caller_n -eq 0 && echo "CALLER stack (max 4):"
134 if [ x"$status_code" != x -a x"$status_code" != x"0" ]; then
141 # Print message in "$1" then record that a non-fatal error occurred in ERROR_COUNT
142 ERROR_COUNT="${ERROR_COUNT:-0}"
144 if [ -n "$1" ] ; then
161 # (e.g., a non-number was passed in) then the check will fail.
165 [ "$1" -gt "$2" ] || die "Check failed: '$1' <= '$2' ${3:+ ($3)}"
170 [ "$1" -lt "$2" ] || die "Check failed: '$1' < '$2' ${3:+ ($3)}"
175 [ "$1" -ge "$2" ] || die "Check failed: '$1' >= '$2' ${3:+ ($3)}"
180 [ "$1" -gt "$2" ] || die "Check failed: '$1' > '$2' ${3:+ ($3)}"
193 ATEXIT="${ATEXIT-}"
195 if [ -z "$ATEXIT" ]; then
204 if [ -z "${TEST_TMPDIR:-}" ]; then
205 export TEST_TMPDIR="$(mktemp -d ${TMPDIR:-/tmp}/bazel-test.XXXXXXXX)"
207 if [ ! -e "${TEST_TMPDIR}" ]; then
208 mkdir -p -m 0700 "${TEST_TMPDIR}"
210 atexit "rm -fr ${TEST_TMPDIR}"
214 # (golden) output.
219 # diff_test_stdout "$TEST_SRCDIR/path/to/golden.out"
222 CAPTURED_STD_OUT="${CAPTURED_STD_OUT:-0}"
223 CAPTURED_STD_ERR="${CAPTURED_STD_ERR:-0}"
240 if [ -z "${XML_OUTPUT_FILE:-}" ]; then
251 # tests invoking 'cd'!
259 TESTS=() # A subset or "working set" of test
261 # default, all tests called test_* are
263 if [ $# -gt 0 ]; then
268 TESTS=($(for i in $@; do echo $i; done | grep ^test_ || true))
269 if (( ${#TESTS[@]} == 0 )); then
270 echo "WARNING: Arguments do not specifies tests!" >&2
283 echo "-- Test log: -----------------------------------------------------------"
284 [[ -e $TEST_log ]] && cat $TEST_log || echo "(Log file did not exist.)"
285 echo "------------------------------------------------------------------------"
288 # Usage: __pad <title> <pad-char>
294 echo -n "$pad$pad $title "
296 } | head -c 80
361 if [ -n "${TEST_WARNINGS_OUTPUT_FILE:-}" ]; then
376 while (( i < ${#FUNCNAME[@]} )) && [[ ${BASH_SOURCE[i]:-} == ${BASH_SOURCE[0]} ]]; do
381 while (( i < ${#FUNCNAME[@]} )) && [[ ${BASH_SOURCE[i]:-} != ${BASH_SOURCE[0]} ]]; do
384 …echo "${BASH_SOURCE[i]:-"Unknown"}:${BASH_LINENO[i - 1]:-"Unknown"}: in call to ${FUNCNAME[i]:-"Un…
392 # Usage: expect_log <regexp> [error-message]
395 # returns non-zero.
398 local message=${2:-Expected regexp "$pattern" not found}
399 grep -sq -- "$pattern" $TEST_log && return 0
405 # Usage: expect_log_warn <regexp> [error-message]
410 local message=${2:-Expected regexp "$pattern" not found}
411 grep -sq -- "$pattern" $TEST_log && return 0
417 # Usage: expect_log_once <regexp> [error-message]
420 # error message otherwise, and returns non-zero.
423 local message=${2:-Expected regexp "$pattern" not found exactly once}
427 # Usage: expect_log_n <regexp> <count> [error-message]
430 # error message otherwise, and returns non-zero.
433 local expectednum=${2:-1}
434 local message=${3:-Expected regexp "$pattern" not found exactly $expectednum times}
435 local count=$(grep -sc -- "$pattern" $TEST_log)
441 # Usage: expect_not_log <regexp> [error-message]
444 # returns non-zero.
447 local message=${2:-Unexpected regexp "$pattern" found}
448 grep -sq -- "$pattern" $TEST_log || return 0
454 # Usage: expect_log_with_timeout <regexp> <timeout> [error-message]
457 # error message otherwise, and returns non-zero.
461 local message=${3:-Regexp "$pattern" not found in "$timeout" seconds}
463 while [ $count -lt $timeout ]; do
464 grep -sq -- "$pattern" $TEST_log && return 0
469 grep -sq -- "$pattern" $TEST_log && return 0
481 local timeout=${3:-10}
483 while [ $count -lt $timeout ]; do
500 local last_arg_index=$((${#args[@]} - 1))
531 # Usage: assert_contains <regexp> <file> [error-message]
534 # returns non-zero.
538 local message=${3:-Expected regexp "$pattern" not found in "$file"}
539 grep -sq -- "$pattern" "$file" && return 0
546 # Usage: assert_not_contains <regexp> <file> [error-message]
549 # returns non-zero.
553 local message=${3:-Expected regexp "$pattern" found in "$file"}
554 grep -sq -- "$pattern" "$file" || return 0
561 # Updates the global variables TESTS if
564 [ -z "${TEST_TOTAL_SHARDS-}" ] && return 0
566 [ "$TEST_TOTAL_SHARDS" -gt 0 ] ||
569 [ "$TEST_SHARD_INDEX" -lt 0 -o "$TEST_SHARD_INDEX" -ge "$TEST_TOTAL_SHARDS" ] &&
572 TESTS=$(for test in "${TESTS[@]}"; do echo "$test"; done |
575 [ -z "${TEST_SHARD_STATUS_FILE-}" ] || touch "$TEST_SHARD_STATUS_FILE"
578 # Usage: __test_terminated <signal-number>
597 if [[ -f $TEST_TMPDIR/__err_handled ]]; then
601 if [[ ! -z "$TEST_name" ]]; then
602 echo -n "$TEST_name "
604 echo "FAILED: terminated because this command returned a non-zero status:" >&2
609 # to run tests, so we shouldn't call tear_down.
610 if [[ ! -z "$TEST_name" ]]; then
632 if [[ ! -e "$XML_OUTPUT_FILE" ]]; then
633 local xml_header='<?xml version="1.0" encoding="UTF-8"?>'
639 perl -e "\
656 local failed=$((total - passed))
660 "s/<testsuites>/<testsuites tests=\"$total\" failures=\"0\" errors=\"$failed\">/" | \
662 "s/<testsuite>/<testsuite tests=\"$total\" failures=\"0\" errors=\"$failed\">/" \
665 rm -f $XML_OUTPUT_FILE
669 # Multi-platform timestamp function
670 UNAME=$(uname -s | tr 'A-Z' 'a-z')
681 command -v python3 &> /dev/null && PYTHON=python3
682 LC_ALL=C "${PYTHON}" -c 'import time; print(int(round(time.time() * 1000)))'
689 run_time_ms=$((${ts_end}-${ts_start}))
690 echo $(($run_time_ms/1000)).${run_time_ms: -3}
693 # Usage: run_tests <suite-comment>
695 # Calls exit with zero on success, non-zero otherwise.
708 # If the user didn't specify an explicit list of tests (e.g. a
710 if [ ${#TESTS[@]} = 0 ]; then
711 TESTS=$(declare -F | awk '{print $3}' | grep ^test_)
712 elif [ -n "${TEST_WARNINGS_OUTPUT_FILE:-}" ]; then
713 if grep -q "TESTS=" "$TEST_script" ; then
714 echo "TESTS variable overridden in Bazel sh_test. Please remove before submitting" \
721 for TEST_name in ${TESTS[@]}; do
731 rm -f $TEST_TMPDIR/{__ts_start,__ts_end}
733 if [ "$(type -t $TEST_name)" = function ]; then
739 rm -f $TEST_TMPDIR/__err_handled
758 [ -f $TEST_TMPDIR/__ts_end ] || timestamp >$TEST_TMPDIR/__ts_end
767 if [ -n "$SAVED_ATEXIT" ]; then
771 else # Bad test explicitly specified in $TESTS.
798 __pad "$passed / $total tests passed." '*' >&2