From 0a20b16545ec47600cc8c987c71300bd28d45fa2 Mon Sep 17 00:00:00 2001 From: Windson yang Date: Thu, 18 Apr 2019 10:36:11 +0800 Subject: [PATCH 1/2] bpo-14817: add tests to extend_path --- Lib/test/test_pkgutil.py | 55 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py index 2887ce6cc055da..7dc1f76ab881b4 100644 --- a/Lib/test/test_pkgutil.py +++ b/Lib/test/test_pkgutil.py @@ -357,6 +357,61 @@ def test_mixed_namespace(self): # XXX: test .pkg files +class ExtendPathBaseTests(unittest.TestCase): + def test_input_type_invalid(self): + # path should be list instead of string + path = 'path' + name = 'foo' + self.assertEqual('path', pkgutil.extend_path(path, name)) + + def test_parent_package_raise_key_error(self): + path = ['path'] + # sys.modules['foo'] raise KeyError + name = 'foo.bar' + self.assertEqual(['path'], pkgutil.extend_path(path, name)) + + def test_parent_package_raise_attr_error(self): + path = ['path'] + # module 'datetime' has no attribute '__path__' + name = 'datetime.date' + self.assertEqual(['path'], pkgutil.extend_path(path, name)) + + def test_extend_path_files(self): + # create foo/bar.py + self.dirname = tempfile.mkdtemp() + sys.path.insert(0, self.dirname) + package_name = "foo" + package_path = os.path.join(self.dirname, package_name) + os.mkdir(package_path) + f = open(os.path.join(package_path, 'bar.py'), "w") + f.close() + # Search valid or invalid module + import foo + path = [foo.__path__] + name = 'foo' + self.assertEqual([path[0], package_path], pkgutil.extend_path(path, name)) + name = 'other' + self.assertEqual(path, pkgutil.extend_path(path, name)) + del sys.path[0] + + def test_extend_pkg_files(self): + self.dirname = tempfile.mkdtemp() + sys.path.insert(0, self.dirname) + package_name = "foo" + package_path = os.path.join(self.dirname, package_name) + sys.path.insert(1, package_path) + os.mkdir(package_path) + f = open(os.path.join(package_path, 'bar.pkg'), "w") + f.write('foo\nbar\n#other\n') + f.close() + # Search valid or invalid module + import foo + path = [foo.__path__] + self.assertEqual([path[0], 'foo', 'bar'], pkgutil.extend_path(path, 'bar')) + del sys.path[0] + del sys.path[1] + + class NestedNamespacePackageTest(unittest.TestCase): def setUp(self): From 61d3986bbee9037be330d3f9f8dd91c86e1aa959 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Sun, 5 Feb 2023 22:16:42 +0400 Subject: [PATCH 2/2] Add the news entry --- .../next/Tests/2023-02-05-22-16-29.gh-issue-59022.dTHNJi.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tests/2023-02-05-22-16-29.gh-issue-59022.dTHNJi.rst diff --git a/Misc/NEWS.d/next/Tests/2023-02-05-22-16-29.gh-issue-59022.dTHNJi.rst b/Misc/NEWS.d/next/Tests/2023-02-05-22-16-29.gh-issue-59022.dTHNJi.rst new file mode 100644 index 00000000000000..4432cf5ba35eb5 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2023-02-05-22-16-29.gh-issue-59022.dTHNJi.rst @@ -0,0 +1 @@ +Add tests for :class:`pkgutil.extend_path`. Patch by Windson Yang.