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

No such file or directory in the fake filesystem: b'/tmp' #595

Closed
Wenzel opened this issue May 11, 2021 · 8 comments · Fixed by #597
Closed

No such file or directory in the fake filesystem: b'/tmp' #595

Wenzel opened this issue May 11, 2021 · 8 comments · Fixed by #597
Labels

Comments

@Wenzel
Copy link

Wenzel commented May 11, 2021

Describe the bug

pyfakefs complains that /tmp directory does not exists, despite the docs stating that "This means that any newly created fake file system will always have either a directory named /tmp when running on Linux or Unix systems"

How To Reproduce
Please have a look at this small repo:
https://github.com/Wenzel/bug_pyfakefs

smply run python -m pytest -v to reproduce the bug:

venv/lib/python3.8/site-packages/pyfakefs/fake_filesystem.py:4624: in open
    return fake_open(file, mode, buffering, encoding, errors,
venv/lib/python3.8/site-packages/pyfakefs/fake_filesystem.py:5202: in __call__
    return self.call(*args, **kwargs)
venv/lib/python3.8/site-packages/pyfakefs/fake_filesystem.py:5263: in call
    file_object = self._init_file_object(file_object,
venv/lib/python3.8/site-packages/pyfakefs/fake_filesystem.py:5330: in _init_file_object
    file_object = self.filesystem.create_file_internally(
venv/lib/python3.8/site-packages/pyfakefs/fake_filesystem.py:2608: in create_file_internally
    self.raise_os_error(errno.ENOENT, parent_directory)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pyfakefs.fake_filesystem.FakeFilesystem object at 0x7f94c2304ac0>, errno = 2, filename = b'/tmp', winerror = None

    def raise_os_error(self, errno, filename=None, winerror=None):
        """Raises OSError.
        The error message is constructed from the given error code and shall
        start with the error string issued in the real system.
        Note: this is not true under Windows if winerror is given - in this
        case a localized message specific to winerror will be shown in the
        real file system.

        Args:
            errno: A numeric error code from the C variable errno.
            filename: The name of the affected file, if any.
            winerror: Windows only - the specific Windows error code.
        """
        message = self._error_message(errno)
        if (winerror is not None and sys.platform == 'win32' and
                self.is_windows_fs):
            raise OSError(errno, message, filename, winerror)
>       raise OSError(errno, message, filename)
E       FileNotFoundError: [Errno 2] No such file or directory in the fake filesystem: b'/tmp'

venv/lib/python3.8/site-packages/pyfakefs/fake_filesystem.py:1010: FileNotFoundError
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Going up in the stacktrace:
Capture d’écran de 2021-05-11 19-50-46

We see here that an error is raised because /tmp is not found.

Your environment
Please run the following and paste the output.

Linux-5.4.0-73-generic-x86_64-with-glibc2.29
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0]
pyfakefs 4.4.0

Thanks for your help !

@mrbean-bremen
Copy link
Member

mrbean-bremen commented May 11, 2021

Thanks for the report!
Out of the box I have no idea. Just to be sure - can you please check what import tempfile; print(tempfile.gettempdir()) returns on your system? This is the path that shall be created in the fake filesystem at startup.

@Wenzel
Copy link
Author

Wenzel commented May 11, 2021

I've just updated the test to print the tempdir returned by gettempdir() and check if it exists:
https://github.com/Wenzel/bug_pyfakefs/blob/master/tests/test_01.py#L10

The result is that it exists before and after the yield:

tests/test_01.py::test_create_container_ok fixture: tmp dir before yield: /tmp - exists:  True
PASSEDfixture: tmp dir after yield: /tmp - exists:  True

tests/test_01.py::test_create_container_ok ERROR

So I don't understand what's going on here

@Wenzel
Copy link
Author

Wenzel commented May 11, 2021

Okay, I've just found something:

When you pass the filepath as str, it works, but when you pass it as bytes, it fails:
Capture d’écran de 2021-05-11 21-02-05

I hopes this can shed some lights

@Wenzel
Copy link
Author

Wenzel commented May 11, 2021

The problem comes from exists() which cannot handle a bytes filepath:

>>> self.exists(b'/tmp')
False
>>> self.exists('/tmp')
True
>>> 

@mrbean-bremen
Copy link
Member

Ah, thank you - that is helpful! Handling of str vs bytes is something that I have addressed in most places (hopefully), but obviously not all. I will have a look somewhere in the next days, thanks!

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue May 13, 2021
mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue May 13, 2021
- includes exists(), isfile(), islink(), isdir() and ismount()
- fixes pytest-dev#595
- comment docker tests (handled in another issue)
mrbean-bremen added a commit that referenced this issue May 13, 2021
- includes exists(), isfile(), islink(), isdir() and ismount()
- fixes #595
- comment docker tests (handled in another issue)
@mrbean-bremen
Copy link
Member

@Wenzel - shall be fixed now in master, please check!

@Wenzel
Copy link
Author

Wenzel commented May 14, 2021

@mrbean-bremen thank you for the quick fix, I confirmed the issue is solved !
Thank you very much for the support.

@mrbean-bremen
Copy link
Member

Thanks - you are welcome!

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

Successfully merging a pull request may close this issue.

2 participants