Skip to content

GH-73991: pathlib ABC tests: add DummyPath.unlink() and rmdir() #120715

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

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
19 changes: 0 additions & 19 deletions Lib/test/test_pathlib/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,25 +764,6 @@ def test_group_no_follow_symlinks(self):
self.assertEqual(expected_gid, gid_2)
self.assertEqual(expected_name, link.group(follow_symlinks=False))

def test_unlink(self):
p = self.cls(self.base) / 'fileA'
p.unlink()
self.assertFileNotFound(p.stat)
self.assertFileNotFound(p.unlink)

def test_unlink_missing_ok(self):
p = self.cls(self.base) / 'fileAAA'
self.assertFileNotFound(p.unlink)
p.unlink(missing_ok=True)

def test_rmdir(self):
p = self.cls(self.base) / 'dirA'
for q in p.iterdir():
q.unlink()
p.rmdir()
self.assertFileNotFound(p.stat)
self.assertFileNotFound(p.unlink)

@os_helper.skip_unless_hardlink
def test_hardlink_to(self):
P = self.cls(self.base)
Expand Down
52 changes: 51 additions & 1 deletion Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ def iterdir(self):
if path in self._files:
raise NotADirectoryError(errno.ENOTDIR, "Not a directory", path)
elif path in self._directories:
return (self / name for name in self._directories[path])
return iter([self / name for name in self._directories[path]])
else:
raise FileNotFoundError(errno.ENOENT, "File not found", path)

Expand All @@ -1517,6 +1517,37 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
self.parent.mkdir(parents=True, exist_ok=True)
self.mkdir(mode, parents=False, exist_ok=exist_ok)

def unlink(self, missing_ok=False):
path_obj = self.parent.resolve(strict=True) / self.name
path = str(path_obj)
name = path_obj.name
parent = str(path_obj.parent)
if path in self._directories:
raise IsADirectoryError(errno.EISDIR, "Is a directory", path)
elif path in self._files:
self._directories[parent].remove(name)
del self._files[path]
elif path in self._symlinks:
self._directories[parent].remove(name)
del self._symlinks[path]
elif not missing_ok:
raise FileNotFoundError(errno.ENOENT, "File not found", path)

def rmdir(self):
path_obj = self.parent.resolve(strict=True) / self.name
path = str(path_obj)
if path in self._files or path in self._symlinks:
raise NotADirectoryError(errno.ENOTDIR, "Not a directory", path)
elif path not in self._directories:
raise FileNotFoundError(errno.ENOENT, "File not found", path)
elif self._directories[path]:
raise OSError(errno.ENOTEMPTY, "Directory not empty", path)
else:
name = path_obj.name
parent = str(path_obj.parent)
self._directories[parent].remove(name)
del self._directories[path]


class DummyPathTest(DummyPurePathTest):
"""Tests for PathBase methods that use stat(), open() and iterdir()."""
Expand Down Expand Up @@ -2400,6 +2431,25 @@ def test_complex_symlinks_relative(self):
def test_complex_symlinks_relative_dot_dot(self):
self._check_complex_symlinks(self.parser.join('dirA', '..'))

def test_unlink(self):
p = self.cls(self.base) / 'fileA'
p.unlink()
self.assertFileNotFound(p.stat)
self.assertFileNotFound(p.unlink)

def test_unlink_missing_ok(self):
p = self.cls(self.base) / 'fileAAA'
self.assertFileNotFound(p.unlink)
p.unlink(missing_ok=True)

def test_rmdir(self):
p = self.cls(self.base) / 'dirA'
for q in p.iterdir():
q.unlink()
p.rmdir()
self.assertFileNotFound(p.stat)
self.assertFileNotFound(p.unlink)

def setUpWalk(self):
# Build:
# TESTFN/
Expand Down
Loading