|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Test runner functions for bazel-mypy-integration integration tests. |
| 4 | +# Credit: https://github.com/bazelbuild/rules_scala/blob/master/test/shell/test_runner.sh |
| 5 | + |
| 6 | +NC='\033[0m' |
| 7 | +GREEN='\033[0;32m' |
| 8 | +RED='\033[0;31m' |
| 9 | +TIMOUT=60 |
| 10 | + |
| 11 | +run_test_ci() { |
| 12 | + # spawns the test to new process |
| 13 | + local TEST_ARG=$@ |
| 14 | + local log_file=output_$$.log |
| 15 | + echo "running test $TEST_ARG" |
| 16 | + eval $TEST_ARG &>$log_file & |
| 17 | + local test_pid=$! |
| 18 | + SECONDS=0 |
| 19 | + test_pulse_printer $! $TIMOUT $TEST_ARG & |
| 20 | + local pulse_printer_pid=$! |
| 21 | + local result |
| 22 | + |
| 23 | + { |
| 24 | + wait $test_pid 2>/dev/null |
| 25 | + result=$? |
| 26 | + kill $pulse_printer_pid && wait $pulse_printer_pid 2>/dev/null || true |
| 27 | + } || return 1 |
| 28 | + |
| 29 | + DURATION=$SECONDS |
| 30 | + if [ $result -eq 0 ]; then |
| 31 | + echo -e "\n${GREEN}Test \"$TEST_ARG\" successful ($DURATION sec) $NC" |
| 32 | + else |
| 33 | + echo -e "\nLog:\n" |
| 34 | + cat $log_file |
| 35 | + echo -e "\n${RED}Test \"$TEST_ARG\" failed $NC ($DURATION sec) $NC" |
| 36 | + fi |
| 37 | + return $result |
| 38 | +} |
| 39 | + |
| 40 | +test_pulse_printer() { |
| 41 | + # makes sure something is printed to stdout while test is running |
| 42 | + local test_pid=$1 |
| 43 | + shift |
| 44 | + local timeout=$1 # in minutes |
| 45 | + shift |
| 46 | + local count=0 |
| 47 | + |
| 48 | + # clear the line |
| 49 | + echo -e "\n" |
| 50 | + |
| 51 | + while [ $count -lt $timeout ]; do |
| 52 | + count=$(($count + 1)) |
| 53 | + echo -ne "Still running: \"$@\"\r" |
| 54 | + sleep 60 |
| 55 | + done |
| 56 | + |
| 57 | + echo -e "\n${RED}Timeout (${timeout} minutes) reached. Terminating \"$@\"${NC}\n" |
| 58 | + kill -9 $test_pid |
| 59 | +} |
| 60 | + |
| 61 | +run_test_local() { |
| 62 | + # runs the tests locally |
| 63 | + set +e |
| 64 | + SECONDS=0 |
| 65 | + TEST_ARG=$@ |
| 66 | + echo "running test $TEST_ARG" |
| 67 | + RES=$($TEST_ARG 2>&1) |
| 68 | + RESPONSE_CODE=$? |
| 69 | + DURATION=$SECONDS |
| 70 | + if [ $RESPONSE_CODE -eq 0 ]; then |
| 71 | + echo -e "${GREEN} Test \"$TEST_ARG\" successful ($DURATION sec) $NC" |
| 72 | + else |
| 73 | + echo -e "\nLog:\n" |
| 74 | + echo "$RES" |
| 75 | + echo -e "${RED} Test \"$TEST_ARG\" failed $NC ($DURATION sec) $NC" |
| 76 | + exit $RESPONSE_CODE |
| 77 | + fi |
| 78 | +} |
| 79 | + |
| 80 | +get_test_runner() { |
| 81 | + test_env=$1 |
| 82 | + if [[ "${test_env}" != "ci" && "${test_env}" != "local" ]]; then |
| 83 | + echo -e "${RED}test_env must be either 'local' or 'ci'" |
| 84 | + exit 1 |
| 85 | + fi |
| 86 | + echo "run_test_${test_env}" |
| 87 | +} |
0 commit comments