Skip to content

Commit

Permalink
Add logging feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kakawait committed Aug 22, 2018
1 parent e1e6f8d commit 13683c0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Usage: $cmd [-cr] [-f <regex>] [-p | -t] <test>...
-c, --count Count the number of test cases without running any tests
-f, --filter Filter test cases by names matching the regular expression
-h, --help Display this help message
-l, --log Combine output and test logs into given path"
-p, --pretty Show results in pretty format (default for terminals)
-r, --recursive Include tests in subdirectories
-t, --tap Show results in TAP format
Expand Down Expand Up @@ -106,6 +107,12 @@ while [[ "$#" -ne 0 ]]; do
shift
flags+=('-f' "$1")
;;
-l|--log)
shift
flags+=('-l', "$1")
export BATS_LOG_FILE="$1"
touch "$BATS_LOG_FILE"
;;
-r|--recursive)
recursive=1
;;
Expand Down
4 changes: 4 additions & 0 deletions libexec/bats-core/bats-exec-suite
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ while [[ "$#" -ne 0 ]]; do
flags+=('-f' "$filter")
shift
;;
-l)
flags+=('-l' "$2")
shift
;;
-x)
extended_syntax_flag='-x'
flags+=('-x')
Expand Down
34 changes: 34 additions & 0 deletions libexec/bats-core/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -eET
BATS_COUNT_ONLY=''
BATS_TEST_FILTER=''
BATS_EXTENDED_SYNTAX=''
BATS_LOG_FILE=''

# Command and flags used by bats_perform_tests when it re-invokes bats-exec-test
# in a new process to run bats_perform_test.
Expand All @@ -18,6 +19,10 @@ while [[ "$#" -ne 0 ]]; do
BATS_TEST_FILTER="$2"
shift
;;
-l)
BATS_LOG_FILE="$2"
shift
;;
-x)
BATS_EXTENDED_SYNTAX='-x'
BATS_PERFORM_TEST_CMD+=('-x')
Expand Down Expand Up @@ -70,6 +75,12 @@ run() {
IFS=$'\n' lines=($output)
IFS="$origIFS"
set "-$origFlags"

if [ -w "$BATS_LOG_FILE" ]; then
if [ -n "$output" ]; then
printf '%s\n' "$output" >> "$BATS_LOG_FILE"
fi
fi
}

setup() {
Expand All @@ -91,6 +102,10 @@ bats_test_begin() {
if [[ -n "$BATS_EXTENDED_SYNTAX" ]]; then
printf 'begin %d %s\n' "$BATS_TEST_NUMBER" "$BATS_TEST_DESCRIPTION" >&3
fi
if [ -w "$BATS_LOG_FILE" ]; then
printf "[%d] starting '%s'\n" "$$" "$BATS_TEST_DESCRIPTION" >> "$BATS_LOG_FILE"
printf '[%d] <<< begin output >>>\n' "$$" >> "$BATS_LOG_FILE"
fi
setup
}

Expand Down Expand Up @@ -136,20 +151,25 @@ bats_print_stack_trace() {

if [[ $index -eq 1 ]]; then
printf '# ('
[[ -w "$BATS_LOG_FILE" ]] && printf '# (' >> "$BATS_LOG_FILE"
else
printf '# '
[[ -w "$BATS_LOG_FILE" ]] && printf '# ' >> "$BATS_LOG_FILE"
fi

local fn
bats_frame_function "$frame" 'fn'
if [[ "$fn" != "$BATS_TEST_NAME" ]]; then
printf "from function \`%s' " "$fn"
[[ -w "$BATS_LOG_FILE" ]] && printf "from function \`%s' " "$fn" >> "$BATS_LOG_FILE"
fi

if [[ $index -eq $count ]]; then
printf 'in test file %s, line %d)\n' "$filename" "$lineno"
[[ -w "$BATS_LOG_FILE" ]] && printf 'in test file %s, line %d)\n' "$filename" "$lineno" >> "$BATS_LOG_FILE"
else
printf 'in file %s, line %d,\n' "$filename" "$lineno"
[[ -w "$BATS_LOG_FILE" ]] && printf 'in file %s, line %d,\n' "$filename" "$lineno" >> "$BATS_LOG_FILE"
fi

((++index))
Expand All @@ -168,11 +188,14 @@ bats_print_failed_command() {
bats_extract_line "$filename" "$lineno" 'failed_line'
bats_strip_string "$failed_line" 'failed_command'
printf '%s' "# \`${failed_command}' "
[[ -w "$BATS_LOG_FILE" ]] && printf '%s' "# \`${failed_command}' " >> "$BATS_LOG_FILE"

if [[ "$BATS_ERROR_STATUS" -eq 1 ]]; then
printf 'failed\n'
[[ -w "$BATS_LOG_FILE" ]] && printf 'failed\n' >> "$BATS_LOG_FILE"
else
printf 'failed with status %d\n' "$BATS_ERROR_STATUS"
[[ -w "$BATS_LOG_FILE" ]] && printf 'failed with status %d\n' "$BATS_ERROR_STATUS" >> "$BATS_LOG_FILE"
fi
}

Expand Down Expand Up @@ -266,6 +289,7 @@ bats_exit_trap() {
local line
local status
local skipped=''
local log=''
trap - err exit

if [[ -n "$BATS_TEST_SKIPPED" ]]; then
Expand All @@ -275,6 +299,10 @@ bats_exit_trap() {
fi
fi

if [ -w "$BATS_LOG_FILE" ]; then
printf '[%d] <<< end output >>>\n' "$$" >> "$BATS_LOG_FILE"
fi

if [[ -z "$BATS_TEST_COMPLETED" || -z "$BATS_TEARDOWN_COMPLETED" ]]; then
if [[ "$BATS_ERROR_STATUS" -eq 0 ]]; then
# For some versions of bash, `$?` may not be set properly for some error
Expand All @@ -300,10 +328,16 @@ bats_exit_trap() {
printf '# %s\n' "$line"
fi
status=1
log="not ok '$BATS_TEST_DESCRIPTION'"
else
printf 'ok %d %s%s\n' "$BATS_TEST_NUMBER" "$BATS_TEST_DESCRIPTION" \
"$skipped" >&3
status=0
log="ok '$BATS_TEST_DESCRIPTION'"
fi

if [ -w "$BATS_LOG_FILE" ]; then
printf '[%d] %s\n' "$$" "$log" >> "$BATS_LOG_FILE"
fi

rm -f "$BATS_OUT"
Expand Down

0 comments on commit 13683c0

Please sign in to comment.