Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from sentry_sdk.utils import (
Components,
Dsn,
_generate_installed_modules,
_get_installed_modules,
datetime_from_isoformat,
ensure_integration_enabled,
Expand Down Expand Up @@ -964,6 +965,31 @@ def test_installed_modules_caching():
mock_generate_installed_modules.assert_not_called()


def test_generate_installed_modules():
def make_dist(name, version):
dist = mock.Mock()
dist.metadata = {"Name": name} if name is not None else {}
dist.version = version
return dist

fake_distributions = [
make_dist("Foo-Package", "1.0.0"),
make_dist(None, "9.9.9"), # no Name in metadata -- must be skipped
# normalizes to the same name as the first entry (case-insensitive) --
# must be skipped in favor of the first occurrence
make_dist("FOO-PACKAGE", "2.0.0"),
make_dist("Baz", None), # no version -- must be skipped
make_dist("Bar", "3.0.0"),
]

with mock.patch(
"importlib.metadata.distributions", return_value=fake_distributions
):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test fails on older Python versions

Medium Severity

The new test patches importlib.metadata.distributions, which does not exist on Python 3.6 and 3.7. Common CI still runs tests/test_utils.py on those versions, so the test errors when applying the patch instead of covering _generate_installed_modules.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 3e86efa. Configure here.

result = dict(_generate_installed_modules())

assert result == {"foo-package": "1.0.0", "bar": "3.0.0"}


def test_devnull_inaccessible():
with mock.patch("sentry_sdk.utils.open", side_effect=OSError("oh no")):
revision = get_git_revision()
Expand Down