#!/bin/sh

set -e -u

cp -va tests "$AUTOPKGTEST_TMP"/
mkdir -p ../../docs/tutorials/tests
cp -va docs/tutorials/tests/* ../../docs/tutorials/tests
cd "$AUTOPKGTEST_TMP"

# Define the test arguments based on architecture
FILES_ARGS="--ignore=tests/integration/test_main.py \
--ignore=tests/integration/test_native_tracking.py"

TIMEOUT_ARGS="and not test_reading_allocations_while_reading_stack_traces \
and not test_live_tracking_waits_for_client \
and not test_live_tracking \
and not test_live_client_exits_properly_on_sigint_before_connecting \
and not test_multithreaded_extension \
and not test_misbehaving_extension \
and not test_extension_that_uses_pygilstate_ensure \
and not test_native_dlopen \
and not test_valloc_at_thread_exit \
and not test_valloc_at_thread_exit_in_subprocess \
and not test_dlopen_with_rpath"

ARM64_ARGS="${FILES_ARGS} \
-v -k 'not test_aggregation_same_python_stack_but_different_native_stack[ALL_ALLOCATIONS] \
and not test_aggregation_same_python_stack_but_different_native_stack[AGGREGATED_ALLOCATIONS] \
and not test_allocations_with_multiprocessing \
and not test_pymalloc_allocations_after_fork \
and not test_stack_cleanup_after_fork \
${TIMEOUT_ARGS}'"

ARMHF_ARGS="${FILES_ARGS} \
--ignore=tests/integration/test_tracing.py \
--ignore=tests/integration/test_tracking.py \
-v -k 'not test_thread_name \
and not test_setting_python_thread_name \
and not test_successful_parse_of_aggregated_capture_file \
${TIMEOUT_ARGS}'"

ARMEL_ARGS="${FILES_ARGS} \
--ignore=tests/integration/test_tracing.py \
-v -k 'not test_thread_name \
and not test_setting_python_thread_name \
and not test_aggregation_same_python_stack_and_same_native_stack[ALL_ALLOCATIONS] \
and not test_aggregation_same_python_stack_and_same_native_stack[AGGREGATED_ALLOCATIONS] \
and not test_aggregation_same_python_stack_but_different_native_stack[ALL_ALLOCATIONS] \
and not test_aggregation_same_python_stack_but_different_native_stack[AGGREGATED_ALLOCATIONS] \
${TIMEOUT_ARGS}'"

S390X_ARGS="${FILES_ARGS} \
-v -k 'not test_aggregation_same_python_stack_and_same_native_stack[ALL_ALLOCATIONS] \
and not test_aggregation_same_python_stack_and_same_native_stack[AGGREGATED_ALLOCATIONS] \
and not test_large_number_of_frame_pops_between_subsequent_allocations \
and not test_aggregation_same_python_stack_but_different_native_stack[ALL_ALLOCATIONS] \
and not test_aggregation_same_python_stack_but_different_native_stack[AGGREGATED_ALLOCATIONS] \
and not test_inverted_very_deep_call_is_limited \
and not test_rejects_different_header_version \
and not test_allocations_with_multiprocessing \
${TIMEOUT_ARGS}'"

AMD64_OTHERS_ARGS="${FILES_ARGS} \
-v -k 'not test_reading_allocations_while_reading_stack_traces \
and not test_live_tracking_waits_for_client \
and not test_live_tracking \
and not test_munmap_multiple_mmaps_multiple_munmaps[ALL_ALLOCATIONS] \
and not test_munmap_multiple_mmaps_multiple_munmaps[AGGREGATED_ALLOCATIONS] \
and not test_basic_attach[gdb] \
and not test_aggregated_attach[gdb] \
and not test_attach_time[gdb] \
and not test_detach_without_attach[gdb] \
${TIMEOUT_ARGS}'"

ARCHITECTURE=$(dpkg --print-architecture)

# Determine the architecture and set the appropriate arguments
case "$ARCHITECTURE" in
    arm64)
        TEST_ARGS=$ARM64_ARGS
        ;;
    armhf)
        TEST_ARGS=$ARMHF_ARGS
        ;;
    armel)
        TEST_ARGS=$ARMEL_ARGS
        ;;
    s390x)
        TEST_ARGS=$S390X_ARGS
        ;;
    amd64|i386|ppc64el|riscv64)
        TEST_ARGS=$AMD64_OTHERS_ARGS
        ;;
    mips64el|ppc64|loong64|sh4)
        echo "Skip tests in $ARCHITECTURE"
        exit 0
        ;;
    *)
        echo "Skip tests in $ARCHITECTURE"
        exit 0
        ;;
esac

for py in $(py3versions --supported); do
    eval "$py -m pytest $TEST_ARGS"
done
