|
| 1 | +#!/bin/bash |
| 2 | +# assert.sh 1.1 - bash unit testing framework |
| 3 | +# Copyright (C) 2009-2015 Robert Lehmann |
| 4 | +# |
| 5 | +# http://github.com/lehmannro/assert.sh |
| 6 | +# |
| 7 | +# This program is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU Lesser General Public License as published |
| 9 | +# by the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public License |
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +export DISCOVERONLY=${DISCOVERONLY:-} |
| 21 | +export DEBUG=${DEBUG:-} |
| 22 | +export STOP=${STOP:-} |
| 23 | +export INVARIANT=${INVARIANT:-} |
| 24 | +export CONTINUE=${CONTINUE:-} |
| 25 | + |
| 26 | +args="$(getopt -n "$0" -l \ |
| 27 | + verbose,help,stop,discover,invariant,continue vhxdic $*)" \ |
| 28 | +|| exit -1 |
| 29 | +for arg in $args; do |
| 30 | + case "$arg" in |
| 31 | + -h) |
| 32 | + echo "$0 [-vxidc]" \ |
| 33 | + "[--verbose] [--stop] [--invariant] [--discover] [--continue]" |
| 34 | + echo "`sed 's/./ /g' <<< "$0"` [-h] [--help]" |
| 35 | + exit 0;; |
| 36 | + --help) |
| 37 | + cat <<EOF |
| 38 | +Usage: $0 [options] |
| 39 | +Language-agnostic unit tests for subprocesses. |
| 40 | +
|
| 41 | +Options: |
| 42 | + -v, --verbose generate output for every individual test case |
| 43 | + -x, --stop stop running tests after the first failure |
| 44 | + -i, --invariant do not measure timings to remain invariant between runs |
| 45 | + -d, --discover collect test suites only, do not run any tests |
| 46 | + -c, --continue do not modify exit code to test suite status |
| 47 | + -h show brief usage information and exit |
| 48 | + --help show this help message and exit |
| 49 | +EOF |
| 50 | + exit 0;; |
| 51 | + -v|--verbose) |
| 52 | + DEBUG=1;; |
| 53 | + -x|--stop) |
| 54 | + STOP=1;; |
| 55 | + -i|--invariant) |
| 56 | + INVARIANT=1;; |
| 57 | + -d|--discover) |
| 58 | + DISCOVERONLY=1;; |
| 59 | + -c|--continue) |
| 60 | + CONTINUE=1;; |
| 61 | + esac |
| 62 | +done |
| 63 | + |
| 64 | +_indent=$'\n\t' # local format helper |
| 65 | + |
| 66 | +_assert_reset() { |
| 67 | + tests_ran=0 |
| 68 | + tests_failed=0 |
| 69 | + tests_errors=() |
| 70 | + tests_starttime="$(date +%s%N)" # nanoseconds_since_epoch |
| 71 | +} |
| 72 | + |
| 73 | +assert_end() { |
| 74 | + # assert_end [suite ..] |
| 75 | + tests_endtime="$(date +%s%N)" |
| 76 | + # required visible decimal place for seconds (leading zeros if needed) |
| 77 | + local tests_time="$( \ |
| 78 | + printf "%010d" "$(( ${tests_endtime/%N/000000000} |
| 79 | + - ${tests_starttime/%N/000000000} ))")" # in ns |
| 80 | + tests="$tests_ran ${*:+$* }tests" |
| 81 | + [[ -n "$DISCOVERONLY" ]] && echo "collected $tests." && _assert_reset && return |
| 82 | + [[ -n "$DEBUG" ]] && echo |
| 83 | + # to get report_time split tests_time on 2 substrings: |
| 84 | + # ${tests_time:0:${#tests_time}-9} - seconds |
| 85 | + # ${tests_time:${#tests_time}-9:3} - milliseconds |
| 86 | + [[ -z "$INVARIANT" ]] \ |
| 87 | + && report_time=" in ${tests_time:0:${#tests_time}-9}.${tests_time:${#tests_time}-9:3}s" \ |
| 88 | + || report_time= |
| 89 | + |
| 90 | + if [[ "$tests_failed" -eq 0 ]]; then |
| 91 | + echo "all $tests passed$report_time." |
| 92 | + else |
| 93 | + for error in "${tests_errors[@]}"; do echo "$error"; done |
| 94 | + echo "$tests_failed of $tests failed$report_time." |
| 95 | + fi |
| 96 | + tests_failed_previous=$tests_failed |
| 97 | + [[ $tests_failed -gt 0 ]] && tests_suite_status=1 |
| 98 | + _assert_reset |
| 99 | +} |
| 100 | + |
| 101 | +assert() { |
| 102 | + # assert <command> <expected stdout> [stdin] |
| 103 | + (( tests_ran++ )) || : |
| 104 | + [[ -z "$DISCOVERONLY" ]] || return |
| 105 | + expected=$(echo -ne "${2:-}") |
| 106 | + result="$(eval 2>/dev/null $1 <<< ${3:-})" || true |
| 107 | + if [[ "$result" == "$expected" ]]; then |
| 108 | + [[ -z "$DEBUG" ]] || echo -n . |
| 109 | + return |
| 110 | + fi |
| 111 | + result="$(sed -e :a -e '$!N;s/\n/\\n/;ta' <<< "$result")" |
| 112 | + [[ -z "$result" ]] && result="nothing" || result="\"$result\"" |
| 113 | + [[ -z "$2" ]] && expected="nothing" || expected="\"$2\"" |
| 114 | + _assert_fail "expected $expected${_indent}got $result" "$1" "$3" |
| 115 | +} |
| 116 | + |
| 117 | +assert_raises() { |
| 118 | + # assert_raises <command> <expected code> [stdin] |
| 119 | + (( tests_ran++ )) || : |
| 120 | + [[ -z "$DISCOVERONLY" ]] || return |
| 121 | + status=0 |
| 122 | + (eval $1 <<< ${3:-}) > /dev/null 2>&1 || status=$? |
| 123 | + expected=${2:-0} |
| 124 | + if [[ "$status" -eq "$expected" ]]; then |
| 125 | + [[ -z "$DEBUG" ]] || echo -n . |
| 126 | + return |
| 127 | + fi |
| 128 | + _assert_fail "program terminated with code $status instead of $expected" "$1" "$3" |
| 129 | +} |
| 130 | + |
| 131 | +_assert_fail() { |
| 132 | + # _assert_fail <failure> <command> <stdin> |
| 133 | + [[ -n "$DEBUG" ]] && echo -n X |
| 134 | + report="test #$tests_ran \"$2${3:+ <<< $3}\" failed:${_indent}$1" |
| 135 | + if [[ -n "$STOP" ]]; then |
| 136 | + [[ -n "$DEBUG" ]] && echo |
| 137 | + echo "$report" |
| 138 | + exit 1 |
| 139 | + fi |
| 140 | + tests_errors[$tests_failed]="$report" |
| 141 | + (( tests_failed++ )) || : |
| 142 | +} |
| 143 | + |
| 144 | +skip_if() { |
| 145 | + # skip_if <command ..> |
| 146 | + (eval $@) > /dev/null 2>&1 && status=0 || status=$? |
| 147 | + [[ "$status" -eq 0 ]] || return |
| 148 | + skip |
| 149 | +} |
| 150 | + |
| 151 | +skip() { |
| 152 | + # skip (no arguments) |
| 153 | + shopt -q extdebug && tests_extdebug=0 || tests_extdebug=1 |
| 154 | + shopt -q -o errexit && tests_errexit=0 || tests_errexit=1 |
| 155 | + # enable extdebug so returning 1 in a DEBUG trap handler skips next command |
| 156 | + shopt -s extdebug |
| 157 | + # disable errexit (set -e) so we can safely return 1 without causing exit |
| 158 | + set +o errexit |
| 159 | + tests_trapped=0 |
| 160 | + trap _skip DEBUG |
| 161 | +} |
| 162 | +_skip() { |
| 163 | + if [[ $tests_trapped -eq 0 ]]; then |
| 164 | + # DEBUG trap for command we want to skip. Do not remove the handler |
| 165 | + # yet because *after* the command we need to reset extdebug/errexit (in |
| 166 | + # another DEBUG trap.) |
| 167 | + tests_trapped=1 |
| 168 | + [[ -z "$DEBUG" ]] || echo -n s |
| 169 | + return 1 |
| 170 | + else |
| 171 | + trap - DEBUG |
| 172 | + [[ $tests_extdebug -eq 0 ]] || shopt -u extdebug |
| 173 | + [[ $tests_errexit -eq 1 ]] || set -o errexit |
| 174 | + return 0 |
| 175 | + fi |
| 176 | +} |
| 177 | + |
| 178 | + |
| 179 | +_assert_reset |
| 180 | +: ${tests_suite_status:=0} # remember if any of the tests failed so far |
| 181 | +_assert_cleanup() { |
| 182 | + local status=$? |
| 183 | + # modify exit code if it's not already non-zero |
| 184 | + [[ $status -eq 0 && -z $CONTINUE ]] && exit $tests_suite_status |
| 185 | +} |
| 186 | +trap _assert_cleanup EXIT |
0 commit comments