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

Skip to content

gh-59022: add tests to extend_path #12871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Lib/test/test_pkgutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments should be complete sentences.

path = 'path'
name = 'foo'
self.assertEqual('path', pkgutil.extend_path(path, name))

def test_parent_package_raise_key_error(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def test_parent_package_raise_key_error(self):
def test_parent_package_raises_key_error(self):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's nothing here making sure KeyError is raised, so the name of the test is confusing.

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name doesn't match what seems to be tested.

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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no cleanup of the temporary directory.

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()
Comment on lines +518 to +519
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a context manager and write a comment in the file as to the purpose of the file existing.

# Search valid or invalid module
import foo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will leave sys.modules dirty from this test. Use the helper functions in test.support to make sure this method cleans up after itself.

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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add tests for :class:`pkgutil.extend_path`. Patch by Windson Yang.