#!/bin/bash

set -e -u

cp -a setup.py README.md sentry_sdk tests "${AUTOPKGTEST_TMP}"

PYTEST_OPTIONS="-k \
not test_basic and \
not test_403_not_captured and \
not test_half_initialized and \
not test_httplib_misuse and \
not test_crumb_capture and \
not test_errors and \
not test_proxy and \
not test_transport_works and \
not test_transport_infinite_loop and \
not test_simple_rate_limits and \
not test_data_category_limits and \
not test_complex_limits_without_data_category and \
not test_handled_exception and \
not test_unhandled_exception and \
not test_timeout_error and \
not test_performance_no_error and \
not test_performance_error and \
not test_traces_sampler_gets_correct_values_in_sampling_context and \
not test_session_mode_defaults_to_request_mode_in_wsgi_handler and \
not test_socks_proxy and \
not test_omit_url_data_if_parsing_fails and \
not test_omit_url_data_if_parsing_fails and \
not test_error_has_new_trace_context_performance_disabled and \
not test_error_has_new_trace_context_performance_enabled and \
not test_error_has_existing_trace_context_performance_enabled and \
not test_error_has_existing_trace_context_performance_disabled and \
not test_getaddrinfo_trace and \
not test_create_connection_trace \
"

# Tests to ignore if running by pytest 7.
ADDITIONAL_PYTEST_OPTIONS="\
and not test_auto_session_tracking_with_aggregates \
and not test_keyboard_interrupt_is_captured \
and not test_transaction_no_error \
and not test_transaction_with \
and not test_start_span_to_start_transaction \
and not test_tracestate_computation \
and not test_doesnt_add_new_tracestate_to_transaction_when_none_given \
and not test_adds_tracestate_to_transaction_when_to_traceparent_called \
and not test_adds_tracestate_to_transaction_when_getting_trace_context \
and not test_tracestate_is_immutable_once_set \
and not test_to_traceparent \
and not test_to_tracestate \
and not test_sentrytrace_extraction \
and not test_tracestate_extraction \
and not test_iter_headers \
and not test_tracestate_reinflation \
and not test_continue_from_headers \
and not test_memory_usage \
and not test_transactions_do_not_go_through_before_send \
and not test_start_span_after_finish \
and not test_span_trimming \
and not test_transaction_naming \
and not test_start_transaction \
and not test_finds_transaction_on_scope \
and not test_finds_transaction_when_descendent_span_is_on_scope \
and not test_finds_orphan_span_on_scope \
and not test_finds_non_orphan_span_on_scope \
and not test_start_transaction \
and not test_circular_references \
and not test_has_tracestate_enabled \
and not test_default_release \
"

for py3vers in $(py3versions -s); do
    echo
    echo "***************************"
    echo "*** Testing with ${py3vers}"
    echo "***************************"
    echo
    # Removing the folder tests/integrations/flask before calling the test
    # run, the test would fail with werkzeug >= 2.1.
    cd ${AUTOPKGTEST_TMP} && \
        echo "Content of current working folder:\n" && \
        ls -la && \
        echo "Running tests...\n" && \
        PYTHONPATH=. "${py3vers}" -m pytest -sv tests "${PYTEST_OPTIONS} ${ADDITIONAL_PYTEST_OPTIONS}" && \
        rm -rf .pytest_cache .hypothesis || exit 1
done

echo

exit 0
