Skip to content

Commit de626f0

Browse files
gpsheadclaude
andcommitted
gh-157144: Reject missing archive paths and unimportable names in discovery
Address review of unittest discovery inside archives: * A path hook such as zipimport claims every path below its archive, so a mistyped start directory there gave an empty suite. discover() now raises ImportError unless a top level directory in an archive is the archive itself or lists a module, and regrtest's findtests() lets os.listdir() raise when the archive lists nothing. * Leave out names which the archive's finder lists but cannot import, such as extension modules in a zip file, instead of reporting them as failed imports. * Pass *kind* to _find_test_path() for archive entries only, so that subclasses overriding it with the previous signature keep working for file system discovery. * Use pkgutil.get_importer() in _is_archive_dir(); it does not cache a negative result, contrary to what the docstring said. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent d3f5935 commit de626f0

4 files changed

Lines changed: 70 additions & 22 deletions

File tree

‎Lib/test/libregrtest/findtests.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ def _listdir(testdir: StrPath) -> list[str]:
4242
# The test package may be inside an archive on sys.path, such as a
4343
# zip file, which the import system can still list.
4444
import pkgutil
45-
if pkgutil.get_importer(testdir) is not None:
46-
return [name if ispkg else f"{name}.py"
47-
for _, name, ispkg in pkgutil.iter_modules([testdir])]
45+
names = [name if ispkg else f"{name}.py"
46+
for _, name, ispkg in pkgutil.iter_modules([testdir])]
47+
if names:
48+
return names
49+
# Not in an archive, or the archive lists nothing at that path, in
50+
# which case os.listdir() raises the error for a missing directory.
4851
return os.listdir(testdir)
4952

5053

‎Lib/test/test_regrtest.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,6 +2662,9 @@ def test_findtests_not_on_file_system(self):
26622662
tests = findtests(testdir=testdir, split_test_dirs={'test_pkg'})
26632663
self.assertEqual(tests, ['test.test_pkg.test_c', 'test_a', 'test_b'])
26642664

2665+
with self.assertRaises(OSError):
2666+
findtests(testdir=os.path.join(zip_path, 'nonexistent'))
2667+
26652668

26662669
class MultiprocessIteratorTestCase(unittest.TestCase):
26672670
def test_yields_all_groups_once(self):

‎Lib/test/test_unittest/test_discovery.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib.machinery
12
import os.path
23
from os.path import abspath
34
import re
@@ -922,6 +923,28 @@ def _import(packagename, *args, **kwargs):
922923
'don\'t know how to discover from {!r}'
923924
.format(package))
924925

926+
def test_find_test_path_override_without_kind(self):
927+
# Subclasses override the private _find_test_path() with a
928+
# signature that has no *kind* parameter; file system discovery
929+
# must keep working for them.
930+
class Loader(unittest.TestLoader):
931+
def _find_test_path(self, full_path, pattern, namespace=False):
932+
return super()._find_test_path(full_path, pattern, namespace)
933+
934+
tmpdir = self.enterContext(os_helper.temp_dir())
935+
with open(os.path.join(tmpdir, 'test_override_gh157144.py'), 'w',
936+
encoding='utf-8') as f:
937+
f.write('import unittest\n'
938+
'class Tests(unittest.TestCase):\n'
939+
' def test_it(self):\n'
940+
' pass\n')
941+
self.enterContext(import_helper.DirsOnSysPath())
942+
self.addCleanup(import_helper.unload, 'test_override_gh157144')
943+
loader = Loader()
944+
suite = loader.discover(tmpdir)
945+
self.assertEqual(loader.errors, [])
946+
self.assertEqual(suite.countTestCases(), 1)
947+
925948

926949
class TestDiscoveryInArchive(unittest.TestCase):
927950
"""Discovery of tests inside a zip archive on sys.path (gh-157144)."""
@@ -953,6 +976,10 @@ def setUp(self):
953976
zf.writestr('zpkg/loaded/test_d.py', self.TEST_MODULE % ('D', 'd'))
954977
# A directory which is not a package is ignored.
955978
zf.writestr('zpkg/data/test_e.py', self.TEST_MODULE % ('E', 'e'))
979+
# An extension module cannot be imported from a zip file, so
980+
# it is ignored.
981+
for suffix in importlib.machinery.EXTENSION_SUFFIXES:
982+
zf.writestr(f'zpkg/test_ext{suffix}', '')
956983
self.enterContext(import_helper.DirsOnSysPath())
957984
self.addCleanup(self.forget_archive)
958985

@@ -1026,6 +1053,15 @@ def test_discover_not_a_package(self):
10261053
loader.discover(os.path.join(self.zip_path, start_dir),
10271054
top_level_dir=self.zip_path)
10281055

1056+
def test_discover_top_level_dir_not_in_archive(self):
1057+
# zipimport accepts any path below the archive, whether or not it
1058+
# exists there, so discovery has to reject these itself.
1059+
for start_dir in ('nonexistent', 'zpkg/test_a.py'):
1060+
with self.subTest(start_dir=start_dir):
1061+
loader = unittest.TestLoader()
1062+
with self.assertRaisesRegex(ImportError, 'not importable'):
1063+
loader.discover(os.path.join(self.zip_path, start_dir))
1064+
10291065

10301066
if __name__ == '__main__':
10311067
unittest.main()

‎Lib/unittest/loader.py‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,24 @@ def _is_archive_dir(path):
7878
7979
A test package can live inside an archive such as a zip file, where
8080
os.listdir() does not work but the path entry finder for the archive
81-
still knows the contents. The path hooks are probed directly rather
82-
than with pkgutil.get_importer(), which would cache a negative result
83-
in sys.path_importer_cache and so hide a directory created at that
84-
path later on.
81+
still knows the contents. A path hook may claim every path below its
82+
archive, so this does not tell whether *path* exists there.
8583
"""
86-
if os.path.isdir(path):
87-
return False
88-
if sys.path_importer_cache.get(path) is not None:
89-
return True
90-
for hook in sys.path_hooks:
91-
try:
92-
hook(path)
93-
except ImportError:
94-
continue
95-
return True
96-
return False
84+
return not os.path.isdir(path) and pkgutil.get_importer(path) is not None
9785

9886

9987
def _list_archive_dir(path):
10088
"""The sorted (name, is_package) pairs of the modules in *path*.
10189
10290
*path* is a directory inside an archive on sys.path; its path entry
103-
finder lists it (see pkgutil.iter_modules()).
91+
finder lists it (see pkgutil.iter_modules()). Names which the finder
92+
lists but cannot import, such as extension modules in a zip file, are
93+
left out.
10494
"""
95+
finder = pkgutil.get_importer(path)
10596
return sorted((name, ispkg)
106-
for _, name, ispkg in pkgutil.iter_modules([path]))
97+
for _, name, ispkg in pkgutil.iter_modules([path])
98+
if finder.find_spec(name) is not None)
10799

108100

109101
class TestLoader(object):
@@ -340,6 +332,12 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
340332
parent, name = os.path.split(start_dir)
341333
siblings = _list_archive_dir(parent)
342334
is_not_importable = (name, True) not in siblings
335+
else:
336+
# A finder cannot say whether a directory exists in its
337+
# archive, so apart from the archive file itself, one
338+
# which lists no modules is taken not to exist.
339+
is_not_importable = not (os.path.isfile(start_dir) or
340+
_list_archive_dir(start_dir))
343341
else:
344342
# support for discovery from dotted module names
345343
try:
@@ -443,7 +441,7 @@ def _find_tests(self, start_dir, pattern, namespace=False):
443441
if name != '.' and name not in self._loading_packages:
444442
# name is in self._loading_packages while we have called into
445443
# loadTestsFromModule with name.
446-
tests, should_recurse = self._find_test_path(
444+
tests, should_recurse = self._load_test_path(
447445
start_dir, pattern, namespace,
448446
_ARCHIVE_PACKAGE if in_archive else _FILESYSTEM_ENTRY)
449447
if tests is not None:
@@ -462,7 +460,7 @@ def _find_tests(self, start_dir, pattern, namespace=False):
462460
for path in sorted(os.listdir(start_dir))]
463461
for path, kind in paths:
464462
full_path = os.path.join(start_dir, path)
465-
tests, should_recurse = self._find_test_path(
463+
tests, should_recurse = self._load_test_path(
466464
full_path, pattern, False, kind)
467465
if tests is not None:
468466
yield tests
@@ -475,6 +473,14 @@ def _find_tests(self, start_dir, pattern, namespace=False):
475473
finally:
476474
self._loading_packages.discard(name)
477475

476+
def _load_test_path(self, full_path, pattern, namespace, kind):
477+
# Subclasses override _find_test_path() without a *kind* parameter.
478+
# Passing it for archive entries only keeps those overrides working
479+
# for discovery on the file system.
480+
if kind == _FILESYSTEM_ENTRY:
481+
return self._find_test_path(full_path, pattern, namespace)
482+
return self._find_test_path(full_path, pattern, namespace, kind)
483+
478484
def _find_test_path(self, full_path, pattern, namespace=False,
479485
kind=_FILESYSTEM_ENTRY):
480486
"""Used by discovery.

0 commit comments

Comments
 (0)