Skip to content

Commit f3338d9

Browse files
authored
Merge pull request #510 from PyFilesystem/fix-memoryfs-move
Fix `move` and `movedir` methods of `MemoryFS` not renaming the entry
2 parents 47f9cfd + d76e7f3 commit f3338d9

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88

99
## Unreleased
1010

11-
- Support more leniant usernames and group names in FTP servers [#507](https://github.com/PyFilesystem/pyfilesystem2/pull/507). Closes [#506](https://github.com/PyFilesystem/pyfilesystem2/pull/506).
11+
### Changed
12+
13+
- Support more lenient usernames and group names in FTP servers
14+
([#507](https://github.com/PyFilesystem/pyfilesystem2/pull/507)).
15+
Closes [#506](https://github.com/PyFilesystem/pyfilesystem2/issues/506).
16+
17+
### Fixed
18+
19+
- Fixed `MemoryFS.move` and `MemoryFS.movedir` not updating the name of moved
20+
resources, causing `MemoryFS.scandir` to use the old name.
21+
([#510](https://github.com/PyFilesystem/pyfilesystem2/pull/510)).
22+
Closes [#509](https://github.com/PyFilesystem/pyfilesystem2/issues/509).
23+
1224

1325
## [2.4.14] - 2021-11-16
1426

fs/memoryfs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,11 @@ def move(self, src_path, dst_path, overwrite=False, preserve_time=False):
463463
elif not overwrite and dst_name in dst_dir_entry:
464464
raise errors.DestinationExists(dst_path)
465465

466+
# move the entry from the src folder to the dst folder
466467
dst_dir_entry.set_entry(dst_name, src_entry)
467468
src_dir_entry.remove_entry(src_name)
469+
# make sure to update the entry name itself (see #509)
470+
src_entry.name = dst_name
468471

469472
if preserve_time:
470473
copy_modified_time(self, src_path, self, dst_path)
@@ -481,12 +484,16 @@ def movedir(self, src_path, dst_path, create=False, preserve_time=False):
481484
if not src_entry.is_dir:
482485
raise errors.DirectoryExpected(src_path)
483486

487+
# move the entry from the src folder to the dst folder
484488
dst_dir_entry = self._get_dir_entry(dst_dir)
485489
if dst_dir_entry is None or (not create and dst_name not in dst_dir_entry):
486490
raise errors.ResourceNotFound(dst_path)
487491

492+
# move the entry from the src folder to the dst folder
488493
dst_dir_entry.set_entry(dst_name, src_entry)
489494
src_dir_entry.remove_entry(src_name)
495+
# make sure to update the entry name itself (see #509)
496+
src_entry.name = dst_name
490497

491498
if preserve_time:
492499
copy_modified_time(self, src_path, self, dst_path)

fs/test.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,24 @@ def test_copy_dir_temp(self):
17381738
self._test_copy_dir("temp://")
17391739
self._test_copy_dir_write("temp://")
17401740

1741+
def test_move_dir_same_fs(self):
1742+
self.fs.makedirs("foo/bar/baz")
1743+
self.fs.makedir("egg")
1744+
self.fs.writetext("top.txt", "Hello, World")
1745+
self.fs.writetext("/foo/bar/baz/test.txt", "Goodbye, World")
1746+
1747+
fs.move.move_dir(self.fs, "foo", self.fs, "foo2")
1748+
1749+
expected = {"/egg", "/foo2", "/foo2/bar", "/foo2/bar/baz"}
1750+
self.assertEqual(set(walk.walk_dirs(self.fs)), expected)
1751+
self.assert_text("top.txt", "Hello, World")
1752+
self.assert_text("/foo2/bar/baz/test.txt", "Goodbye, World")
1753+
1754+
self.assertEqual(sorted(self.fs.listdir("/")), ["egg", "foo2", "top.txt"])
1755+
self.assertEqual(
1756+
sorted(x.name for x in self.fs.scandir("/")), ["egg", "foo2", "top.txt"]
1757+
)
1758+
17411759
def _test_move_dir_write(self, protocol):
17421760
# Test moving to this filesystem from another.
17431761
other_fs = open_fs(protocol)
@@ -1760,19 +1778,6 @@ def test_move_dir_mem(self):
17601778
def test_move_dir_temp(self):
17611779
self._test_move_dir_write("temp://")
17621780

1763-
def test_move_same_fs(self):
1764-
self.fs.makedirs("foo/bar/baz")
1765-
self.fs.makedir("egg")
1766-
self.fs.writetext("top.txt", "Hello, World")
1767-
self.fs.writetext("/foo/bar/baz/test.txt", "Goodbye, World")
1768-
1769-
fs.move.move_dir(self.fs, "foo", self.fs, "foo2")
1770-
1771-
expected = {"/egg", "/foo2", "/foo2/bar", "/foo2/bar/baz"}
1772-
self.assertEqual(set(walk.walk_dirs(self.fs)), expected)
1773-
self.assert_text("top.txt", "Hello, World")
1774-
self.assert_text("/foo2/bar/baz/test.txt", "Goodbye, World")
1775-
17761781
def test_move_file_same_fs(self):
17771782
text = "Hello, World"
17781783
self.fs.makedir("foo").writetext("test.txt", text)
@@ -1782,6 +1787,9 @@ def test_move_file_same_fs(self):
17821787
self.assert_not_exists("foo/test.txt")
17831788
self.assert_text("foo/test2.txt", text)
17841789

1790+
self.assertEqual(self.fs.listdir("foo"), ["test2.txt"])
1791+
self.assertEqual(next(self.fs.scandir("foo")).name, "test2.txt")
1792+
17851793
def _test_move_file(self, protocol):
17861794
other_fs = open_fs(protocol)
17871795

0 commit comments

Comments
 (0)