forked from getsentry/sentry-python
/
test_modules.py
71 lines (55 loc) · 1.91 KB
/
test_modules.py
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 pytest
import re
import sentry_sdk
from sentry_sdk.integrations.modules import (
ModulesIntegration,
_get_installed_modules,
)
def _normalize_distribution_name(name):
# type: (str) -> str
"""Normalize distribution name according to PEP-0503.
See:
https://peps.python.org/pep-0503/#normalized-names
for more details.
"""
return re.sub(r"[-_.]+", "-", name).lower()
def test_basic(sentry_init, capture_events):
sentry_init(integrations=[ModulesIntegration()])
events = capture_events()
sentry_sdk.capture_exception(ValueError())
(event,) = events
assert "sentry-sdk" in event["modules"]
assert "pytest" in event["modules"]
def test_installed_modules():
try:
from importlib.metadata import distributions, version
importlib_available = True
except ImportError:
importlib_available = False
try:
import pkg_resources
pkg_resources_available = True
except ImportError:
pkg_resources_available = False
installed_distributions = {
_normalize_distribution_name(dist): version
for dist, version in _get_installed_modules().items()
}
if importlib_available:
importlib_distributions = {
_normalize_distribution_name(dist.metadata["Name"]): version(
dist.metadata["Name"]
)
for dist in distributions()
if dist.metadata["Name"] is not None
and version(dist.metadata["Name"]) is not None
}
assert installed_distributions == importlib_distributions
elif pkg_resources_available:
pkg_resources_distributions = {
_normalize_distribution_name(dist.key): dist.version
for dist in pkg_resources.working_set
}
assert installed_distributions == pkg_resources_distributions
else:
pytest.fail("Neither importlib nor pkg_resources is available")