|
| 1 | +# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other |
| 2 | +# sbang project developers. See the top-level COPYRIGHT file for details. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 5 | + |
| 6 | +# |
| 7 | +# A testing framework for any POSIX-compatible shell. |
| 8 | +# |
| 9 | + |
| 10 | +# ------------------------------------------------------------------------ |
| 11 | +# Functions for color output. |
| 12 | +# ------------------------------------------------------------------------ |
| 13 | + |
| 14 | +# Colors for output |
| 15 | +red='\033[1;31m' |
| 16 | +cyan='\033[1;36m' |
| 17 | +green='\033[1;32m' |
| 18 | +reset='\033[0m' |
| 19 | + |
| 20 | +echo_red() { |
| 21 | + printf "${red}$*${reset}\n" |
| 22 | +} |
| 23 | + |
| 24 | +echo_green() { |
| 25 | + printf "${green}$*${reset}\n" |
| 26 | +} |
| 27 | + |
| 28 | +echo_msg() { |
| 29 | + printf "${cyan}$*${reset}\n" |
| 30 | +} |
| 31 | + |
| 32 | +# ------------------------------------------------------------------------ |
| 33 | +# Generic functions for testing shell code. |
| 34 | +# ------------------------------------------------------------------------ |
| 35 | + |
| 36 | +# counts of test successes and failures. |
| 37 | +success=0 |
| 38 | +errors=0 |
| 39 | + |
| 40 | +# Print out a header for a group of tests. |
| 41 | +title() { |
| 42 | + echo |
| 43 | + echo_msg "$@" |
| 44 | + echo_msg "---------------------------------" |
| 45 | +} |
| 46 | + |
| 47 | +# echo FAIL in red text; increment failures |
| 48 | +fail() { |
| 49 | + echo_red FAIL |
| 50 | + errors=$((errors+1)) |
| 51 | +} |
| 52 | + |
| 53 | +# |
| 54 | +# Echo SUCCESS in green; increment successes |
| 55 | +# |
| 56 | +pass() { |
| 57 | + echo_green SUCCESS |
| 58 | + success=$((success+1)) |
| 59 | +} |
| 60 | + |
| 61 | +# |
| 62 | +# Run a command and suppress output unless it fails. |
| 63 | +# On failure, echo the exit code and output. |
| 64 | +# |
| 65 | +succeeds() { |
| 66 | + printf "'%s' succeeds ... " "$*" |
| 67 | + output=$("$@" 2>&1) |
| 68 | + err="$?" |
| 69 | + |
| 70 | + if [ "$err" != 0 ]; then |
| 71 | + fail |
| 72 | + echo_red "Command failed with error $err." |
| 73 | + if [ -n "$output" ]; then |
| 74 | + echo_msg "Output:" |
| 75 | + echo "$output" |
| 76 | + else |
| 77 | + echo_msg "No output." |
| 78 | + fi |
| 79 | + else |
| 80 | + pass |
| 81 | + fi |
| 82 | +} |
| 83 | + |
| 84 | +# |
| 85 | +# Run a command and suppress output unless it succeeds. |
| 86 | +# If the command succeeds, echo the output. |
| 87 | +# |
| 88 | +fails() { |
| 89 | + printf "'%s' fails ... " "$*" |
| 90 | + output=$("$@" 2>&1) |
| 91 | + err="$?" |
| 92 | + |
| 93 | + if [ "$err" = 0 ]; then |
| 94 | + fail |
| 95 | + echo_red "Command failed with error $err." |
| 96 | + if [ -n "$output" ]; then |
| 97 | + echo_msg "Output:" |
| 98 | + echo "$output" |
| 99 | + else |
| 100 | + echo_msg "No output." |
| 101 | + fi |
| 102 | + else |
| 103 | + pass |
| 104 | + fi |
| 105 | +} |
| 106 | + |
| 107 | +# |
| 108 | +# Ensure that a string is in the output of a command. |
| 109 | +# Suppresses output on success. |
| 110 | +# On failure, echo the exit code and output. |
| 111 | +# |
| 112 | +contains() { |
| 113 | + string="$1" |
| 114 | + shift |
| 115 | + |
| 116 | + printf "'%s' output contains '$string' ... " "$*" |
| 117 | + output=$("$@" 2>&1) |
| 118 | + err="$?" |
| 119 | + |
| 120 | + if [ "${output#*$string}" = "${output}" ]; then |
| 121 | + fail |
| 122 | + echo_red "Command exited with error $err." |
| 123 | + echo_red "'$string' was not in output." |
| 124 | + if [ -n "$output" ]; then |
| 125 | + echo_msg "Output:" |
| 126 | + echo "$output" |
| 127 | + else |
| 128 | + echo_msg "No output." |
| 129 | + fi |
| 130 | + else |
| 131 | + pass |
| 132 | + fi |
| 133 | +} |
| 134 | + |
| 135 | +# |
| 136 | +# Ensure that a variable is set. |
| 137 | +# |
| 138 | +is_set() { |
| 139 | + printf "'%s' is set ... " "$1" |
| 140 | + if eval "[ -z \${${1:-}+x} ]"; then |
| 141 | + fail |
| 142 | + echo_msg "$1 was not set!" |
| 143 | + else |
| 144 | + pass |
| 145 | + fi |
| 146 | +} |
| 147 | + |
| 148 | +# |
| 149 | +# Ensure that a variable is not set. |
| 150 | +# Fails and prints the value of the variable if it is set. |
| 151 | +# |
| 152 | +is_not_set() { |
| 153 | + printf "'%s' is not set ... " "$1" |
| 154 | + if eval "[ ! -z \${${1:-}+x} ]"; then |
| 155 | + fail |
| 156 | + echo_msg "$1 was set:" |
| 157 | + echo " $1" |
| 158 | + else |
| 159 | + pass |
| 160 | + fi |
| 161 | +} |
| 162 | + |
| 163 | +# |
| 164 | +# Report the number of tests that succeeded and failed on exit. |
| 165 | +# |
| 166 | +teardown() { |
| 167 | + if [ "$?" != 0 ]; then |
| 168 | + trapped_error=true |
| 169 | + else |
| 170 | + trapped_error=false |
| 171 | + fi |
| 172 | + |
| 173 | + if type cleanup &> /dev/null |
| 174 | + then |
| 175 | + cleanup |
| 176 | + fi |
| 177 | + |
| 178 | + echo |
| 179 | + echo "$success tests succeeded." |
| 180 | + echo "$errors tests failed." |
| 181 | + |
| 182 | + if [ "$trapped_error" = true ]; then |
| 183 | + echo "Exited due to an error." |
| 184 | + fi |
| 185 | + |
| 186 | + if [ "$errors" = 0 ] && [ "$trapped_error" = false ]; then |
| 187 | + pass |
| 188 | + exit 0 |
| 189 | + else |
| 190 | + fail |
| 191 | + exit 1 |
| 192 | + fi |
| 193 | +} |
| 194 | + |
| 195 | +trap teardown EXIT |
0 commit comments