GH-127381: pathlib ABCs: remove PathBase.move() and move_into() (#128337)

These methods combine `_delete()` and `copy()`, but `_delete()` isn't part
of the public interface, and it's unlikely to be added until the pathlib
ABCs are made official, or perhaps even later.
This commit is contained in:
Barney Gale 2025-01-04 12:53:51 +00:00 committed by GitHub
parent a4e773c540
commit 95352dcb93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 148 additions and 153 deletions

View file

@ -1423,26 +1423,97 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
self.assertTrue(target.is_symlink())
self.assertEqual(source_readlink, target.readlink())
def test_move_file(self):
base = self.cls(self.base)
source = base / 'fileA'
source_text = source.read_text()
target = base / 'fileA_moved'
result = source.move(target)
self.assertEqual(result, target)
self.assertFalse(source.exists())
self.assertTrue(target.exists())
self.assertEqual(source_text, target.read_text())
@patch_replace
def test_move_file_other_fs(self):
self.test_move_file()
def test_move_file_to_file(self):
base = self.cls(self.base)
source = base / 'fileA'
source_text = source.read_text()
target = base / 'dirB' / 'fileB'
result = source.move(target)
self.assertEqual(result, target)
self.assertFalse(source.exists())
self.assertTrue(target.exists())
self.assertEqual(source_text, target.read_text())
@patch_replace
def test_move_file_to_file_other_fs(self):
self.test_move_file_to_file()
def test_move_file_to_dir(self):
base = self.cls(self.base)
source = base / 'fileA'
target = base / 'dirB'
self.assertRaises(OSError, source.move, target)
@patch_replace
def test_move_file_to_dir_other_fs(self):
self.test_move_file_to_dir()
def test_move_file_to_itself(self):
base = self.cls(self.base)
source = base / 'fileA'
self.assertRaises(OSError, source.move, source)
def test_move_dir(self):
base = self.cls(self.base)
source = base / 'dirC'
target = base / 'dirC_moved'
result = source.move(target)
self.assertEqual(result, target)
self.assertFalse(source.exists())
self.assertTrue(target.is_dir())
self.assertTrue(target.joinpath('dirD').is_dir())
self.assertTrue(target.joinpath('dirD', 'fileD').is_file())
self.assertEqual(target.joinpath('dirD', 'fileD').read_text(),
"this is file D\n")
self.assertTrue(target.joinpath('fileC').is_file())
self.assertTrue(target.joinpath('fileC').read_text(),
"this is file C\n")
@patch_replace
def test_move_dir_other_fs(self):
self.test_move_dir()
def test_move_dir_to_dir(self):
base = self.cls(self.base)
source = base / 'dirC'
target = base / 'dirB'
self.assertRaises(OSError, source.move, target)
self.assertTrue(source.exists())
self.assertTrue(target.exists())
@patch_replace
def test_move_dir_to_dir_other_fs(self):
self.test_move_dir_to_dir()
def test_move_dir_to_itself(self):
base = self.cls(self.base)
source = base / 'dirC'
self.assertRaises(OSError, source.move, source)
self.assertTrue(source.exists())
def test_move_dir_into_itself(self):
base = self.cls(self.base)
source = base / 'dirC'
target = base / 'dirC' / 'bar'
self.assertRaises(OSError, source.move, target)
self.assertTrue(source.exists())
self.assertFalse(target.exists())
@patch_replace
def test_move_dir_into_itself_other_fs(self):
self.test_move_dir_into_itself()
@ -1472,10 +1543,26 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
def test_move_dangling_symlink_other_fs(self):
self.test_move_dangling_symlink()
def test_move_into(self):
base = self.cls(self.base)
source = base / 'fileA'
source_text = source.read_text()
target_dir = base / 'dirA'
result = source.move_into(target_dir)
self.assertEqual(result, target_dir / 'fileA')
self.assertFalse(source.exists())
self.assertTrue(result.exists())
self.assertEqual(source_text, result.read_text())
@patch_replace
def test_move_into_other_os(self):
self.test_move_into()
def test_move_into_empty_name(self):
source = self.cls('')
target_dir = self.base
self.assertRaises(ValueError, source.move_into, target_dir)
@patch_replace
def test_move_into_empty_name_other_os(self):
self.test_move_into_empty_name()
@ -1794,6 +1881,37 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
self.assertFileNotFound(p.stat)
self.assertFileNotFound(p.unlink)
def test_delete_file(self):
p = self.cls(self.base) / 'fileA'
p._delete()
self.assertFalse(p.exists())
self.assertFileNotFound(p._delete)
def test_delete_dir(self):
base = self.cls(self.base)
base.joinpath('dirA')._delete()
self.assertFalse(base.joinpath('dirA').exists())
self.assertFalse(base.joinpath('dirA', 'linkC').exists(
follow_symlinks=False))
base.joinpath('dirB')._delete()
self.assertFalse(base.joinpath('dirB').exists())
self.assertFalse(base.joinpath('dirB', 'fileB').exists())
self.assertFalse(base.joinpath('dirB', 'linkD').exists(
follow_symlinks=False))
base.joinpath('dirC')._delete()
self.assertFalse(base.joinpath('dirC').exists())
self.assertFalse(base.joinpath('dirC', 'dirD').exists())
self.assertFalse(base.joinpath('dirC', 'dirD', 'fileD').exists())
self.assertFalse(base.joinpath('dirC', 'fileC').exists())
self.assertFalse(base.joinpath('dirC', 'novel.txt').exists())
def test_delete_missing(self):
tmp = self.cls(self.base, 'delete')
tmp.mkdir()
# filename is guaranteed not to exist
filename = tmp / 'foo'
self.assertRaises(FileNotFoundError, filename._delete)
@needs_symlinks
def test_delete_symlink(self):
tmp = self.cls(self.base, 'delete')