Skip to content

Commit

Permalink
Merge pull request #510 from PyFilesystem/fix-memoryfs-move
Browse files Browse the repository at this point in the history
Fix `move` and `movedir` methods of `MemoryFS` not renaming the entry
  • Loading branch information
althonos authored Dec 13, 2021
2 parents 47f9cfd + d76e7f3 commit f3338d9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

- 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).
### Changed

- Support more lenient usernames and group names in FTP servers
([#507](https://github.com/PyFilesystem/pyfilesystem2/pull/507)).
Closes [#506](https://github.com/PyFilesystem/pyfilesystem2/issues/506).

### Fixed

- Fixed `MemoryFS.move` and `MemoryFS.movedir` not updating the name of moved
resources, causing `MemoryFS.scandir` to use the old name.
([#510](https://github.com/PyFilesystem/pyfilesystem2/pull/510)).
Closes [#509](https://github.com/PyFilesystem/pyfilesystem2/issues/509).


## [2.4.14] - 2021-11-16

Expand Down
7 changes: 7 additions & 0 deletions fs/memoryfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,11 @@ def move(self, src_path, dst_path, overwrite=False, preserve_time=False):
elif not overwrite and dst_name in dst_dir_entry:
raise errors.DestinationExists(dst_path)

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

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

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

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

if preserve_time:
copy_modified_time(self, src_path, self, dst_path)
Expand Down
34 changes: 21 additions & 13 deletions fs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,24 @@ def test_copy_dir_temp(self):
self._test_copy_dir("temp://")
self._test_copy_dir_write("temp://")

def test_move_dir_same_fs(self):
self.fs.makedirs("foo/bar/baz")
self.fs.makedir("egg")
self.fs.writetext("top.txt", "Hello, World")
self.fs.writetext("/foo/bar/baz/test.txt", "Goodbye, World")

fs.move.move_dir(self.fs, "foo", self.fs, "foo2")

expected = {"/egg", "/foo2", "/foo2/bar", "/foo2/bar/baz"}
self.assertEqual(set(walk.walk_dirs(self.fs)), expected)
self.assert_text("top.txt", "Hello, World")
self.assert_text("/foo2/bar/baz/test.txt", "Goodbye, World")

self.assertEqual(sorted(self.fs.listdir("/")), ["egg", "foo2", "top.txt"])
self.assertEqual(
sorted(x.name for x in self.fs.scandir("/")), ["egg", "foo2", "top.txt"]
)

def _test_move_dir_write(self, protocol):
# Test moving to this filesystem from another.
other_fs = open_fs(protocol)
Expand All @@ -1760,19 +1778,6 @@ def test_move_dir_mem(self):
def test_move_dir_temp(self):
self._test_move_dir_write("temp://")

def test_move_same_fs(self):
self.fs.makedirs("foo/bar/baz")
self.fs.makedir("egg")
self.fs.writetext("top.txt", "Hello, World")
self.fs.writetext("/foo/bar/baz/test.txt", "Goodbye, World")

fs.move.move_dir(self.fs, "foo", self.fs, "foo2")

expected = {"/egg", "/foo2", "/foo2/bar", "/foo2/bar/baz"}
self.assertEqual(set(walk.walk_dirs(self.fs)), expected)
self.assert_text("top.txt", "Hello, World")
self.assert_text("/foo2/bar/baz/test.txt", "Goodbye, World")

def test_move_file_same_fs(self):
text = "Hello, World"
self.fs.makedir("foo").writetext("test.txt", text)
Expand All @@ -1782,6 +1787,9 @@ def test_move_file_same_fs(self):
self.assert_not_exists("foo/test.txt")
self.assert_text("foo/test2.txt", text)

self.assertEqual(self.fs.listdir("foo"), ["test2.txt"])
self.assertEqual(next(self.fs.scandir("foo")).name, "test2.txt")

def _test_move_file(self, protocol):
other_fs = open_fs(protocol)

Expand Down

0 comments on commit f3338d9

Please sign in to comment.