From 79c0f8e53d062ad0e185df4fce6ae735e4f01ce4 Mon Sep 17 00:00:00 2001 From: southerntofu Date: Fri, 27 Nov 2020 18:52:13 +0100 Subject: [PATCH] Start tests to ensure consistent output across implementations --- tests/output.bats | 116 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100755 tests/output.bats diff --git a/tests/output.bats b/tests/output.bats new file mode 100755 index 0000000..6f43a05 --- /dev/null +++ b/tests/output.bats @@ -0,0 +1,116 @@ +#! /usr/bin/env bats + +# This is a test file for use with the bats testing framework +# https://github.com/bats-core/bats-core +# On Debian: apt install bats + +# output.bats tests that output is uniform across forgebuild implementations, +# by using the special value "NONE" as LANG environment variable when calling +# forgebuild. + +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 + + # Setup settings + mkdir $FORGEBUILDDIR/config + echo "default" > $FORGEBUILDDIR/config/entry + mkdir $FORGEBUILDDIR/$HOSTNAME + echo "host" > $FORGEBUILDDIR/$HOSTNAME/entry + + # Disable translations + export LANG="NONE" +} + +function teardown { + cd $ORIGDIR + clean +} + +function clean { + if [ -d $TMPDIR ]; then rm -rf $TMPDIR; fi +} + +# Emulate translation, without actual escape codes (colors), +# variable substitution, or actual translations +warn() { + echo "warning${1}" +} + +info() { + if [[ "$LOG" = "info" ]] || [[ "$LOG" = "debug" ]] || [[ "$LOG" = "" ]]; then + echo "[git-build] ${1}" + fi +} + +debug() { + [[ "$LOG" = "debug" ]] && echo "debug${1}" +} + +error() { + echo "error${1}" +} + +# Simple repository + +@test "missing basedir" { + run $FORGEBUILD -b /tmp/THISPATHDOESNOTEXIST + [ "$output" = "$(error missing_basedir)" ] +} + +@test "unknown task" { + run $FORGEBUILD -b $FORGEBUILDDIR THISTASKDOESNOTEXIST + [ "$output" = "$(error unknown_arg)" ] +} + +@test "unknown argument" { + run $FORGEBUILD -WTF -b $FORGEBUILDDIR + [ "$output" = "$(error unknown_arg)" ] +} + +@test "clone failed" { + echo "/tmp/THISPATHDOESNOTEXIST" > $FORGEBUILDDIR/time.source + run $FORGEBUILD -b $FORGEBUILDDIR time + [ "${lines[0]}" = "$(info config)" ] + [ "${lines[1]}" = "$(info process)" ] + [ "${lines[2]}" = "$(info clone)" ] + [ "${lines[3]}" = "$(error clone_failed)" ] +} + +@test "simple sourceless run" { + run $FORGEBUILD -b $FORGEBUILDDIR time + [ "${lines[0]}" = "$(info config)" ] + [ "${lines[1]}" = "$(info process)" ] + [ "${lines[2]}" = "$(info run)" ] +} + +@test "no tasks requested" { + run $FORGEBUILD -b $FORGEBUILDDIR + [ "${lines[0]}" = "$(info no_task)" ] + [ "${lines[1]}" = "$(info config)" ] + [ "${lines[2]}" = "$(info process)" ] + [ "${lines[3]}" = "$(info run)" ] + [ "${lines[4]}" = "$(info process)" ] + [ "${lines[5]}" = "$(info run)" ] +} + +@test "no tasks exist" { + rm -r $FORGEBUILDDIR/* + run $FORGEBUILD -b $FORGEBUILDDIR + echo $output + [ "${lines[0]}" = "$(info no_task)" ] + [ "${lines[1]}" = "$(info config)" ] + # TODO: maybe introduce a "nothing to do" message here? +}