Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | # we always start with no errors |
| 16 | ERROR=0 |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 17 | PASS=0 |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 18 | FAILED_FUNCS="" |
| 19 | |
Ghanshyam Mann | f073640 | 2021-05-18 17:15:30 -0500 | [diff] [blame] | 20 | export PYTHON=$(which python3 2>/dev/null) |
| 21 | |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 22 | # pass a test, printing out MSG |
| 23 | # usage: passed message |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 24 | function passed { |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 25 | local lineno |
| 26 | lineno=$(caller 0 | awk '{print $1}') |
| 27 | local function |
| 28 | function=$(caller 0 | awk '{print $2}') |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 29 | local msg="$1" |
| 30 | if [ -z "$msg" ]; then |
| 31 | msg="OK" |
| 32 | fi |
| 33 | PASS=$((PASS+1)) |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 34 | echo "PASS: $function:L$lineno - $msg" |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 35 | } |
| 36 | |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 37 | # fail a test, printing out MSG |
| 38 | # usage: failed message |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 39 | function failed { |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 40 | local lineno |
| 41 | lineno=$(caller 0 | awk '{print $1}') |
| 42 | local function |
| 43 | function=$(caller 0 | awk '{print $2}') |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 44 | local msg="$1" |
| 45 | FAILED_FUNCS+="$function:L$lineno\n" |
| 46 | echo "ERROR: $function:L$lineno!" |
| 47 | echo " $msg" |
| 48 | ERROR=$((ERROR+1)) |
| 49 | } |
| 50 | |
Swapnil Kulkarni (coolsvap) | 7f0be4f | 2015-11-20 10:52:59 +0530 | [diff] [blame] | 51 | # assert string comparison of val1 equal val2, printing out msg |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 52 | # usage: assert_equal val1 val2 msg |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 53 | function assert_equal { |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 54 | local lineno |
| 55 | lineno=`caller 0 | awk '{print $1}'` |
| 56 | local function |
| 57 | function=`caller 0 | awk '{print $2}'` |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 58 | local msg=$3 |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 59 | |
| 60 | if [ -z "$msg" ]; then |
| 61 | msg="OK" |
| 62 | fi |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 63 | if [[ "$1" != "$2" ]]; then |
| 64 | FAILED_FUNCS+="$function:L$lineno\n" |
| 65 | echo "ERROR: $1 != $2 in $function:L$lineno!" |
| 66 | echo " $msg" |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 67 | ERROR=$((ERROR+1)) |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 68 | else |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 69 | PASS=$((PASS+1)) |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 70 | echo "PASS: $function:L$lineno - $msg" |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 71 | fi |
| 72 | } |
| 73 | |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 74 | # assert variable is empty/blank, printing out msg |
| 75 | # usage: assert_empty VAR msg |
| 76 | function assert_empty { |
Ian Wienand | ada886d | 2015-10-07 14:06:26 +1100 | [diff] [blame] | 77 | local lineno |
| 78 | lineno=`caller 0 | awk '{print $1}'` |
| 79 | local function |
| 80 | function=`caller 0 | awk '{print $2}'` |
Ian Wienand | b997db6 | 2015-07-22 10:05:32 +1000 | [diff] [blame] | 81 | local msg=$2 |
| 82 | |
| 83 | if [ -z "$msg" ]; then |
| 84 | msg="OK" |
| 85 | fi |
| 86 | if [[ ! -z ${!1} ]]; then |
| 87 | FAILED_FUNCS+="$function:L$lineno\n" |
| 88 | echo "ERROR: $1 not empty in $function:L$lineno!" |
| 89 | echo " $msg" |
| 90 | ERROR=$((ERROR+1)) |
| 91 | else |
| 92 | PASS=$((PASS+1)) |
| 93 | echo "PASS: $function:L$lineno - $msg" |
| 94 | fi |
| 95 | } |
| 96 | |
Ian Wienand | 2ba36cd | 2015-11-12 13:52:36 +1100 | [diff] [blame] | 97 | # assert the arguments evaluate to true |
| 98 | # assert_true "message" arg1 arg2 |
| 99 | function assert_true { |
| 100 | local lineno |
| 101 | lineno=`caller 0 | awk '{print $1}'` |
| 102 | local function |
| 103 | function=`caller 0 | awk '{print $2}'` |
| 104 | local msg=$1 |
| 105 | shift |
| 106 | |
| 107 | $@ |
| 108 | if [ $? -eq 0 ]; then |
| 109 | PASS=$((PASS+1)) |
| 110 | echo "PASS: $function:L$lineno - $msg" |
| 111 | else |
| 112 | FAILED_FUNCS+="$function:L$lineno\n" |
| 113 | echo "ERROR: test failed in $function:L$lineno!" |
| 114 | echo " $msg" |
| 115 | ERROR=$((ERROR+1)) |
| 116 | fi |
| 117 | } |
| 118 | |
| 119 | # assert the arguments evaluate to false |
| 120 | # assert_false "message" arg1 arg2 |
| 121 | function assert_false { |
| 122 | local lineno |
| 123 | lineno=`caller 0 | awk '{print $1}'` |
| 124 | local function |
| 125 | function=`caller 0 | awk '{print $2}'` |
| 126 | local msg=$1 |
| 127 | shift |
| 128 | |
| 129 | $@ |
| 130 | if [ $? -eq 0 ]; then |
| 131 | FAILED_FUNCS+="$function:L$lineno\n" |
| 132 | echo "ERROR: test failed in $function:L$lineno!" |
| 133 | echo " $msg" |
| 134 | ERROR=$((ERROR+1)) |
| 135 | else |
| 136 | PASS=$((PASS+1)) |
| 137 | echo "PASS: $function:L$lineno - $msg" |
| 138 | fi |
| 139 | } |
| 140 | |
| 141 | |
Ian Wienand | ca7e4f2 | 2015-11-13 11:15:15 +1100 | [diff] [blame] | 142 | # Print a summary of passing and failing tests and exit |
| 143 | # (with an error if we have failed tests) |
Ian Wienand | 165afa2 | 2015-05-25 11:29:48 +1000 | [diff] [blame] | 144 | # usage: report_results |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 145 | function report_results { |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 146 | echo "$PASS Tests PASSED" |
Ian Wienand | ca7e4f2 | 2015-11-13 11:15:15 +1100 | [diff] [blame] | 147 | if [[ $ERROR -gt 0 ]]; then |
Ian Wienand | 1cb809d | 2015-04-17 12:55:38 +1000 | [diff] [blame] | 148 | echo |
| 149 | echo "The following $ERROR tests FAILED" |
| 150 | echo -e "$FAILED_FUNCS" |
| 151 | echo "---" |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 152 | exit 1 |
| 153 | fi |
Ian Wienand | ca7e4f2 | 2015-11-13 11:15:15 +1100 | [diff] [blame] | 154 | exit 0 |
Sean Dague | 5375329 | 2014-12-04 19:38:15 -0500 | [diff] [blame] | 155 | } |