Skip to content

Commit

Permalink
Enable test mode outside of unit tests
Browse files Browse the repository at this point in the history
_RUNNING_TEST is only set when getssl is sourced for unit tests.

This adds the undocumented -D switch to set it - and debug mode
when testing.  In this mode, debug output is suppressed unless
an error occurs.
  • Loading branch information
tlhackque committed Jul 31, 2021
1 parent 04b6c25 commit 540cb93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 17 additions & 8 deletions getssl
Original file line number Diff line number Diff line change
Expand Up @@ -2424,7 +2424,7 @@ test_exit() { # Exit hander when running tests
exit_code="$?"

exec 3>&-
exec 3>&123
exec 3>&123 || true
if [[ ${exit_code} -ne 0 ]] && [[ -f "${_TEST_LOG}" ]] ; then
echo "# $(date "+%b %d %T") ${FUNCNAME[1]}:${BASH_LINENO[1]} Test exit code ${exit_code}, log from ${_TEST_LOG} follows" >&3
cat "${_TEST_LOG}" >&3
Expand All @@ -2434,6 +2434,19 @@ test_exit() { # Exit hander when running tests
exit "${exit_code}"
}

test_setup() { # setup for running test
if [[ -z "${_TEST_LOG}" ]] ; then
_TEST_LOG="$(mktemp -t "getssl_test_log_XXXXXXXXXX")"
exec 123>&3- || true
exec 3>"${_TEST_LOG}"
trap "test_exit" EXIT
trap "test_exit" ERR
_USE_DEBUG=1
_RUNNING_TEST=1
info "Test harness detected. Logging to ${_TEST_LOG}"
fi
}

traceback() { # Print function traceback
local i d=1 lbl=" called"
debug "Traceback"
Expand Down Expand Up @@ -2650,13 +2663,7 @@ trap "signal_exit INT" INT
# Save output in a temporary file, which is displayed if an error occurs
_TEST_LOG=""
if [[ ${_RUNNING_TEST} -eq 1 ]] ; then
_TEST_LOG="$(mktemp -t "getssl_test_log_XXXXXXXXXX")"
exec 123>&3-
exec 3>"${_TEST_LOG}"
trap "test_exit" EXIT
trap "test_exit" ERR
_USE_DEBUG=1
info "Test harness detected. Logging to ${_TEST_LOG}"
test_setup
fi

# Parse command-line
Expand All @@ -2668,6 +2675,8 @@ while [[ -n ${1+defined} ]]; do
echo "$PROGNAME V$VERSION"; graceful_exit ;;
-d | --debug)
_USE_DEBUG=1 ;;
-D)
test_setup ;;
-c | --create)
_CREATE_CONFIG=1 ;;
-f | --force)
Expand Down
4 changes: 2 additions & 2 deletions test/test_helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ create_certificate() {
# Create certificate
cp "${CODE_DIR}/test/test-config/${CONFIG_FILE}" "${INSTALL_DIR}/.getssl/${GETSSL_CMD_HOST}/getssl.cfg"
# shellcheck disable=SC2086
run ${CODE_DIR}/getssl "$@" "$GETSSL_CMD_HOST"
run ${CODE_DIR}/getssl "-D $@" "$GETSSL_CMD_HOST"
}

init_getssl() {
# Run initialisation (create account key, etc)
run ${CODE_DIR}/getssl -c "$GETSSL_CMD_HOST"
run ${CODE_DIR}/getssl -D -c "$GETSSL_CMD_HOST"
assert_success
[ -d "$INSTALL_DIR/.getssl" ]
}
Expand Down

0 comments on commit 540cb93

Please sign in to comment.