From f33951c0bb1cb428d89e8b2bbd875bb3e2dc575e Mon Sep 17 00:00:00 2001 From: Kili Date: Mon, 13 Jul 2026 15:32:32 +0200 Subject: [PATCH] Fix host-dependent flakiness in testMissingEverythingRaisesFileNotFoundError (#820) The system-path fallback list in _get_ip_database_path() checks real absolute paths like /usr/share/GeoIP/GeoLite2-Country.mmdb. On a host that actually has one installed there, the fallback succeeds and no FileNotFoundError is raised, regardless of the test's mocked bundled path -- the code is behaving correctly, the test just wasn't isolated from the host filesystem. Patch os.path.exists to force every system path to look absent so the test is host-independent. Co-authored-by: MISAPOR LAB Co-authored-by: Claude Sonnet 5 --- tests/test_utils.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index dfcbcae..3d76968 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -732,7 +732,21 @@ class TestUtilsIpDbPaths(unittest.TestCase): def testMissingEverythingRaisesFileNotFoundError(self): """When neither the bundled database nor any system path exists, - the error names the expected bundled install location.""" + the error names the expected bundled install location. + + The system-path fallback list in ``_get_ip_database_path()`` + includes real absolute paths like + ``/usr/share/GeoIP/GeoLite2-Country.mmdb``. On a host that + actually has a GeoIP package installed there (common on + malware-analysis / network-tooling workstations, and the same + file behind https://github.com/domainaware/parsedmarc/issues/810), + that fallback silently succeeds and no FileNotFoundError is + raised, regardless of the mocked bundled path or the temp cwd + below. ``os.path.exists`` is patched to force every system path + to look absent, so this test asserts the "nothing found + anywhere" behavior regardless of what's actually installed on + the machine running the suite. + """ tmp_dir = tempfile.mkdtemp() old_cwd = os.getcwd() self.addCleanup(lambda: (os.chdir(old_cwd), shutil.rmtree(tmp_dir))) @@ -741,8 +755,9 @@ class TestUtilsIpDbPaths(unittest.TestCase): missing = os.path.join(tmp_dir, "does-not-exist.mmdb") with patch("parsedmarc.utils.files") as mock_files: mock_files.return_value.joinpath.return_value = missing - with self.assertRaises(FileNotFoundError) as ctx: - parsedmarc.utils.get_ip_address_db_record("8.8.8.8") + with patch("parsedmarc.utils.os.path.exists", return_value=False): + with self.assertRaises(FileNotFoundError) as ctx: + parsedmarc.utils.get_ip_address_db_record("8.8.8.8") self.assertIn(missing, str(ctx.exception)) def testOldDatabaseFileWarns(self):