Skip to content

Commit 3e86efa

Browse files
hikmetba-bitclaude
andcommitted
tests: add real coverage for _generate_installed_modules
Fixes #4594. The previous test for this function (removed in #4593) just mirrored the implementation's own loop, so it never exercised any behavior independent of the code it was testing. This mocks importlib.metadata.distributions() with a small fixed set of fake distributions and asserts on the actual contract: names are normalized case-insensitively, entries with no Name are skipped, entries with no version are skipped, and a case-insensitive duplicate name is skipped in favor of the first occurrence. Verified the test actually catches a regression by temporarily breaking the dedup check locally and confirming the test fails, then reverting. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 3189a58 commit 3e86efa

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

‎tests/test_utils.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from sentry_sdk.utils import (
1313
Components,
1414
Dsn,
15+
_generate_installed_modules,
1516
_get_installed_modules,
1617
datetime_from_isoformat,
1718
ensure_integration_enabled,
@@ -964,6 +965,31 @@ def test_installed_modules_caching():
964965
mock_generate_installed_modules.assert_not_called()
965966

966967

968+
def test_generate_installed_modules():
969+
def make_dist(name, version):
970+
dist = mock.Mock()
971+
dist.metadata = {"Name": name} if name is not None else {}
972+
dist.version = version
973+
return dist
974+
975+
fake_distributions = [
976+
make_dist("Foo-Package", "1.0.0"),
977+
make_dist(None, "9.9.9"), # no Name in metadata -- must be skipped
978+
# normalizes to the same name as the first entry (case-insensitive) --
979+
# must be skipped in favor of the first occurrence
980+
make_dist("FOO-PACKAGE", "2.0.0"),
981+
make_dist("Baz", None), # no version -- must be skipped
982+
make_dist("Bar", "3.0.0"),
983+
]
984+
985+
with mock.patch(
986+
"importlib.metadata.distributions", return_value=fake_distributions
987+
):
988+
result = dict(_generate_installed_modules())
989+
990+
assert result == {"foo-package": "1.0.0", "bar": "3.0.0"}
991+
992+
967993
def test_devnull_inaccessible():
968994
with mock.patch("sentry_sdk.utils.open", side_effect=OSError("oh no")):
969995
revision = get_git_revision()

0 commit comments

Comments
 (0)