summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pathlib
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-06-18 22:13:45 (GMT)
committerGitHub <noreply@github.com>2024-06-18 22:13:45 (GMT)
commit9f741e55c16376412c1473aa45b94314c00a0c43 (patch)
treeb50b499a852965ab21923a96c0561fe2afbae4fa /Lib/test/test_pathlib
parentb7f478948fcf3bf8e62c79ebcb3ff69bf06d9c4d (diff)
downloadcpython-9f741e55c16376412c1473aa45b94314c00a0c43.zip
cpython-9f741e55c16376412c1473aa45b94314c00a0c43.tar.gz
cpython-9f741e55c16376412c1473aa45b94314c00a0c43.tar.bz2
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`.
Diffstat (limited to 'Lib/test/test_pathlib')
-rw-r--r--Lib/test/test_pathlib/test_pathlib.py19
-rw-r--r--Lib/test/test_pathlib/test_pathlib_abc.py52
2 files changed, 51 insertions, 20 deletions
diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py
index 3df354e..89af1f7 100644
--- a/Lib/test/test_pathlib/test_pathlib.py
+++ b/Lib/test/test_pathlib/test_pathlib.py
@@ -764,25 +764,6 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
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)
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py
index fd71284..1c95905 100644
--- a/Lib/test/test_pathlib/test_pathlib_abc.py
+++ b/Lib/test/test_pathlib/test_pathlib_abc.py
@@ -1496,7 +1496,7 @@ class DummyPath(PathBase):
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)
@@ -1517,6 +1517,37 @@ class DummyPath(PathBase):
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()."""
@@ -2400,6 +2431,25 @@ class DummyPathTest(DummyPurePathTest):
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/