#!/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
)

DISABLED_TESTS=(
 test_download_beach
 test_download_dataset_texture
 test_dataset_loader_source_url_blob
 test_download_headsq
 test_download_can_crushed_hdf
 test_download_biplane
 test_hdf_reader
 test_xdmf_reader
 test_nrrd_reader
)

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
	test_reader
	test_composite
    )
else
    NETWORK_TESTS=("${NETWORK_TESTS[@]}"
	test_user_logo
    test_actor_texture
    test_import_3ds
    test_remove_environment_texture_cubemap
    test_import_obj_with_texture
    test_import_obj
    test_voxelize_volume
    test_property_pbr
    test_get_background_texture
    test_brush
    test_logo_widget
    )
    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]

	# segfaults
	test_exodus_blocks
    )
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
