#!/bin/bash
cd "${0%/*}" || exit 1    # Run from this directory

case=$(basename "$PWD")
logfile="log.${case}"

# Write error string to log file
# (Ensures that error is reported in case subsequent steps behave unexpectedly.)
echo "FOAM_FATAL_ERROR" > "$logfile"

# Run typical testing command which is expected to fail (but do not allow the
# tool to exit the script)
mpytest --suffix "${case}" almostequal \
    validation/reference/a.dat validation/reference/b.dat || true
logfile_testingtool="log.almostequal.${case}"

# Check if testing tool log file contains error string (expected behavior)
testPassed=false
if grep -q "FOAM_FATAL_ERROR" "$logfile_testingtool"
then
    testPassed=true
fi
rm -f "$logfile_testingtool" # Remove testing tool log file

# Overwrite error string with success string in case behavior is as expected
if "$testPassed"
then
    echo "End" > "$logfile"
fi

#------------------------------------------------------------------------------
