#! /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 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 simple git repository GITREPO="$TMPDIR"/git mkdir $GITREPO cd $GITREPO git init echo "first commit" > README git add README git commit -m "first commit" # Setup forgebuild tasks FORGEBUILDDIR="$TMPDIR"/forgebuild mkdir $FORGEBUILDDIR echo "$GITREPO" > $FORGEBUILDDIR/time.source echo "date +%s%N > $TMPDIR/forgebuild-time" > $FORGEBUILDDIR/time chmod +x $FORGEBUILDDIR/time echo "$GITREPO" > $FORGEBUILDDIR/host.source echo "cat \$FORGEBUILDCONF/entry > $TMPDIR/forgebuild-host" > $FORGEBUILDDIR/host chmod +x $FORGEBUILDDIR/host echo "echo \$(pwd) > $TMPDIR/forgebuild-pwd" > $FORGEBUILDDIR/pwd chmod +x $FORGEBUILDDIR/pwd echo "$GITREPO" > $FORGEBUILDDIR/pwd.source # Setup settings mkdir $FORGEBUILDDIR/config echo "default" > $FORGEBUILDDIR/config/entry mkdir $FORGEBUILDDIR/$HOSTNAME echo "host" > $FORGEBUILDDIR/$HOSTNAME/entry } function teardown { cd $ORIGDIR if [ -d $TMPDIR ]; then rm -rf $TMPDIR; fi } # Task settings @test "Per-host configuration is loaded as \$FORGEBUILDCONF" { $FORGEBUILD -b $FORGEBUILDDIR host [[ "$(cat $TMPDIR/forgebuild-host)" = "host" ]] } @test "Unknown host uses \$FORGEBUILDDIR/config as \$FORGEBUILDCONF" { HOST="UNKNOWN" $FORGEBUILD -b $FORGEBUILDDIR host [[ "$(cat $TMPDIR/forgebuild-host)" = "default" ]] } @test "task only runs in allowlisted hosts" { # When t.hosts exist, only this (line-separated) list of hosts # runs the task echo "$HOSTNAME" > $FORGEBUILDDIR/time.hosts $FORGEBUILD -b $FORGEBUILDDIR time TIME="$(cat $TMPDIR/forgebuild-time)" # Doesn't run on unknown host HOST="UNKNOWN" $FORGEBUILD -b $FORGEBUILDDIR time NEWTIME="$(cat $TMPDIR/forgebuild-time)" [[ "$TIME" = "$NEWTIME" ]] echo "TESTHOST" >> $FORGEBUILDDIR/time.hosts # Force trigger because repo was not updated HOST="TESTHOST" $FORGEBUILD -f -b $FORGEBUILDDIR time NEWTIME="$(cat $TMPDIR/forgebuild-time)" [[ "$TIME" != "$NEWTIME" ]] } @test "task doesn't run in ignored host" { touch $FORGEBUILDDIR/$HOSTNAME/time.ignore $FORGEBUILD -b $FORGEBUILDDIR time [ ! -f $TMPDIR/forgebuild-time ] HOST="TESTHOST" $FORGEBUILD -f -b $FORGEBUILDDIR time [ -f $TMPDIR/forgebuild-time ] }