Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c6c232c

Browse files
committed
gh-59022: Added tests for pkgutil.extend_path (#59022)
This adds tests for the documented behaviour of `pkgutil.extend_path` regarding different argument types as well as for `*.pkg` files.
1 parent dc03ce7 commit c6c232c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Lib/test/test_pkgutil.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,42 @@ def test_mixed_namespace(self):
522522
del sys.modules['foo.bar']
523523
del sys.modules['foo.baz']
524524

525-
# XXX: test .pkg files
525+
526+
def test_extend_path_argument_types(self):
527+
pkgname = 'foo'
528+
dirname_0 = self.create_init(pkgname)
529+
530+
# If the input path is not a list it is returned unchanged
531+
self.assertEqual('notalist', pkgutil.extend_path('notalist', 'foo'))
532+
self.assertEqual(('not', 'a', 'list'), pkgutil.extend_path(('not', 'a', 'list'), 'foo'))
533+
self.assertEqual(123, pkgutil.extend_path(123, 'foo'))
534+
self.assertEqual(None, pkgutil.extend_path(None, 'foo'))
535+
536+
# Cleanup
537+
shutil.rmtree(dirname_0)
538+
539+
540+
def test_extend_path_pkg_files(self):
541+
pkgname = 'foo'
542+
dirname_0 = self.create_init(pkgname)
543+
544+
with open(os.path.join(dirname_0, 'bar.pkg'), 'w') as pkg_file:
545+
pkg_file.write('\n'.join([
546+
'baz',
547+
'/foo/bar/baz',
548+
'',
549+
'#comment'
550+
]))
551+
552+
# A *.pkg file is trusted at face value. Apart from checking for duplicates, all entries found in a *.pkg
553+
# file are added as-is to the resulting paths, regardless of whether they exist on the filesystem.
554+
extended_paths = pkgutil.extend_path(sys.path, 'bar')
555+
556+
self.assertEqual(extended_paths[-2], 'baz')
557+
self.assertEqual(extended_paths[-1], '/foo/bar/baz')
558+
559+
# Cleanup
560+
shutil.rmtree(dirname_0)
526561

527562

528563
class NestedNamespacePackageTest(unittest.TestCase):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tests for :class:`pkgutil.extend_path`. Patch by Andreas Stocker.

0 commit comments

Comments
 (0)