File: test_sys_exit.py

package info (click to toggle)
sentry-python 2.19.2-3
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 4,088 kB
  • sloc: python: 56,942; sh: 117; makefile: 114; xml: 2
file content (71 lines) | stat: -rw-r--r-- 2,263 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import sys

import pytest

from sentry_sdk.integrations.sys_exit import SysExitIntegration


@pytest.mark.parametrize(
    ("integration_params", "exit_status", "should_capture"),
    (
        ({}, 0, False),
        ({}, 1, True),
        ({}, None, False),
        ({}, "unsuccessful exit", True),
        ({"capture_successful_exits": False}, 0, False),
        ({"capture_successful_exits": False}, 1, True),
        ({"capture_successful_exits": False}, None, False),
        ({"capture_successful_exits": False}, "unsuccessful exit", True),
        ({"capture_successful_exits": True}, 0, True),
        ({"capture_successful_exits": True}, 1, True),
        ({"capture_successful_exits": True}, None, True),
        ({"capture_successful_exits": True}, "unsuccessful exit", True),
    ),
)
def test_sys_exit(
    sentry_init, capture_events, integration_params, exit_status, should_capture
):
    sentry_init(integrations=[SysExitIntegration(**integration_params)])

    events = capture_events()

    # Manually catch the sys.exit rather than using pytest.raises because IDE does not recognize that pytest.raises
    # will catch SystemExit.
    try:
        sys.exit(exit_status)
    except SystemExit:
        ...
    else:
        pytest.fail("Patched sys.exit did not raise SystemExit")

    if should_capture:
        (event,) = events
        (exception_value,) = event["exception"]["values"]

        assert exception_value["type"] == "SystemExit"
        assert exception_value["value"] == (
            str(exit_status) if exit_status is not None else ""
        )
    else:
        assert len(events) == 0


def test_sys_exit_integration_not_auto_enabled(sentry_init, capture_events):
    sentry_init()  # No SysExitIntegration

    events = capture_events()

    # Manually catch the sys.exit rather than using pytest.raises because IDE does not recognize that pytest.raises
    # will catch SystemExit.
    try:
        sys.exit(1)
    except SystemExit:
        ...
    else:
        pytest.fail(
            "sys.exit should not be patched, but it must have been because it did not raise SystemExit"
        )

    assert (
        len(events) == 0
    ), "No events should have been captured because sys.exit should not have been patched"