#!/bin/bash
set -efu

arch=$(dpkg-architecture -qDEB_HOST_ARCH)

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

# this subset of demo tests (test_tutorials.py::test_geoFiles)
# only passes on amd64 and armhf
if [[ "$arch" != "amd64" && "$arch" != "armhf" ]]; then
    SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} boxcyl.geo-mp11-5 cylsphere.geo-mp61-1 cylsphere.geo-mp64-4 \
        hinge.stl-mp99-5 part1.stl-mp129-1 part1.stl-mp132-4 \
        sculpture.geo-mp149-4 sphereincube.geo-mp168-5 \
        twobricks.geo-mp199-0 twobricks.geo-mp201-2 twobricks.geo-mp202-3 \
        twocubes.geo-mp205-0 twocubes.geo-mp207-2 twocubes.geo-mp208-3)
fi

if [ "$arch" = "i386" ]; then
    SKIP_TEST_LIST=(${SKIP_TEST_LIST[@]} boxcyl.geo-mp10-4 hinge.stl-mp95-1 lense square)
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[@]}"


cd tests/pytest

echo "Run serial python test"
pytest-3 -k "${SKIP_TESTS}"

if [[ "$arch" == "i386" || "$arch" == "armhf" ]]; then
  # the mpi test is flakey and often segfaults on i386, armhf
  echo -e "\n\nSkipping MPI python test on $arch"
else
  echo -e "\n\nRun MPI python test"
  OMPI_MCA_rmaps_base_oversubscribe=yes mpirun -n 4 python3 -m pytest --with-mpi test_mpi4py.py
fi
