#!/bin/bash
set -e

export PRTE_MCA_rmaps_default_mapping_policy=:oversubscribe

DEB_HOST_ARCH=`dpkg-architecture -qDEB_HOST_ARCH`

# list tests to skip in array variable SKIP_TEST_LIST
declare -a SKIP_TEST_LIST

# to run network (ros3) tests separately
SKIP_TEST_LIST=("${SKIP_TEST_LIST[@]}" ros3)

if [ "${DEB_HOST_ARCH}" = "riscv64" ]; then
    # file-locking does not release within 10 sec on riscv64, so skip
    SKIP_TEST_LIST=("${SKIP_TEST_LIST[@]}" "(TestFileLocking and test_multiprocess)")
fi

SKIP_TESTS=""
list_initialised=0
for t in "${SKIP_TEST_LIST[@]}"; do
    if [ ${list_initialised} = 0 ]; then
        SKIP_TESTS=$t
        list_initialised=1
    else
        SKIP_TESTS="${SKIP_TESTS} or $t"
    fi
done
if [ "x${SKIP_TESTS}" != "x" ]; then
    SKIP_TESTS="not ( ${SKIP_TESTS} )"
fi
echo "skipping tests with SKIP_TEST_LIST=${SKIP_TEST_LIST[@]}"

for py in $(py3versions -s 2>/dev/null);
do
    cd "$AUTOPKGTEST_TMP"

    echo -e "\n=== Testing mpi build with $py ==="
    mpirun -n 4 $py -mpytest -o cache_dir=$AUTOPKGTEST_TMP -v --with-mpi -p no:xvfb --color=no -k "${SKIP_TESTS}" /usr/lib/python3/dist-packages/h5py/_debian_h5py_mpi/tests
done
