From 3e86efa92a326a1be9d35e336aba3304afc8d699 Mon Sep 17 00:00:00 2001 From: hikmetba-bit Date: Wed, 16 Sep 2026 21:00:52 +0300 Subject: [PATCH] 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 --- tests/test_utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_utils.py b/tests/test_utils.py index 1b6d2892b9..b7b78acfc0 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -12,6 +12,7 @@ from sentry_sdk.utils import ( Components, Dsn, + _generate_installed_modules, _get_installed_modules, datetime_from_isoformat, ensure_integration_enabled, @@ -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 + ): + 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()