#!/usr/bin/env bash

pythonexec="/usr/bin/env python" # You can change this variable according to your system
wai=$(dirname $(readlink -f "$0")) # Current script directory
tests=$(ls -d ${wai}/*/) # Find tests
out=$(mktemp)
test_timeout=20
abort=1

for test in ${tests}
do
    printf "%-50s%s %s" "- $(basename $test)" "=>"
    cd $test
    testcmd="esds run platform.yaml"
    timeout $test_timeout ${testcmd} &> "$out"

    # Ensure timeout
    if [ $? -eq 124 ]
    then
        echo "failed :("
        echo "------------- Test timeout (should not exceed ${test_timeout}s) -------------"
        cat "$out";
        rm "$out"
        exit 2
    fi

    # Ensure OSX compatibility 
    case "$OSTYPE" in
	darwin*)  optbase="-i" ;;
	*)        optbase="" ;;
    esac

    # Ensure test output
    
    if [ "$(base64 $optbase $out)" = "$(base64 $optbase ./out)" ]
    then
        echo "passed"
    else
        echo "failed :("
        echo "------------- Expected -------------"
        cat out
        echo "------------- Got -------------"
        cat "$out";
        rm "$out"
        [ $abort -eq 1 ] && exit 1
    fi

    # Prepare for next test
    cd - &>/dev/null
done

rm "$out"