mirror of
https://github.com/domainaware/parsedmarc.git
synced 2026-10-21 10:18:14 +00:00
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 <misapor@lab.misapor.pl> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 file changed
+18
-3
+18
-3
@@ -732,7 +732,21 @@ class TestUtilsIpDbPaths(unittest.TestCase):
|
|||||||
|
|
||||||
def testMissingEverythingRaisesFileNotFoundError(self):
|
def testMissingEverythingRaisesFileNotFoundError(self):
|
||||||
"""When neither the bundled database nor any system path exists,
|
"""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()
|
tmp_dir = tempfile.mkdtemp()
|
||||||
old_cwd = os.getcwd()
|
old_cwd = os.getcwd()
|
||||||
self.addCleanup(lambda: (os.chdir(old_cwd), shutil.rmtree(tmp_dir)))
|
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")
|
missing = os.path.join(tmp_dir, "does-not-exist.mmdb")
|
||||||
with patch("parsedmarc.utils.files") as mock_files:
|
with patch("parsedmarc.utils.files") as mock_files:
|
||||||
mock_files.return_value.joinpath.return_value = missing
|
mock_files.return_value.joinpath.return_value = missing
|
||||||
with self.assertRaises(FileNotFoundError) as ctx:
|
with patch("parsedmarc.utils.os.path.exists", return_value=False):
|
||||||
parsedmarc.utils.get_ip_address_db_record("8.8.8.8")
|
with self.assertRaises(FileNotFoundError) as ctx:
|
||||||
|
parsedmarc.utils.get_ip_address_db_record("8.8.8.8")
|
||||||
self.assertIn(missing, str(ctx.exception))
|
self.assertIn(missing, str(ctx.exception))
|
||||||
|
|
||||||
def testOldDatabaseFileWarns(self):
|
def testOldDatabaseFileWarns(self):
|
||||||
|
|||||||
Reference in new issue
Block a user