From 13683c06f370626fe512c49c4dd49f679392bc6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibaud=20Lepr=C3=AAtre?= Date: Wed, 22 Aug 2018 10:47:49 +0200 Subject: [PATCH] Add logging feature report from sstephenson/bats#123 --- libexec/bats-core/bats | 7 +++++++ libexec/bats-core/bats-exec-suite | 4 ++++ libexec/bats-core/bats-exec-test | 34 +++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/libexec/bats-core/bats b/libexec/bats-core/bats index d3e2f475335..e513c4650d4 100755 --- a/libexec/bats-core/bats +++ b/libexec/bats-core/bats @@ -29,6 +29,7 @@ Usage: $cmd [-cr] [-f ] [-p | -t] ... -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 @@ -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 ;; diff --git a/libexec/bats-core/bats-exec-suite b/libexec/bats-core/bats-exec-suite index 53dc43db1c3..8aabce8a33b 100755 --- a/libexec/bats-core/bats-exec-suite +++ b/libexec/bats-core/bats-exec-suite @@ -16,6 +16,10 @@ while [[ "$#" -ne 0 ]]; do flags+=('-f' "$filter") shift ;; + -l) + flags+=('-l' "$2") + shift + ;; -x) extended_syntax_flag='-x' flags+=('-x') diff --git a/libexec/bats-core/bats-exec-test b/libexec/bats-core/bats-exec-test index 7a940743914..e4045f36cd3 100755 --- a/libexec/bats-core/bats-exec-test +++ b/libexec/bats-core/bats-exec-test @@ -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. @@ -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') @@ -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() { @@ -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 } @@ -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)) @@ -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 } @@ -266,6 +289,7 @@ bats_exit_trap() { local line local status local skipped='' + local log='' trap - err exit if [[ -n "$BATS_TEST_SKIPPED" ]]; then @@ -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 @@ -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"