#!/bin/bash
set -e

DEB_HOST_ARCH=$(dpkg-architecture -qDEB_HOST_ARCH)

# 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 which tests to run
declare -a NETWORK_TESTS

# Disable which tests are failing
declare -a DISABLED_TESTS

NETWORK_TESTS=(
 test_download
 test_dataset_loader
 test_examples
 test_reader
 test_composite
)

DISABLED_TESTS=(
 test_download_beach
 test_download_dataset_texture
 test_dataset_loader_source_url_blob
 test_download_headsq
 test_download_can_crushed_hdf
)

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_meshio
	test_protein_ribbon  # core/test_polydata_filters.py
    )
else
    DISABLED_TESTS=("${DISABLED_TESTS[@]}"
        test_download_files
	    test_dataset_loader
    )
fi

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

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"
)

if [ "${DEB_HOST_ARCH}" = "s390x" ] ; then
    DISABLED_TESTS=("${DISABLED_TESTS[@]}"
        # all gltf tests segfault
        test_download_gltf

	# bad data reads, is it a bigendian issue?
	test_dataset_loader_from_nested_multiblock
	test_meshio[mesh_in0]
	test_meshio[mesh_in2]
    )
fi

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
