Skip to content

Commit 9f741e5

Browse files
authored
GH-73991: pathlib ABC tests: add DummyPath.unlink() and rmdir() (#120715)
In preparation for the addition of `PathBase.rmtree()`, implement `DummyPath.unlink()` and `rmdir()`, and move corresponding tests into `test_pathlib_abc` so they're run against `DummyPath`.
1 parent b7f4789 commit 9f741e5

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

Lib/test/test_pathlib/test_pathlib.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -764,25 +764,6 @@ def test_group_no_follow_symlinks(self):
764764
self.assertEqual(expected_gid, gid_2)
765765
self.assertEqual(expected_name, link.group(follow_symlinks=False))
766766

767-
def test_unlink(self):
768-
p = self.cls(self.base) / 'fileA'
769-
p.unlink()
770-
self.assertFileNotFound(p.stat)
771-
self.assertFileNotFound(p.unlink)
772-
773-
def test_unlink_missing_ok(self):
774-
p = self.cls(self.base) / 'fileAAA'
775-
self.assertFileNotFound(p.unlink)
776-
p.unlink(missing_ok=True)
777-
778-
def test_rmdir(self):
779-
p = self.cls(self.base) / 'dirA'
780-
for q in p.iterdir():
781-
q.unlink()
782-
p.rmdir()
783-
self.assertFileNotFound(p.stat)
784-
self.assertFileNotFound(p.unlink)
785-
786767
@os_helper.skip_unless_hardlink
787768
def test_hardlink_to(self):
788769
P = self.cls(self.base)

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,7 @@ def iterdir(self):
14961496
if path in self._files:
14971497
raise NotADirectoryError(errno.ENOTDIR, "Not a directory", path)
14981498
elif path in self._directories:
1499-
return (self / name for name in self._directories[path])
1499+
return iter([self / name for name in self._directories[path]])
15001500
else:
15011501
raise FileNotFoundError(errno.ENOENT, "File not found", path)
15021502

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

1520+
def unlink(self, missing_ok=False):
1521+
path_obj = self.parent.resolve(strict=True) / self.name
1522+
path = str(path_obj)
1523+
name = path_obj.name
1524+
parent = str(path_obj.parent)
1525+
if path in self._directories:
1526+
raise IsADirectoryError(errno.EISDIR, "Is a directory", path)
1527+
elif path in self._files:
1528+
self._directories[parent].remove(name)
1529+
del self._files[path]
1530+
elif path in self._symlinks:
1531+
self._directories[parent].remove(name)
1532+
del self._symlinks[path]
1533+
elif not missing_ok:
1534+
raise FileNotFoundError(errno.ENOENT, "File not found", path)
1535+
1536+
def rmdir(self):
1537+
path_obj = self.parent.resolve(strict=True) / self.name
1538+
path = str(path_obj)
1539+
if path in self._files or path in self._symlinks:
1540+
raise NotADirectoryError(errno.ENOTDIR, "Not a directory", path)
1541+
elif path not in self._directories:
1542+
raise FileNotFoundError(errno.ENOENT, "File not found", path)
1543+
elif self._directories[path]:
1544+
raise OSError(errno.ENOTEMPTY, "Directory not empty", path)
1545+
else:
1546+
name = path_obj.name
1547+
parent = str(path_obj.parent)
1548+
self._directories[parent].remove(name)
1549+
del self._directories[path]
1550+
15201551

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

2434+
def test_unlink(self):
2435+
p = self.cls(self.base) / 'fileA'
2436+
p.unlink()
2437+
self.assertFileNotFound(p.stat)
2438+
self.assertFileNotFound(p.unlink)
2439+
2440+
def test_unlink_missing_ok(self):
2441+
p = self.cls(self.base) / 'fileAAA'
2442+
self.assertFileNotFound(p.unlink)
2443+
p.unlink(missing_ok=True)
2444+
2445+
def test_rmdir(self):
2446+
p = self.cls(self.base) / 'dirA'
2447+
for q in p.iterdir():
2448+
q.unlink()
2449+
p.rmdir()
2450+
self.assertFileNotFound(p.stat)
2451+
self.assertFileNotFound(p.unlink)
2452+
24032453
def setUpWalk(self):
24042454
# Build:
24052455
# TESTFN/

0 commit comments

Comments
 (0)