gistfile1.txt
· 1.1 KiB · Text
Исходник
function cmd::test::run_cmd() {
local desc="$1" expected="${2:-}"
shift 2
local tmp exit_code
tmp=$(mktemp)
set +e
timeout 30 "$WGCTL_BINARY" "$@" > "$tmp" 2>&1
exit_code=$?
set -e
# Reset terminal color in case command output left ANSI state dirty
printf "\033[0m" >&2
if [[ $exit_code -eq 124 ]]; then
test::warn "${desc} (timed out after 30s)"
rm -f "$tmp"
return 1
fi
local clean
clean=$(cmd::test::_strip_ansi < "$tmp")
if [[ $exit_code -ne 0 ]]; then
local msg="${desc}"
[[ -n "$expected" ]] && msg="${desc} (expected '${expected}', command failed)"
test::fail "$msg"
if [[ "${WGCTL_TEST_VERBOSE:-false}" == "true" ]]; then
printf " Output: %s\n" "$(echo "$clean" | head -3 | tr '\n' ' ')"
fi
rm -f "$tmp"
return 1
fi
if [[ -n "$expected" ]] && ! echo "$clean" | grep -qF "$expected"; then
local actual
actual=$(echo "$clean" | head -3 | tr '\n' ' ' | sed 's/ */ /g' | cut -c1-100)
test::fail "${desc} (expected '${expected}', got: '${actual}')"
rm -f "$tmp"
return 1
fi
test::pass "$desc"
rm -f "$tmp"
}
| 1 | function cmd::test::run_cmd() { |
| 2 | local desc="$1" expected="${2:-}" |
| 3 | shift 2 |
| 4 | |
| 5 | local tmp exit_code |
| 6 | tmp=$(mktemp) |
| 7 | |
| 8 | set +e |
| 9 | timeout 30 "$WGCTL_BINARY" "$@" > "$tmp" 2>&1 |
| 10 | exit_code=$? |
| 11 | set -e |
| 12 | |
| 13 | # Reset terminal color in case command output left ANSI state dirty |
| 14 | printf "\033[0m" >&2 |
| 15 | |
| 16 | if [[ $exit_code -eq 124 ]]; then |
| 17 | test::warn "${desc} (timed out after 30s)" |
| 18 | rm -f "$tmp" |
| 19 | return 1 |
| 20 | fi |
| 21 | |
| 22 | local clean |
| 23 | clean=$(cmd::test::_strip_ansi < "$tmp") |
| 24 | |
| 25 | if [[ $exit_code -ne 0 ]]; then |
| 26 | local msg="${desc}" |
| 27 | [[ -n "$expected" ]] && msg="${desc} (expected '${expected}', command failed)" |
| 28 | test::fail "$msg" |
| 29 | if [[ "${WGCTL_TEST_VERBOSE:-false}" == "true" ]]; then |
| 30 | printf " Output: %s\n" "$(echo "$clean" | head -3 | tr '\n' ' ')" |
| 31 | fi |
| 32 | rm -f "$tmp" |
| 33 | return 1 |
| 34 | fi |
| 35 | |
| 36 | if [[ -n "$expected" ]] && ! echo "$clean" | grep -qF "$expected"; then |
| 37 | local actual |
| 38 | actual=$(echo "$clean" | head -3 | tr '\n' ' ' | sed 's/ */ /g' | cut -c1-100) |
| 39 | test::fail "${desc} (expected '${expected}', got: '${actual}')" |
| 40 | rm -f "$tmp" |
| 41 | return 1 |
| 42 | fi |
| 43 | |
| 44 | test::pass "$desc" |
| 45 | rm -f "$tmp" |
| 46 | } |