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

Skip to content

Commit 8fb5f1c

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 8fb5f1c

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Lib/test/test_pkgutil.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,44 @@ 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+
del sys.path[0]
539+
540+
541+
def test_extend_path_pkg_files(self):
542+
pkgname = 'foo'
543+
dirname_0 = self.create_init(pkgname)
544+
545+
with open(os.path.join(dirname_0, 'bar.pkg'), 'w') as pkg_file:
546+
pkg_file.write('\n'.join([
547+
'baz',
548+
'/foo/bar/baz',
549+
'',
550+
'#comment'
551+
]))
552+
553+
# A *.pkg file is trusted at face value. Apart from checking for duplicates, all entries found in a *.pkg
554+
# file are added as-is to the resulting paths, regardless of whether they exist on the filesystem.
555+
extended_paths = pkgutil.extend_path(sys.path, 'bar')
556+
557+
self.assertEqual(extended_paths[-2], 'baz')
558+
self.assertEqual(extended_paths[-1], '/foo/bar/baz')
559+
560+
# Cleanup
561+
shutil.rmtree(dirname_0)
562+
del sys.path[0]
526563

527564

528565
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)