|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -e |
| 3 | + |
| 4 | +version() { |
| 5 | + echo "Bats 0.4.0" |
| 6 | +} |
| 7 | + |
| 8 | +usage() { |
| 9 | + version |
| 10 | + echo "Usage: bats [-c] [-p | -t] <test> [<test> ...]" |
| 11 | +} |
| 12 | + |
| 13 | +help() { |
| 14 | + usage |
| 15 | + echo |
| 16 | + echo " <test> is the path to a Bats test file, or the path to a directory" |
| 17 | + echo " containing Bats test files." |
| 18 | + echo |
| 19 | + echo " -c, --count Count the number of test cases without running any tests" |
| 20 | + echo " -h, --help Display this help message" |
| 21 | + echo " -p, --pretty Show results in pretty format (default for terminals)" |
| 22 | + echo " -t, --tap Show results in TAP format" |
| 23 | + echo " -v, --version Display the version number" |
| 24 | + echo |
| 25 | + echo " For more information, see https://github.com/sstephenson/bats" |
| 26 | + echo |
| 27 | +} |
| 28 | + |
| 29 | +resolve_link() { |
| 30 | + $(type -p greadlink readlink | head -1) "$1" |
| 31 | +} |
| 32 | + |
| 33 | +abs_dirname() { |
| 34 | + local cwd="$(pwd)" |
| 35 | + local path="$1" |
| 36 | + |
| 37 | + while [ -n "$path" ]; do |
| 38 | + cd "${path%/*}" |
| 39 | + local name="${path##*/}" |
| 40 | + path="$(resolve_link "$name" || true)" |
| 41 | + done |
| 42 | + |
| 43 | + pwd |
| 44 | + cd "$cwd" |
| 45 | +} |
| 46 | + |
| 47 | +expand_path() { |
| 48 | + { cd "$(dirname "$1")" 2>/dev/null |
| 49 | + local dirname="$PWD" |
| 50 | + cd "$OLDPWD" |
| 51 | + echo "$dirname/$(basename "$1")" |
| 52 | + } || echo "$1" |
| 53 | +} |
| 54 | + |
| 55 | +BATS_LIBEXEC="$(abs_dirname "$0")" |
| 56 | +export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")" |
| 57 | +export BATS_CWD="$(abs_dirname .)" |
| 58 | +export PATH="$BATS_LIBEXEC:$PATH" |
| 59 | + |
| 60 | +options=() |
| 61 | +arguments=() |
| 62 | +for arg in "$@"; do |
| 63 | + if [ "${arg:0:1}" = "-" ]; then |
| 64 | + if [ "${arg:1:1}" = "-" ]; then |
| 65 | + options[${#options[*]}]="${arg:2}" |
| 66 | + else |
| 67 | + index=1 |
| 68 | + while option="${arg:$index:1}"; do |
| 69 | + [ -n "$option" ] || break |
| 70 | + options[${#options[*]}]="$option" |
| 71 | + let index+=1 |
| 72 | + done |
| 73 | + fi |
| 74 | + else |
| 75 | + arguments[${#arguments[*]}]="$arg" |
| 76 | + fi |
| 77 | +done |
| 78 | + |
| 79 | +unset count_flag pretty |
| 80 | +[ -t 0 ] && [ -t 1 ] && pretty="1" |
| 81 | +[ -n "$CI" ] && pretty="" |
| 82 | + |
| 83 | +for option in "${options[@]}"; do |
| 84 | + case "$option" in |
| 85 | + "h" | "help" ) |
| 86 | + help |
| 87 | + exit 0 |
| 88 | + ;; |
| 89 | + "v" | "version" ) |
| 90 | + version |
| 91 | + exit 0 |
| 92 | + ;; |
| 93 | + "c" | "count" ) |
| 94 | + count_flag="-c" |
| 95 | + ;; |
| 96 | + "t" | "tap" ) |
| 97 | + pretty="" |
| 98 | + ;; |
| 99 | + "p" | "pretty" ) |
| 100 | + pretty="1" |
| 101 | + ;; |
| 102 | + * ) |
| 103 | + usage >&2 |
| 104 | + exit 1 |
| 105 | + ;; |
| 106 | + esac |
| 107 | +done |
| 108 | + |
| 109 | +if [ "${#arguments[@]}" -eq 0 ]; then |
| 110 | + usage >&2 |
| 111 | + exit 1 |
| 112 | +fi |
| 113 | + |
| 114 | +filenames=() |
| 115 | +for filename in "${arguments[@]}"; do |
| 116 | + if [ -d "$filename" ]; then |
| 117 | + shopt -s nullglob |
| 118 | + for suite_filename in "$(expand_path "$filename")"/*.bats; do |
| 119 | + filenames["${#filenames[@]}"]="$suite_filename" |
| 120 | + done |
| 121 | + shopt -u nullglob |
| 122 | + else |
| 123 | + filenames["${#filenames[@]}"]="$(expand_path "$filename")" |
| 124 | + fi |
| 125 | +done |
| 126 | + |
| 127 | +if [ "${#filenames[@]}" -eq 1 ]; then |
| 128 | + command="bats-exec-test" |
| 129 | +else |
| 130 | + command="bats-exec-suite" |
| 131 | +fi |
| 132 | + |
| 133 | +if [ -n "$pretty" ]; then |
| 134 | + extended_syntax_flag="-x" |
| 135 | + formatter="bats-format-tap-stream" |
| 136 | +else |
| 137 | + extended_syntax_flag="" |
| 138 | + formatter="cat" |
| 139 | +fi |
| 140 | + |
| 141 | +set -o pipefail execfail |
| 142 | +exec "$command" $count_flag $extended_syntax_flag "${filenames[@]}" | "$formatter" |
0 commit comments