| 1 | #!/usr/bin/env bash |
| 2 | # Usage: . testlib.sh |
| 3 | # Simple shell command language test library. |
| 4 | # |
| 5 | # Tests must follow the basic form: |
| 6 | # |
| 7 | # begin_test "the thing" |
| 8 | # ( |
| 9 | # set -e |
| 10 | # echo "hello" |
| 11 | # false |
| 12 | # ) |
| 13 | # end_test |
| 14 | # |
| 15 | # When a test fails its stdout and stderr are shown. |
| 16 | # |
| 17 | # Note that tests must `set -e' within the subshell block or failed assertions |
| 18 | # will not cause the test to fail and the result may be misreported. |
| 19 | # |
| 20 | # Copyright (c) 2011-13 by Ryan Tomayko <http://tomayko.com> |
| 21 | # License: MIT |
| 22 | |
| 23 | fullfile="$(pwd)/$0" |
| 24 | |
| 25 | . "$(dirname "$0")/testenv.sh" |
| 26 | set -e |
| 27 | |
| 28 | # keep track of num tests and failures |
| 29 | tests=0 |
| 30 | failures=0 |
| 31 | |
| 32 | # this runs at process exit |
| 33 | atexit () { |
| 34 | tap_show_plan "$tests" |
| 35 | shutdown |
| 36 | |
| 37 | if [ $failures -gt 0 ]; then |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | exit 0 |
| 42 | } |
| 43 | |
| 44 | # create the trash dir |
| 45 | trap "atexit" SIGKILL SIGINT SIGTERM EXIT |
| 46 | |
| 47 | GITSERVER=undefined |
| 48 | |
| 49 | setup |
| 50 | |
| 51 | GITSERVER=$(cat "$LFS_URL_FILE") |
| 52 | SSLGITSERVER=$(cat "$LFS_SSL_URL_FILE") |
| 53 | CLIENTCERTGITSERVER=$(cat "$LFS_CLIENT_CERT_URL_FILE") |
| 54 | cd "$TRASHDIR" |
| 55 | |
| 56 | # Mark the beginning of a test. A subshell should immediately follow this |
| 57 | # statement. |
| 58 | begin_test () { |
| 59 | test_status=$? |
| 60 | [ -n "$test_description" ] && end_test $test_status |
| 61 | unset test_status |
| 62 | |
| 63 | tests=$(( tests + 1 )) |
| 64 | test_description="$1" |
| 65 | |
| 66 | exec 3>&1 4>&2 |
| 67 | out="$TRASHDIR/out" |
| 68 | err="$TRASHDIR/err" |
| 69 | trace="$TRASHDIR/trace" |
| 70 | |
| 71 | exec 1>"$out" 2>"$err" |
| 72 | |
| 73 | # enabling GIT_TRACE can cause Windows git to stall, esp with fd 5 |
| 74 | # other fd numbers like 8/9 don't stall but still don't work, so disable |
| 75 | if [ $IS_WINDOWS -eq 0 ]; then |
| 76 | exec 5>"$trace" |
| 77 | export GIT_TRACE=5 |
| 78 | fi |
| 79 | |
| 80 | # reset global git config |
| 81 | HOME="$TRASHDIR/home" |
| 82 | rm -rf "$TRASHDIR/home" |
| 83 | mkdir "$HOME" |
| 84 | cp "$TESTHOME/.gitconfig" "$HOME/.gitconfig" |
| 85 | |
| 86 | # allow the subshell to exit non-zero without exiting this process |
| 87 | set -x +e |
| 88 | } |
| 89 | |
| 90 | # Mark the end of a test. |
| 91 | end_test () { |
| 92 | test_status="${1:-$?}" |
| 93 | set +x -e |
| 94 | exec 1>&3 2>&4 |
| 95 | # close fd 5 (GIT_TRACE) |
| 96 | exec 5>&- |
| 97 | |
| 98 | local dump_output="$LFS_DUMP_TEST_OUTPUT" |
| 99 | if [ "$test_status" -eq 0 ]; then |
| 100 | printf "ok %d - %-60s\n" "$tests" "$test_description ..." |
| 101 | else |
| 102 | failures=$(( failures + 1 )) |
| 103 | printf "not ok %d - %-60s\n" "$tests" "$test_description ..." |
| 104 | dump_output=1 |
| 105 | fi |
| 106 | |
| 107 | if [ -n "$dump_output" ] |
| 108 | then |
| 109 | ( |
| 110 | echo "# -- stdout --" |
| 111 | sed 's/^/# /' <"$TRASHDIR/out" |
| 112 | echo "# -- stderr --" |
| 113 | grep -v -e '^\+ end_test' -e '^+ set +x' <"$TRASHDIR/err" | |
| 114 | sed 's/^/# /' |
| 115 | if [ $IS_WINDOWS -eq 0 ]; then |
| 116 | echo "# -- git trace --" |
| 117 | sed 's/^/# /' <"$TRASHDIR/trace" |
| 118 | fi |
| 119 | ) 1>&2 |
| 120 | echo |
| 121 | fi |
| 122 | unset test_description |
| 123 | } |