Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
Support logging to specified file
Browse files Browse the repository at this point in the history
Currently, bats puts logs to a temporary file and removes the file
at the end of the test. This is not convenient for observing the logs.
This commit makes it possible for user to specify a log file and
observe the logs during test or for post test analyzing. A new option
named -l (or --logfile) is added for this function.
  • Loading branch information
jinuxstyle committed Jul 1, 2016
1 parent 0d743bc commit d7aea2a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
21 changes: 19 additions & 2 deletions libexec/bats
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version() {

usage() {
version
echo "Usage: bats [-c] [-p | -t] <test> [<test> ...]"
echo "Usage: bats [-c] [-p | -t] [-l <file>] <test> [<test> ...]"
}

help() {
Expand All @@ -21,6 +21,7 @@ help() {
echo " -p, --pretty Show results in pretty format (default for terminals)"
echo " -t, --tap Show results in TAP format"
echo " -v, --version Display the version number"
echo " -l, --logfile Print logs to specified file"
echo
echo " For more information, see https://github.com/sstephenson/bats"
echo
Expand Down Expand Up @@ -57,7 +58,7 @@ export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")"
export BATS_CWD="$(abs_dirname .)"
export PATH="$BATS_LIBEXEC:$PATH"

unset count_flag pretty
unset count_flag pretty log_file
[ -t 0 ] && [ -t 1 ] && pretty="1"
[ -n "$CI" ] && pretty=""

Expand Down Expand Up @@ -106,6 +107,16 @@ while [[ $# -gt 0 ]]; do
"-p" | "--pretty" )
pretty="1"
;;
"-l" | "--logfile" )
# Bail out if the option is specified in a group or it's not
# given a value
if [ "$opt_grouped" = "true" ] || [ "$2" = "" ]; then
usage >&2
exit 1
fi
log_file="$2"
shift
;;
* )
usage >&2
exit 1
Expand All @@ -117,6 +128,12 @@ while [[ $# -gt 0 ]]; do
continue
done

if [ -n "$log_file" ]; then
echo -n > $log_file
fi

export BATS_LOG_FILE="$log_file"

if [ "${#arguments[@]}" -eq 0 ]; then
usage >&2
exit 1
Expand Down
13 changes: 11 additions & 2 deletions libexec/bats-exec-test
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ bats_exit_trap() {
status=0
fi

rm -f "$BATS_OUT"
if [ -n "$BATS_OUT_TEMP" ]; then
rm -f $BATS_OUT_TEMP
fi

exit "$status"
}

Expand Down Expand Up @@ -309,7 +312,13 @@ fi

BATS_TMPNAME="$BATS_TMPDIR/bats.$$"
BATS_PARENT_TMPNAME="$BATS_TMPDIR/bats.$PPID"
BATS_OUT="${BATS_TMPNAME}.out"

if [ -z "$BATS_LOG_FILE" ]; then
BATS_OUT="${BATS_TMPNAME}.out"
BATS_OUT_TEMP=$BATS_OUT
else
BATS_OUT=$BATS_LOG_FILE
fi

bats_preprocess_source() {
BATS_TEST_SOURCE="${BATS_TMPNAME}.src"
Expand Down

0 comments on commit d7aea2a

Please sign in to comment.