#!/bin/bash
set -efu

arch=$(dpkg-architecture -qDEB_HOST_ARCH)

EXTRA_TEST_FLAGS=-v

# some tests are expected to fail
# so list tests to skip in array variable SKIP_TEST_LIST
declare -a SKIP_TEST_LIST

# parallel and openmp tests are unstable, often timing out
SKIP_TEST_LIST=("${SKIP_TEST_LIST[@]}" parallel multiprocess openmp)

# gsd tends to induce test time-outs, https://github.com/MDAnalysis/mdanalysis/issues/4209
SKIP_TEST_LIST=("${SKIP_TEST_LIST[@]}" gsd GSD)

# NSGrid is flaky and causes distance tests to often (randomly) fail
# https://github.com/MDAnalysis/mdanalysis/issues/4906
SKIP_TEST_LIST=("${SKIP_TEST_LIST[@]}" test_distances)

# mdahole2, pathsimanalysis are not yet packaged for debian
# (duecredit test journal.pcbi.1004568 accesses pathsimanalysis)
SKIP_TEST_LIST=("${SKIP_TEST_LIST[@]}" test_all_import[.analysis.hole2] journal.pcbi.1004568)

if [ "$arch" = "i386" ]; then
    SKIP_TEST_LIST=("${SKIP_TEST_LIST[@]}" test_multiprocess_COG[u4] test_ramachandran \
	test_clustering_three_ensembles_two_identical test_hbond_analysis)
fi

# many tests fail on big-endian systems
# see https://github.com/MDAnalysis/mdanalysis/issues/1829
if [ "$arch" = "s390x" ]; then
    SKIP_TEST_LIST=("${SKIP_TEST_LIST[@]}" test_start_stop_step test_count_by_time \
        test_rmsd test_mass_weighted test_custom_weighted test_ref_mobile_mass_mismapped \
	test_time test_dt test_total_time test_iter test_last_frame test_frame_jump \
	test_next_gives_second_frame test_set_time test_write_istart \
	test_write_trajectory_atomgroup test_write_trajectory_universe test_Timestep_time \
	test_rename_aux test_namdbin ref_reader25 ref_reader_extra_args25)
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`; do
    MPLBACKEND=agg $py -mpytest ${EXTRA_TEST_FLAGS} -k "${SKIP_TESTS}" --disable-pytest-warnings
done
