build/tests/02-log-level.bats

100 lines
3.3 KiB
Bash
Executable File

#! /usr/bin/env bats
# 02-log-level.bats
# In these tests, we ensure that log levels are respected. Log level is passed
# to forgebuild via the `LOG` environment variable. Its possible values are:
# error, info, debug. Casing may be mixed, so LOG=ERROR is the same as LOG=error.
# When the log level is not recognized or unspecified, it defaults to LOG=info.
# LOG=error only prints errors and warnings.
# LOG=info prints errors, warnings and info.
# LOG=debug prints errors, warnings, info and debug (everything).
# LOG=debug is already covered by the test suite in 01-cli-flags.bats so
# we don't need to check again, just make sure you don't break previous tests
load helpers
function setup {
ORIGDIR="$(pwd)"
TMPDIR="$(mktemp -d)"
# If no $FORGEBUILD is supplied in ENV, use the default
# (otherwise you can't `bats ./tests/` manually)
[ -z "$FORGEBUILD" ] && FORGEBUILD="forgebuild"
# Setup forgebuild tasks
FORGEBUILDDIR="$TMPDIR"/forgebuild
mkdir $FORGEBUILDDIR
echo "date +%s%N > $TMPDIR/forgebuild-time" > $FORGEBUILDDIR/time
chmod +x $FORGEBUILDDIR/time
echo "echo \$(pwd) > $TMPDIR/forgebuild-pwd" > $FORGEBUILDDIR/pwd
chmod +x $FORGEBUILDDIR/pwd
# Disable translations
export LANG="NONE"
# Enable debug for full log
export LOG="debug"
}
function teardown {
cd $ORIGDIR
if [ -d $TMPDIR ]; then rm -rf $TMPDIR; fi
}
# When LOG=info, debug lines are not printed
@test "LOG=info doesn't print debug lines" {
LOG="info" run $FORGEBUILD -b $FORGEBUILDDIR
[ $status = 0 ]
echo "$output"
eq "${lines[0]}" "$(info no_task)"
# Maybe there's some additional output (like config)
# to suppress from this test.
prev_output="$output"
CONTEXT="$output" run find_line "$(info process)"
[ $status = 0 ]
# The line number matching process
i="$output"
run echo "$prev_output"
eq "${lines[i++]}" "$(info process)"
eq "${lines[i++]}" "$(info run)"
eq "${lines[i++]}" "$(info process)"
eq "${lines[i++]}" "$(info run)"
}
# Check that LOG variable is case-insensitive
@test "LOG variable is case-insensitive" {
# Do a first run in case sourceless tasks are supported,
# and therefore marked as done, which would invalidate
# equal outputs (first run would run tasks, unlike the second)
$FORGEBUILD -b $FORGEBUILDDIR
run1="$(LOG="info" $FORGEBUILD -b $FORGEBUILDDIR)"
run2="$(LOG="iNfo" $FORGEBUILD -b $FORGEBUILDDIR)"
run3="$(LOG="INFO" $FORGEBUILD -b $FORGEBUILDDIR)"
eq "$run1" "$run2"
eq "$run1" "$run3"
}
# Unknown LOG level is the same as info loglevel
# TODO: maybe wrong loglevel warning?
@test "Unknown LOG level is treated like LOG=info" {
# Do a first run in case sourceless tasks are supported,
# and therefore marked as done, which would invalidate
# equal outputs (first run would run tasks, unlike the second)
$FORGEBUILD -b $FORGEBUILDDIR
run1="$(LOG="info") $FORGEBUILD -b $FORGEBUILDDIR)"
run2="$(LOG="BOGUS") $FORGEBUILD -b $FORGEBUILDDIR)"
eq "$run1" "$run2"
}
# When LOG=error, only error lines are printed
@test "LOG=error only prints error lines" {
LOG=error run $FORGEBUILD -b $FORGEBUILDDIR
eq "$output" ""
# Let's make sure we still output something when
# there is an actual error
LOG=error run $FORGEBUILD -b /tmp/THISPATHDOESNOTEXIST
neq "" "$output"
}