Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shutil.move does not work like the real one #145

Closed
janneronkko opened this issue Dec 20, 2016 · 3 comments
Closed

shutil.move does not work like the real one #145

janneronkko opened this issue Dec 20, 2016 · 3 comments
Assignees
Labels

Comments

@janneronkko
Copy link
Contributor

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)
@mrbean-bremen mrbean-bremen self-assigned this Dec 20, 2016
@mrbean-bremen
Copy link
Member

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.

@mrbean-bremen
Copy link
Member

Thanks for the contribution!

@mrbean-bremen
Copy link
Member

Fixed by @jannero

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants