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

cannot use pyfakefs and tempfile at the same time #384

Closed
franklingu opened this issue May 7, 2018 · 3 comments
Closed

cannot use pyfakefs and tempfile at the same time #384

franklingu opened this issue May 7, 2018 · 3 comments

Comments

@franklingu
Copy link

I am not sure if this is actually an issue or it is something that I do not understand. So I have a module that makes use of tempfile to create temporary directory and manipulate files in the directory. When I test with out pyfakefs, things are OK and tests are OK as well. But I when I load pyfakefs in another test file that will be run before this test file, things will break -- temporary dir will not be created, and writing to files in temprorary dir will fail of course.

I am using Python 3.6.1. Ubuntu 16.04.

A minimal case:

pyfakefs_test_code

And the output:

(.venv36) jgu@jgu-pc:~/repos/temp % py.test -vv -s tests/test_abc.py
=========================================================================================== test session starts ============================================================================================
platform linux -- Python 3.6.1, pytest-3.4.2, py-1.5.2, pluggy-0.6.0 -- /home/jgu/repos/.venv36/bin/python3.6
cachedir: .pytest_cache
rootdir: /home/jgu/repos/temp, inifile:
plugins: mock-1.7.1, cov-2.5.1, pyfakefs-3.4
collected 1 item                                                                                                                                                                                           

tests/test_abc.py::test_func PASSED

========================================================================================= 1 passed in 0.11 seconds =========================================================================================
(.venv36) jgu@jgu-pc:~/repos/temp % py.test -vv -s                  
=========================================================================================== test session starts ============================================================================================
platform linux -- Python 3.6.1, pytest-3.4.2, py-1.5.2, pluggy-0.6.0 -- /home/jgu/repos/.venv36/bin/python3.6
cachedir: .pytest_cache
rootdir: /home/jgu/repos/temp, inifile:
plugins: mock-1.7.1, cov-2.5.1, pyfakefs-3.4
collected 2 items                                                                                                                                                                                          

tests/test_a.py::test_void PASSED
tests/test_abc.py::test_func FAILED

================================================================================================= FAILURES =================================================================================================
________________________________________________________________________________________________ test_func _________________________________________________________________________________________________

    def func():
        try:
            td = tempfile.TemporaryDirectory()
>           with open(td.name + '/tmp.txt', 'w') as ofile:
E           FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpoepn6_lx/tmp.txt'

packa/abc.py:7: FileNotFoundError

During handling of the above exception, another exception occurred:

    def test_func():
>       assert abc.func()

tests/test_abc.py:5: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
packa/abc.py:11: in func
    td.cleanup()
../.venv36/lib/python3.6/tempfile.py:811: in cleanup
    _shutil.rmtree(self.name)
../.venv36/lib/python3.6/shutil.py:471: in rmtree
    onerror(os.lstat, path, sys.exc_info())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

path = '/tmp/tmpoepn6_lx', ignore_errors = False, onerror = <function rmtree.<locals>.onerror at 0x7f221a845bf8>

    def rmtree(path, ignore_errors=False, onerror=None):
        """Recursively delete a directory tree.
    
        If ignore_errors is set, errors are ignored; otherwise, if onerror
        is set, it is called to handle the error with arguments (func,
        path, exc_info) where func is platform and implementation dependent;
        path is the argument to that function that caused it to fail; and
        exc_info is a tuple returned by sys.exc_info().  If ignore_errors
        is false and onerror is None, an exception is raised.
    
        """
        if ignore_errors:
            def onerror(*args):
                pass
        elif onerror is None:
            def onerror(*args):
                raise
        if _use_fd_functions:
            # While the unsafe rmtree works fine on bytes, the fd based does not.
            if isinstance(path, bytes):
                path = os.fsdecode(path)
            # Note: To guard against symlink races, we use the standard
            # lstat()/open()/fstat() trick.
            try:
>               orig_st = os.lstat(path)
E               FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpoepn6_lx'

../.venv36/lib/python3.6/shutil.py:469: FileNotFoundError
==================================================================================== 1 failed, 1 passed in 0.19 seconds ====================================================================================
@mrbean-bremen
Copy link
Member

Could you please test with pyfakefs 3.4.1? There has been a similar issue that has been fixed in this release.

@franklingu
Copy link
Author

I just upgraded. tests are OK now. I think can close this issue. Thanks a lot for the help

@mrbean-bremen
Copy link
Member

You are welcome!

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

No branches or pull requests

2 participants