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

pathlib.Path with UNC paths throwing exception #890

Closed
d0d0 opened this issue Oct 3, 2023 · 4 comments · Fixed by #891
Closed

pathlib.Path with UNC paths throwing exception #890

d0d0 opened this issue Oct 3, 2023 · 4 comments · Fixed by #891

Comments

@d0d0
Copy link

d0d0 commented Oct 3, 2023

Describe the bug
When creating fake UNC directory using pathlib.Path the Path.mkdir throws FileNotFoundError exception. When using simple windows paths like C:/test/test1 each of following examples work.

How To Reproduce

import os
import unittest
from pathlib import Path

from pyfakefs.fake_filesystem_unittest import TestCase


class BugTestCase(TestCase):
    def setUp(self) -> None:
        self.setUpPyfakefs()

    def test_os_makedirs(self):
        os.makedirs(r'\\test\folder1\folder2', exist_ok=True)
        self.assertTrue(Path(r'\\test\folder1\folder2').exists())

    def test_path_mkdir(self):
        Path(r'\\test\folder1\folder2').mkdir(parents=True, exist_ok=True)
        self.assertTrue(Path(r'\\test\folder1\folder2').exists())

    def test_continuous(self):
        Path(r'\\test').mkdir(parents=True, exist_ok=True)
        Path(r'\\test\folder1').mkdir(parents=True, exist_ok=True)
        Path(r'\\test\folder1\folder2').mkdir(parents=True, exist_ok=True)
        self.assertTrue(Path(r'\\test\folder1\folder2').exists())

    def test_fs_create_dir(self):
        self.fs.create_dir(r'\\test\folder1\folder2')
        self.assertTrue(Path(r'\\test\folder1\folder2').exists())


if __name__ == '__main__':
    unittest.main()

Output

============================= test session starts =============================
collecting ... collected 3 items

test_release.py::BugTestCase::test_fs_create_dir 
test_release.py::BugTestCase::test_os_makedirs 
test_release.py::BugTestCase::test_path_mkdir Clearing the cache


========================= 1 failed, 2 passed in 0.80s =========================
PASSED                  [ 33%]PASSED                    [ 66%]FAILED                     [100%]
test_release.py:15 (BugTestCase.test_path_mkdir)
self = WindowsPath('//test/folder1/folder2'), mode = 511, parents = True
exist_ok = True

    def mkdir(self, mode=0o777, parents=False, exist_ok=False):
        """
        Create a new directory at this given path.
        """
        if self._closed:
            self._raise_closed()
        try:
>           self._accessor.mkdir(self, mode)

C:\Python38-64\lib\pathlib.py:1287: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\venv\lib\site-packages\pyfakefs\fake_pathlib.py:72: in _wrapped
    return strfunc(pathobj.filesystem, str(pathobj), *args, **kwargs)
..\venv\lib\site-packages\pyfakefs\fake_filesystem.py:2636: in makedir
    self.raise_os_error(errno.ENOENT, base_dir)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pyfakefs.fake_filesystem.FakeFilesystem object at 0x0000024A491A8B50>
err_no = 2, filename = '\\\\test\\folder1\\', winerror = None

    def raise_os_error(
        self,
        err_no: int,
        filename: Optional[AnyString] = None,
        winerror: Optional[int] = None,
    ) -> NoReturn:
        """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:
            err_no: 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 = os.strerror(err_no) + " in the fake filesystem"
        if winerror is not None and sys.platform == "win32" and self.is_windows_fs:
            raise OSError(err_no, message, filename, winerror)
>       raise OSError(err_no, message, filename)
E       FileNotFoundError: [Errno 2] No such file or directory in the fake filesystem: '\\\\test\\folder1\\'

..\venv\lib\site-packages\pyfakefs\fake_filesystem.py:428: FileNotFoundError

During handling of the above exception, another exception occurred:

self = <tests.test_release.BugTestCase testMethod=test_path_mkdir>

    def test_path_mkdir(self):
>       Path(r'\\test\folder1\folder2').mkdir(parents=True, exist_ok=True)

test_release.py:17: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
C:\Python38-64\lib\pathlib.py:1291: in mkdir
    self.parent.mkdir(parents=True, exist_ok=True)
C:\Python38-64\lib\pathlib.py:1287: in mkdir
    self._accessor.mkdir(self, mode)
..\venv\lib\site-packages\pyfakefs\fake_pathlib.py:72: in _wrapped
    return strfunc(pathobj.filesystem, str(pathobj), *args, **kwargs)
..\venv\lib\site-packages\pyfakefs\fake_filesystem.py:2636: in makedir
    self.raise_os_error(errno.ENOENT, base_dir)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pyfakefs.fake_filesystem.FakeFilesystem object at 0x0000024A491A8B50>
err_no = 2, filename = '\\\\test\\folder1', winerror = None

    def raise_os_error(
        self,
        err_no: int,
        filename: Optional[AnyString] = None,
        winerror: Optional[int] = None,
    ) -> NoReturn:
        """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:
            err_no: 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 = os.strerror(err_no) + " in the fake filesystem"
        if winerror is not None and sys.platform == "win32" and self.is_windows_fs:
            raise OSError(err_no, message, filename, winerror)
>       raise OSError(err_no, message, filename)
E       FileNotFoundError: [Errno 2] No such file or directory in the fake filesystem: '\\\\test\\folder1'

..\venv\lib\site-packages\pyfakefs\fake_filesystem.py:428: FileNotFoundError

Process finished with exit code 1

Your environment

Windows-10-10.0.19041-SP0
Python 3.8.6 (tags/v3.8.6:db45529, Sep 23 2020, 15:52:53) [MSC v.1927 64 bit (AMD64)]
pyfakefs 5.2.4
pytest 7.4.0
@mrbean-bremen
Copy link
Member

Thanks for the report! I will probably have a look at the weekend (traveling right now).

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Oct 7, 2023
- automount drive or UNC path under Windows if needed
  in FakeFilesystem.makedir() (used by Path.mkdir)
- fixes pytest-dev#890
mrbean-bremen added a commit that referenced this issue Oct 8, 2023
- automount drive or UNC path under Windows if needed
  in FakeFilesystem.makedir() (used by Path.mkdir)
- fixes #890
@mrbean-bremen
Copy link
Member

Shall be fixed in main now - please check!

@d0d0
Copy link
Author

d0d0 commented Oct 9, 2023

Nice, thank you. Run the same example on master and it executed without problems.

@mrbean-bremen
Copy link
Member

Good to know, thanks! I will probably make a new release after I have fixed another issue related to Python 3.12.

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

Successfully merging a pull request may close this issue.

2 participants