-
Notifications
You must be signed in to change notification settings - Fork 92
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
Changing cwd in a Linux mocked filesystem in a Windows environment causes confusion #558
Comments
Thanks for the report! Some background: Setting the filesystem OS in the tests is something we haven't paid too much attention, as it seemed not to be used much outside of pyfakefs itself. We use this quite a lot for our own tests, and I know that there are a few shortcomings. Some of the faked code (mainly I will have a closer look at the issue later, maybe there is an easy fix. |
I think that any path that starts with a windows drive should be resolved as an absolute path only as a fallback (or you can force the developer to add a leading backslash to indicate it is indeed an absolute path) since it will cause resolving relative paths as absolute. There may be a better solution, I couldn't think about one now. import os
from pathlib import Path
from pyfakefs.fake_filesystem import OSType
from pyfakefs.fake_filesystem_unittest import Patcher
def bug():
with Patcher() as patcher:
filesystem = patcher.fs
filesystem.os = OSType.LINUX
folder = Path('/test')
file = folder / 'C:/testfile'
file.parent.mkdir(parents=True)
file.touch()
os.chdir(folder)
open(file.relative_to(folder)) # fail - FileNotFoundError: [Errno 2] No such file or directory in the fake filesystem: PosixPath('C:/testfile')
bug() Since it will resolve stacktrace:
|
The combination of real files (added by |
- if a Windows systems emulates a Posix system, it shall handle both the cases of a mapped-in real file, and a native file that has a drive-like name - this handling has been improved no lower chances of false drive detection - see #558
Ok, I made the compromise a bit less problematic, while still retaining the ability to handle real files. It will not solve all cases, but hopefully most. |
Describe the bug
When changing the current working directory while using the patched filesystem that mocks a Linux filesystem, the fake filesystem gets confused when trying to access Windows absolute paths, since absolute paths in Windows are not prefixed with a '/' while in Linux they are. The fake filesystem tries to resolve the path as a relative path in the current working directory and fails.
How To Reproduce
My project tree structure:
My test file:
Your enviroment
Please run the following and paste the output.
Output:
The text was updated successfully, but these errors were encountered: