Skip to content

Commit 2c013da

Browse files
committed
Improve test
1 parent 4fedf3e commit 2c013da

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

Lib/test/test_pkgutil.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,40 @@ def test_getdata_zipfile(self):
9393

9494
del sys.modules[pkg]
9595

96-
def test_iter_modules(self):
96+
def test_issue44061_iter_modules(self):
9797
#see: issue44061
98-
actual = list(pkgutil.iter_modules([Path("/home")], 'somepackage.somesubpackage'))
99-
self.assertListEqual([], actual)
98+
zip = 'test_getdata_zipfile.zip'
99+
pkg = 'test_getdata_zipfile'
100+
101+
# Include a LF and a CRLF, to test that binary data is read back
102+
RESOURCE_DATA = b'Hello, world!\nSecond line\r\nThird line'
103+
104+
# Make a package with some resources
105+
zip_file = os.path.join(self.dirname, zip)
106+
z = zipfile.ZipFile(zip_file, 'w')
107+
108+
# Empty init.py
109+
z.writestr(pkg + '/__init__.py', "")
110+
# Resource files, res.txt
111+
z.writestr(pkg + '/res.txt', RESOURCE_DATA)
112+
z.close()
113+
114+
# Check we can read the resources
115+
sys.path.insert(0, zip_file)
116+
res = pkgutil.get_data(pkg, 'res.txt')
117+
self.assertEqual(res, RESOURCE_DATA)
118+
119+
# make sure iter_modules accepts Path objects
120+
names = []
121+
for moduleinfo in pkgutil.iter_modules([Path(zip_file)]):
122+
self.assertIsInstance(moduleinfo, pkgutil.ModuleInfo)
123+
names.append(moduleinfo.name)
124+
self.assertEqual(names, [pkg])
125+
126+
del sys.path[0]
127+
del sys.modules[pkg]
100128

129+
# assert path must be None or list of paths
101130
expected_msg = "path must be None or list of paths to look for modules in"
102131
with self.assertRaisesRegex(ValueError, expected_msg):
103132
list(pkgutil.iter_modules("invalid_path"))

0 commit comments

Comments
 (0)