#!/bin/bash

if [ -z $1 ]; then
    logfile=/dev/null
else
    logfile=$1
fi

if [ -z $logall ]; then
    logall=no
fi

run_test () {
    if [ -z "$@" ]; then
        echo "run_test needs a command to run"
    else
        tempLogfile=$(mktemp)
        cmd=$(eval echo "$@")
        echo -e "\\n------------ INPUT -------------------" | tee -a $tempLogfile
        echo "$" "$cmd"  | tee -a $tempLogfile
        echo "------------ OUTPUT ------------------" | tee -a $tempLogfile
        eval $cmd 2>&1 | tee -a $tempLogfile
        if [ $logall = "yes" ]; then
            cat $tempLogfile >> $logfile
            echo -e "\\n\\n" >> $logfile
        else
            read -N 1 -p "Did the test pass? [y/n]: " pass
            if [ $pass = 'n' ]; then
                cat $tempLogfile >> $logfile
                echo -e "\\n\\n" >> $logfile
            fi
        fi
        rm $tempLogfile
    fi
}