You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The documentation for shutil.move says Recursively move a file or directory (src) to another location (dst) and return the destination.
If shutil.move is used with pyfakefs and directory is given as source, it tries to move the directory to the same path as target (instead of sub directory of the target).
The following code can be used to demonstrate the issue:
import os
import shutil
import tempfile
import unittest
import pyfakefs.fake_filesystem_unittest
class TestMixin:
def test_shutil_move(self):
with tempfile.TemporaryDirectory() as temp_path:
source = os.path.join(temp_path, 'source')
self.create_file(os.path.join(source, 'f1'))
self.create_file(os.path.join(source, 's', 'f2'))
target = os.path.join(temp_path, 'target')
os.makedirs(target)
shutil.move(os.path.join(source, 'f1'), target)
shutil.move(os.path.join(source, 's'), target)
print(list(self.gen_sub_paths(temp_path)))
def create_file(self, path):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'wt') as f:
f.write(path)
def gen_sub_paths(self, top):
for root, dirs, files in os.walk(top):
yield from (
os.path.join(root, filename)
for filename in files
)
class RealCase(unittest.TestCase, TestMixin):
pass
class FakeFsCase(pyfakefs.fake_filesystem_unittest.TestCase, TestMixin):
def setUp(self):
super().setUp()
self.setUpPyfakefs()
If you run the above file as unit test -m unittest -v the following output is produced:
test_shutil_move (pyfakefs_issue.FakeFsCase) ... ERROR
test_shutil_move (pyfakefs_issue.RealCase) ... ['/tmp/tmps0zlprzh/target/f1', '/tmp/tmps0zlprzh/target/s/f2']
ok
======================================================================
ERROR: test_shutil_move (pyfakefs_issue.FakeFsCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/pyfakefs_issue.py", line 21, in test_shutil_move
shutil.move(os.path.join(source, 's'), target)
File "/tmp/venv/lib/python3.5/site-packages/pyfakefs/fake_filesystem_shutil.py", line 281, in move
raise shutil.Error("Destination path '%s' already exists" % dst)
shutil.Error: Destination path '/tmp/tmpp7gqg2rz/target' already exists
----------------------------------------------------------------------
Ran 2 tests in 0.004s
FAILED (errors=1)
The text was updated successfully, but these errors were encountered:
Well spotted! Seems to be an overlooked bug that has been sitting there since the beginning of time...
Please change the PR according to the review comment (otherwise the test will fail under Windows) and I will merge it.
The documentation for shutil.move says Recursively move a file or directory (src) to another location (dst) and return the destination.
If shutil.move is used with pyfakefs and directory is given as source, it tries to move the directory to the same path as target (instead of sub directory of the target).
The following code can be used to demonstrate the issue:
If you run the above file as unit test
-m unittest -v
the following output is produced:The text was updated successfully, but these errors were encountered: