| 1 | set -Eo pipefail |
|---|
| 2 | |
|---|
| 3 | # copy FDs |
|---|
| 4 | exec 5<&0 |
|---|
| 5 | exec 6>&1 |
|---|
| 6 | exec 7>&2 |
|---|
| 7 | |
|---|
| 8 | test_num=0 |
|---|
| 9 | test_count=$(@GREP@ -c "begin_test" "$0") |
|---|
| 10 | echo "1..$test_count" |
|---|
| 11 | |
|---|
| 12 | do_clean=yes |
|---|
| 13 | test_helper_tmpdirs_=() |
|---|
| 14 | trap test_helper_cleanup_ EXIT |
|---|
| 15 | script_error_disable_= |
|---|
| 16 | trap script_error ERR |
|---|
| 17 | |
|---|
| 18 | script_error() { |
|---|
| 19 | if [ -z "$script_error_disable_" ]; then |
|---|
| 20 | n="${#FUNCNAME[@]}" |
|---|
| 21 | test_err="${test_err}Error in bash, callstack: |
|---|
| 22 | " |
|---|
| 23 | let n--; |
|---|
| 24 | for ((; n>0; --n)); do |
|---|
| 25 | let m=n-1 |
|---|
| 26 | test_err="${test_err} $n: ${BASH_SOURCE[$n]}:${BASH_LINENO[$m]} in ${FUNCNAME[$n]}() |
|---|
| 27 | " |
|---|
| 28 | done |
|---|
| 29 | fi |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | maketmpdir() { |
|---|
| 33 | local tmp |
|---|
| 34 | tmp=$(@MKTEMP@ -d 2>/dev/null || mktemp -d -t 'mytmpdir') |
|---|
| 35 | test_helper_tmpdirs_+=($tmp) |
|---|
| 36 | eval $1=$tmp |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | test_helper_cleanup_() { |
|---|
| 40 | if test x"$do_clean" == x"yes"; then |
|---|
| 41 | for dir in "${test_helper_tmpdirs_[@]}"; do |
|---|
| 42 | echo "Removing $dir" |
|---|
| 43 | rm -rf "$dir" |
|---|
| 44 | done |
|---|
| 45 | else |
|---|
| 46 | test_name=$(basename $0) |
|---|
| 47 | for dir in "${test_helper_tmpdirs_[@]}"; do |
|---|
| 48 | tgt=test_failures/$test_name |
|---|
| 49 | echo "Copying $dir to $tgt" |
|---|
| 50 | rm -rf $tgt |
|---|
| 51 | mkdir -p $tgt |
|---|
| 52 | mv "$dir"/* $tgt |
|---|
| 53 | rm -rf "$dir" |
|---|
| 54 | done |
|---|
| 55 | fi |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | capture_stdout() { |
|---|
| 59 | echo "----- Running \"$*\"" |
|---|
| 60 | script_error_disable_=1 |
|---|
| 61 | output=$(eval "$SINA_TEST_WRAPPER $*" | @TEE@ /dev/fd/6) |
|---|
| 62 | exit_code=$? |
|---|
| 63 | script_error_disable_= |
|---|
| 64 | echo "----- Finished with exit code $exit_code (cmd: \"$*\"" |
|---|
| 65 | } |
|---|
| 66 | capture_stderr() { |
|---|
| 67 | echo "----- Running \"$*\"" |
|---|
| 68 | script_error_disable_=1 |
|---|
| 69 | output=$(eval "$SINA_TEST_WRAPPER $*" 2>&1 >&6 | @TEE@ /dev/fd/7) |
|---|
| 70 | exit_code=$? |
|---|
| 71 | script_error_disable_= |
|---|
| 72 | echo "----- Finished with exit code $exit_code (cmd: \"$*\"" |
|---|
| 73 | } |
|---|
| 74 | capture_stdouterr() { |
|---|
| 75 | echo "----- Running \"$*\"" |
|---|
| 76 | script_error_disable_=1 |
|---|
| 77 | output=$(eval "$SINA_TEST_WRAPPER $*" 2>&1 | @TEE@ /dev/fd/6) |
|---|
| 78 | exit_code=$? |
|---|
| 79 | script_error_disable_= |
|---|
| 80 | echo "----- Finished with exit code $exit_code (cmd: \"$*\"" |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | begin_test() { |
|---|
| 84 | let ++test_num |
|---|
| 85 | test_name=$1 |
|---|
| 86 | test_err= |
|---|
| 87 | test_var= |
|---|
| 88 | echo "" |
|---|
| 89 | echo "$test_num $test_name" |
|---|
| 90 | echo "$test_num $test_name" | sed 's/./=/g' |
|---|
| 91 | echo "" |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | end_test() { |
|---|
| 95 | if [ -n "$test_err" ]; then |
|---|
| 96 | do_clean=no |
|---|
| 97 | echo -n "not " |
|---|
| 98 | fi |
|---|
| 99 | echo "ok $test_num - $test_name" |
|---|
| 100 | if [ -n "$test_var" ]; then |
|---|
| 101 | echo "#$test_var" |
|---|
| 102 | fi |
|---|
| 103 | if [ -n "$test_err" ]; then |
|---|
| 104 | echo "--- BEGIN ERR ---" |
|---|
| 105 | echo "$test_err" |
|---|
| 106 | echo "--- END ERR ---" |
|---|
| 107 | fi |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | assert_exit_success() { |
|---|
| 111 | if [ -n "$1" ]; then |
|---|
| 112 | capture_stdouterr "$@" |
|---|
| 113 | fi |
|---|
| 114 | if [ $exit_code -ne 0 ]; then |
|---|
| 115 | test_err="${test_err}# command exited with $exit_code |
|---|
| 116 | " |
|---|
| 117 | fi |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | assert_exit_failure() { |
|---|
| 121 | if [ -n "$1" ]; then |
|---|
| 122 | capture_stdouterr "$@" |
|---|
| 123 | fi |
|---|
| 124 | if [ $exit_code -eq 0 ]; then |
|---|
| 125 | test_err="${test_err}# command exited with $exit_code |
|---|
| 126 | " |
|---|
| 127 | fi |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | assert_output_contains() { |
|---|
| 131 | if echo "$output" | @GREP@ "$1" >/dev/null; then |
|---|
| 132 | : |
|---|
| 133 | else |
|---|
| 134 | test_err="${test_err}# command output did not include '$1' |
|---|
| 135 | OUTPUT_BEGIN |
|---|
| 136 | $output |
|---|
| 137 | OUTPUT_END |
|---|
| 138 | " |
|---|
| 139 | fi |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | assert_output_not_contains() { |
|---|
| 143 | if echo "$output" | @GREP@ "$1" >/dev/null; then |
|---|
| 144 | test_err="${test_err}# command should not have included '$1' |
|---|
| 145 | OUTPUT_BEGIN |
|---|
| 146 | $output |
|---|
| 147 | OUTPUT_END |
|---|
| 148 | " |
|---|
| 149 | fi |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | assert_output_count() { |
|---|
| 153 | if test $(echo "$output" | @GREP@ -c "$1") $2 $3; then |
|---|
| 154 | : |
|---|
| 155 | else |
|---|
| 156 | test_err="${test_err}# command output did not match $@ |
|---|
| 157 | " |
|---|
| 158 | fi |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | assert_output_value() { |
|---|
| 162 | # convert 8.4e-10 to 8.4*10^-10 (from https://stackoverflow.com/questions/12882611) |
|---|
| 163 | snot2bc='s/([+-]?[0-9.]+)[eE]\+?(-?)([0-9]+)/(\1*10^\2\3)/g' |
|---|
| 164 | var="$1" |
|---|
| 165 | shift |
|---|
| 166 | val=$(echo "$output" | @SED@ -n 's/.*'$var'//p' | @SED@ -E "$snot2bc") |
|---|
| 167 | test_var="${test_var} $var$val" |
|---|
| 168 | while test $# -gt 0; do |
|---|
| 169 | if (( $(echo "$val $1" | @BC@) )); then |
|---|
| 170 | : |
|---|
| 171 | else |
|---|
| 172 | test_err="${test_err}# value for key $var ($val) did not match $1 |
|---|
| 173 | " |
|---|
| 174 | fi |
|---|
| 175 | shift |
|---|
| 176 | done |
|---|
| 177 | } |
|---|