# MPI tests are set up to run on no more than 3 processes.
NPROC=$( nproc )
if [[ $NPROC > 2 ]]; then
  N_MPI=3
else
  N_MPI=2
fi
# and only 2 processes for unit tests
N_MPI_UNITTEST=2

export OMPI_MCA_plm_rsh_agent=/bin/false
export OMPI_MCA_rmaps_base_oversubscribe=1
export OMPI_MCA_btl_base_warn_component_unused=0

echo "== running python tests for complex build =="

mkdir -p ${AUTOPKGTEST_TMP}/python/dist-packages
cp -ra python/dolfinx_utils ${AUTOPKGTEST_TMP}/python/dist-packages
export PYTHONPATH=${PYTHONPATH}:${AUTOPKGTEST_TMP}/python/dist-packages

PETSC_DIR_COMPLEX=/usr/lib/petscdir/petsc-complex
SLEPC_DIR_COMPLEX=/usr/lib/slepcdir/slepc-complex

# help CI tests run a little faster
export DOLFINX_JIT_CFLAGS="-g0 -O0"

# numba is not ready for python3.9
NUMBA_TESTS="custom_jit_kernels custom_assembler expression_evaluation"
EXCLUDE_NUMBA=
INCLUDE_NUMBA=
for nt in $NUMBA_TESTS; do
  EXCLUDE_NUMBA="$EXCLUDE_NUMBA --ignore-glob=*${nt}*"
  INCLUDE_NUMBA="$INCLUDE_NUMBA or ${nt}"
done
INCLUDE_NUMBA=$(echo $INCLUDE_NUMBA | sed "s/or //" )

TESTS_SKIPPED="test_RT_N1curl_simplex"
MULTIPLE_TESTS_SKIPPED="test_assembly_solve_taylor_hood test_cube_distance test_read_write_p2_mesh gmsh"

for test in $MULTIPLE_TESTS_SKIPPED; do
  TESTS_SKIPPED="$TESTS_SKIPPED or $test"
done
TEST_KEYWORD="not (${TESTS_SKIPPED})"

echo "=== python unit test (serial complex build) ==="
PETSC_DIR=${PETSC_DIR_COMPLEX} SLEPC_DIR=${SLEPC_DIR_COMPLEX} python3 -m pytest --durations=20 $EXCLUDE_NUMBA -k "${TEST_KEYWORD}" python/test/unit/

echo "=== python unit test (MPI complex build using ${N_MPI_UNITTEST} processors out of $NPROC) ==="
PETSC_DIR=${PETSC_DIR_COMPLEX} SLEPC_DIR=${SLEPC_DIR_COMPLEX} mpirun -n ${N_MPI_UNITTEST} python3 -m pytest --durations=20 $EXCLUDE_NUMBA -k "${TEST_KEYWORD}" python/test/unit/ --color=no
