#!/bin/bash
set -e

DEB_HOST_ARCH=$(dpkg-architecture -qDEB_HOST_ARCH)

# Drop a global import of trimesh, which is not packaged on debian
sed -i 's|import trimesh|# import trimesh|g' tests/core/test_helpers.py

# Do not require an installation of pytest-pyvista, since we do not have a cache
# to test against anyways
cat <<EOF >> tests/plotting/conftest.py
@pytest.fixture()
def verify_image_cache(request, pytestconfig):
    return lambda plotter: True
EOF

# Disable some tests which are failing
declare -a NETWORK_TESTS
NETWORK_TESTS=(
 test_download
 test_dataset_loader
 test_examples
)

if [ "${DEB_HOST_ARCH}" != "i386" ] ; then
    # HTTP requests to download example data files are refused from i386,
    # but ok for other arches
    NETWORK_TESTS=("${NETWORK_TESTS[@]}"
	test_download_files
	test_meshio
	test_protein_ribbon  # core/test_polydata_filters.py
    )
fi

TESTS_SEPARATOR=" or "
NETWORK_TESTS_STRING=$(printf "${TESTS_SEPARATOR}%s" "${NETWORK_TESTS[@]}")
NETWORK_TESTS_STRING=${NETWORK_TESTS_STRING:${#TESTS_SEPARATOR}}

# Disable some tests which are failing
declare -a DISABLED_TESTS
DISABLED_TESTS=(
    ### TESTS/EXAMPLES ###
    # tests/examples/test_download_files.py::test_download_meshio_xdmf
    # requires vtkmodules.vtkIOXdmf2, which seems not be available with debian's vtk
    "test_download_meshio_xdmf"
)

DISABLED_TESTS_STRING=$(printf "${TESTS_SEPARATOR}%s" "${DISABLED_TESTS[@]}")
DISABLED_TESTS_STRING=${DISABLED_TESTS_STRING:${#TESTS_SEPARATOR}}

# Print report about pyvista installation
python3 -c "import pyvista; print(pyvista.Report())"

# Run the network tests through xvfb-run, since plotting tests require a
# virtual frame buffer
xvfb-run -a python3 -P -m pytest \
    -k "${NETWORK_TESTS_STRING} and not (${DISABLED_TESTS_STRING})" \
    --test_downloads `# required by the examples directory` \
    -vv --random-order \
    tests
