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

INTERNALERROR /path/to/file is not in the subpath of '\\/path/to/file' #814

Closed
jamesbraza opened this issue Apr 18, 2023 · 21 comments · Fixed by #818 or #828
Closed

INTERNALERROR /path/to/file is not in the subpath of '\\/path/to/file' #814

jamesbraza opened this issue Apr 18, 2023 · 21 comments · Fixed by #818 or #828
Labels

Comments

@jamesbraza
Copy link
Contributor

Describe the bug

In unit testing, I need to emulate a Windows OS. My base machine is macOS.

When using pyfakefs.fake_filesystem.OSType.WINDOWS + tempfile + --log-cli-level=DEBUG, I get the below internal error.

How To Reproduce

import tempfile

import pyfakefs.fake_filesystem


def test_foo(fs) -> None:
    fs.os = pyfakefs.fake_filesystem.OSType.WINDOWS

    with tempfile.TemporaryDirectory():
        pass

Outputs the following:

> pytest test/repo/test/test_play.py -s --log-cli-level=DEBUG
=============================================================================================================== test session starts ===============================================================================================================
platform darwin -- Python 3.10.9, pytest-7.2.2, pluggy-1.0.0
rootdir: /Users/user/code/repo/test, configfile: pyproject.toml
plugins: pyfakefs-5.2.2, mock-3.10.0, subtests-0.10.0
collected 1 item

test/repo/test/test_play.py::test_foo
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 270, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 324, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 349, in pytest_runtestloop
INTERNALERROR>     item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/runner.py", line 112, in pytest_runtest_protocol
INTERNALERROR>     runtestprotocol(item, nextitem=nextitem)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/runner.py", line 131, in runtestprotocol
INTERNALERROR>     reports.append(call_and_report(item, "call", log))
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/runner.py", line 224, in call_and_report
INTERNALERROR>     hook.pytest_runtest_logreport(report=report)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 551, in pytest_runtest_logreport
INTERNALERROR>     line = self._locationline(rep.nodeid, *rep.location)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 878, in _locationline
INTERNALERROR>     res = mkrel(nodeid)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 868, in mkrel
INTERNALERROR>     line = self.config.cwd_relative_nodeid(nodeid)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1104, in cwd_relative_nodeid
INTERNALERROR>     nodeid = bestrelpath(self.invocation_params.dir, fullpath)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/pathlib.py", line 710, in bestrelpath
INTERNALERROR>     reldirectory = directory.relative_to(base)
INTERNALERROR>   File "/Users/user/.pyenv/versions/3.10.9/lib/python3.10/pathlib.py", line 818, in relative_to
INTERNALERROR>     raise ValueError("{!r} is not in the subpath of {!r}"
INTERNALERROR> ValueError: '/Users/user/code/repo' is not in the subpath of '\\/Users/user/code/repo' OR one path is relative and the other is absolute.
Traceback (most recent call last):
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1618, in get_object_from_normpath
    target = target.get_entry(component)  # type: ignore
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_file.py", line 542, in get_entry
    return self.entries[to_string(pathname_name)]
KeyError: 'Users'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/user/code/repo/venv/bin/pytest", line 8, in <module>
    sys.exit(console_main())
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 190, in console_main
    code = main()
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 167, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 317, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 302, in wrap_session
    os.chdir(session.startpath)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_os.py", line 1354, in wrapped
    return f(*args, **kwargs)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_os.py", line 421, in chdir
    self.filesystem.confirmdir(path)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 2810, in confirmdir
    self.resolve(target_directory, check_owner=check_owner),
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1692, in resolve
    return self.get_object_from_normpath(
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1627, in get_object_from_normpath
    self.raise_os_error(errno.ENOENT, path)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 418, in raise_os_error
    raise OSError(err_no, message, filename)
FileNotFoundError: [Errno 2] No such file or directory in the fake filesystem: 'C:\\Users\\user\\code\\repo'

Your environment

I have pyfakefs==5.2.2. Note the current version printing from the issue template is broken.

> python -c "import platform; print(platform.platform())"
macOS-12.6-arm64-arm-64bit
> python -c "import sys; print('Python', sys.version)"
Python 3.10.9 (main, Jan  6 2023, 11:19:20) [Clang 14.0.0 (clang-1400.0.29.102)]
> python -c "from pyfakefs.fake_filesystem import __version__; print('pyfakefs', __version__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: cannot import name '__version__' from 'pyfakefs.fake_filesystem' (/Users/user/proj/proj_venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py)
> python -c "import pytest; print('pytest', pytest.__version__)"
pytest 7.2.2
@mrbean-bremen
Copy link
Member

Thanks for the report!
The cross-os functionality of pyfakefs is not perfect, and mostly only used in our own tests, so there are certainly issues.
In this case it has probably to do with the different location of temp directories, though I have to have a closer look later.

As for the broken issue template - I actually noticed this in the last bug report, but forgot to check it out. Thanks for the reminder!

@jamesbraza
Copy link
Contributor Author

Yeah thank you! Sounds good.

I also came up with a simpler repro today, simply setting the fs.os to Windows:

import pyfakefs.fake_filesystem


def test_foo(fs) -> None:
    fs.os = pyfakefs.fake_filesystem.OSType.WINDOWS

Running like this yields the error:

> pytest test/repo/test/test_play.py -s --log-cli-level=DEBUG
=============================================================================================================== test session starts ===============================================================================================================
platform darwin -- Python 3.10.9, pytest-7.2.2, pluggy-1.0.0
rootdir: /Users/user/code/repo/test, configfile: pyproject.toml
plugins: pyfakefs-5.2.2, mock-3.10.0, subtests-0.10.0
collected 1 item

test/repo/test/test_play.py::test_foo
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 270, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 324, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 349, in pytest_runtestloop
INTERNALERROR>     item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/runner.py", line 112, in pytest_runtest_protocol
INTERNALERROR>     runtestprotocol(item, nextitem=nextitem)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/runner.py", line 131, in runtestprotocol
INTERNALERROR>     reports.append(call_and_report(item, "call", log))
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/runner.py", line 224, in call_and_report
INTERNALERROR>     hook.pytest_runtest_logreport(report=report)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 551, in pytest_runtest_logreport
INTERNALERROR>     line = self._locationline(rep.nodeid, *rep.location)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 878, in _locationline
INTERNALERROR>     res = mkrel(nodeid)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 868, in mkrel
INTERNALERROR>     line = self.config.cwd_relative_nodeid(nodeid)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1104, in cwd_relative_nodeid
INTERNALERROR>     nodeid = bestrelpath(self.invocation_params.dir, fullpath)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/pathlib.py", line 710, in bestrelpath
INTERNALERROR>     reldirectory = directory.relative_to(base)
INTERNALERROR>   File "/Users/user/.pyenv/versions/3.10.9/lib/python3.10/pathlib.py", line 818, in relative_to
INTERNALERROR>     raise ValueError("{!r} is not in the subpath of {!r}"
INTERNALERROR> ValueError: '/Users/user/code/repo' is not in the subpath of '\\/Users/user/code/repo' OR one path is relative and the other is absolute.
Traceback (most recent call last):
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1618, in get_object_from_normpath
    target = target.get_entry(component)  # type: ignore
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_file.py", line 542, in get_entry
    return self.entries[to_string(pathname_name)]
KeyError: 'Users'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/user/code/repo/venv/bin/pytest", line 8, in <module>
    sys.exit(console_main())
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 190, in console_main
    code = main()
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 167, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 317, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 302, in wrap_session
    os.chdir(session.startpath)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_os.py", line 1354, in wrapped
    return f(*args, **kwargs)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_os.py", line 421, in chdir
    self.filesystem.confirmdir(path)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 2810, in confirmdir
    self.resolve(target_directory, check_owner=check_owner),
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1692, in resolve
    return self.get_object_from_normpath(
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1627, in get_object_from_normpath
    self.raise_os_error(errno.ENOENT, path)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 418, in raise_os_error
    raise OSError(err_no, message, filename)
FileNotFoundError: [Errno 2] No such file or directory in the fake filesystem: 'C:\\Users\\user\\code\\repo'

@mrbean-bremen
Copy link
Member

Ok, I think I know what the problem is - I think this is actually a regression.
On resetting the file system on changing the OS the temp is not recreated, which seem to cause the problem. I will see if I can think ths in the next days.

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Apr 22, 2023
- the temp dir is now created during filesystem
  initialization instead of at patcher setup
- fixes pytest-dev#814
mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Apr 22, 2023
- the temp dir is now created during filesystem
  initialization instead of at patcher setup
- fixes pytest-dev#814
@davidlbaird
Copy link
Collaborator

If this is a regression, what was the previous code that was creating a temporary directory and adding a symlink to the fake filesystem, as is suggested for the pull request?

@mrbean-bremen
Copy link
Member

The code to create the temp directory previously was in the patcher. The regression happened because I added a filesystem reset in the os setters, and this removed the temp directory.

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue Apr 23, 2023
- the temp dir is now created during filesystem
  initialization instead of at patcher setup if configured
- this is only done by default if created from the Patcher
- fixes pytest-dev#814
davidlbaird pushed a commit that referenced this issue Apr 23, 2023
* Re-create the temp directory on resetting the filesystem

- the temp dir is now created during filesystem
  initialization instead of at patcher setup if configured
- this is only done by default if created from the Patcher
- fixes #814

* Call reset() from __init__

- change from review
@jamesbraza
Copy link
Contributor Author

I am just giving this a whirl right now! I don't know that #818 actually fixed the issue though. I am still getting the same Exception. I first cloned the current main branch (right after #818).

> pip install ~/code/pyfakefs
Looking in indexes: https://pypi.org/simple
Processing /Users/user/code/pyfakefs
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: pyfakefs
  Building wheel for pyfakefs (pyproject.toml) ... done
  Created wheel for pyfakefs: filename=pyfakefs-5.3.dev0-py3-none-any.whl size=205504 sha256=blah
  Stored in directory: /private/var/folders/78/blah/T/pip-ephem-wheel-cache-or80khnp/wheels/17/ef/87/blah
Successfully built pyfakefs
Installing collected packages: pyfakefs
Successfully installed pyfakefs-5.3.dev0

With this file:

import pyfakefs.fake_filesystem


def test_foo(fs) -> None:
    fs.os = pyfakefs.fake_filesystem.OSType.WINDOWS

Running like so:

> pytest test/repo/test/test_play.py -s --log-cli-level=DEBUG
=============================================================================================================== test session starts ===============================================================================================================
platform darwin -- Python 3.10.9, pytest-7.3.1, pluggy-1.0.0
rootdir: /Users/user/code/repo/test
configfile: pyproject.toml
plugins: pyfakefs-5.3.dev0, mock-3.10.0, subtests-0.10.0
collected 1 item

test/repo/test/test_play.py::test_foo
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 269, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 323, in _main
INTERNALERROR>     config.hook.pytest_runtestloop(session=session)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 348, in pytest_runtestloop
INTERNALERROR>     item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/runner.py", line 114, in pytest_runtest_protocol
INTERNALERROR>     runtestprotocol(item, nextitem=nextitem)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/runner.py", line 133, in runtestprotocol
INTERNALERROR>     reports.append(call_and_report(item, "call", log))
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/runner.py", line 226, in call_and_report
INTERNALERROR>     hook.pytest_runtest_logreport(report=report)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
INTERNALERROR>     return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 557, in pytest_runtest_logreport
INTERNALERROR>     line = self._locationline(rep.nodeid, *rep.location)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 882, in _locationline
INTERNALERROR>     res = mkrel(nodeid)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 872, in mkrel
INTERNALERROR>     line = self.config.cwd_relative_nodeid(nodeid)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1110, in cwd_relative_nodeid
INTERNALERROR>     nodeid = bestrelpath(self.invocation_params.dir, fullpath)
INTERNALERROR>   File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/pathlib.py", line 741, in bestrelpath
INTERNALERROR>     reldirectory = directory.relative_to(base)
INTERNALERROR>   File "/Users/user/.pyenv/versions/3.10.9/lib/python3.10/pathlib.py", line 818, in relative_to
INTERNALERROR>     raise ValueError("{!r} is not in the subpath of {!r}"
INTERNALERROR> ValueError: '/Users/user/code/repo' is not in the subpath of '\\/Users/user/code/repo' OR one path is relative and the other is absolute.
Traceback (most recent call last):
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1628, in get_object_from_normpath
    target = target.get_entry(component)  # type: ignore
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_file.py", line 542, in get_entry
    return self.entries[to_string(pathname_name)]
KeyError: 'Users'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/user/code/repo/venv/bin/pytest", line 8, in <module>
    sys.exit(console_main())
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 189, in console_main
    code = main()
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 166, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 316, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/_pytest/main.py", line 301, in wrap_session
    os.chdir(session.startpath)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_os.py", line 1354, in wrapped
    return f(*args, **kwargs)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_os.py", line 421, in chdir
    self.filesystem.confirmdir(path)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 2820, in confirmdir
    self.resolve(target_directory, check_owner=check_owner),
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1702, in resolve
    return self.get_object_from_normpath(
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1637, in get_object_from_normpath
    self.raise_os_error(errno.ENOENT, path)
  File "/Users/user/code/repo/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 428, in raise_os_error
    raise OSError(err_no, message, filename)
FileNotFoundError: [Errno 2] No such file or directory in the fake filesystem: 'C:\\Users\\user\\code\\repo'

Any ideas?

@mrbean-bremen mrbean-bremen reopened this Apr 24, 2023
@mrbean-bremen
Copy link
Member

I just noticed that the pytest tests run only under Linux and Windows, so the added tests are not run under macOS. As I don't have a Mac, I can only rely on the CI tests. I would have thought that Posix systems behave the same in that respect, but I was obviously wrong.

I first cloned the current main branch (right after #818).

You mean after it was merged, right?
Anyway, I will have another look tonight, if I find the time, or tomorrow.

@jamesbraza
Copy link
Contributor Author

Thanks for reopening! I see what you mean per os: [ubuntu-latest, windows-latest].

You mean after it was merged, right?

Yeah, I have https://github.com/pytest-dev/pyfakefs/tree/b79f55616c2b757a4376d6e9b46472713189f344 checked out

@mrbean-bremen
Copy link
Member

Hm, I added the tests in #820, and they all pass. The test in question is just:

def test_switch_to_windows(fs):
    fs.os = OSType.WINDOWS
    assert os.path.exists(tempfile.gettempdir())

which to me seems to be exactly what you are doing.
I will change the Python version from 3.9 to 3.10 to match your test (and it makes sense anyway) and see if that changes anything...

@mrbean-bremen
Copy link
Member

mrbean-bremen commented Apr 24, 2023

It did not, the tests all pass for macOS. Can you please check what may be different in your environment? I really have no idea how to test this without a Mac and without reproducibility in CI....
EDIT: No, they actually failed for all OSes for all pytest versions < 6.2 - but that is most probaby due to the fact that these pytest versions did not support Python 3.10 yet... So this doesn't help either. I will change it back to Python 3.9 to have the tests in the older pytest versions, and will add the latest pytest version to the matrix (just in case...).

@jamesbraza
Copy link
Contributor Author

jamesbraza commented Apr 24, 2023

they actually failed for all OSes for all pytest versions < 6.2

I am using pytest version 7 with Python 3.10, so the 6.2 version is not my issue.


One important comment I can add is the INTERNALERROR does not trigger when outside of a native namespace package, it only triggers when part of a native namespace package. Hence you see the pytest test/repo/test/test_play.py path above:

  • First test: folder in a monorepo
  • repo: native namespace
  • test/test_play.py path within native namespace package

This is the relevant trace in the INTERNALERROR stack:

INTERNALERROR>   File "/Users/user/.pyenv/versions/3.10.9/lib/python3.10/pathlib.py", line 818, in relative_to
INTERNALERROR>     raise ValueError("{!r} is not in the subpath of {!r}"
INTERNALERROR> ValueError: '/Users/user/code/repo' is not in the subpath of '\\/Users/user/code/repo' OR one path is relative and the other is absolute.

It looks like pathlib.relative_to is having some aspect of Windows base (\\) and some bleedthrough of macOS ('/Users/user/...). Perhaps there is some path normalization missing somewhere.

@mrbean-bremen
Copy link
Member

Ok, thank you, that is a good hint - I'm done for today, will see if I can find out more tomorrow.

@mrbean-bremen
Copy link
Member

Yes, I think you are right, and the problem here is indeed related to the mixup of different filesystem paths.
The thing is that the emulation of another OS in pyfakefs is not perfect anyway - there are more problems related to pathlib, and you can't completely rely on the behavior to be the same as on the native OS.
I may have to revisit the related code and think about how to make this better - this may take a bit, though...

@mrbean-bremen
Copy link
Member

@jamesbraza - I'm struggling to reproduce your problem, even with a namespace package, but maybe I'm doing something wrong. Have you by any chance a small reproducible failing example (could also be a repository), so I can be sure to test the correct thing?

@jamesbraza
Copy link
Contributor Author

@mrbean-bremen yeah sure, I can make one by 5/19/2023 (next Friday) 👌

Sorry I didn't have a more complete repro for this creation of this ticket, didn't realize it would be tough to reproduce

@mrbean-bremen
Copy link
Member

Thank you! I may just have made some dumb mistake, but as I don't have a Mac, it is good to have a reliable reproducable test that I can run via CI.

@jamesbraza
Copy link
Contributor Author

Okay @mrbean-bremen I have a working reproduction via macOS. One thing that's important: using the --log-cli-level=DEBUG flag. The error doesn't show up if not using that flag.

Control Group: Working

repo
└── test
    └── repo
        └── test
            └── test_file.py

Where test_file.py:

import pyfakefs.fake_filesystem


def test_foo(fs) -> None:
    fs.os = pyfakefs.fake_filesystem.OSType.WINDOWS

No errors, yay!

(venv) ➜  repo pytest --log-cli-level=DEBUG test
=============================================================================================================== test session starts ===============================================================================================================
platform darwin -- Python 3.10.9, pytest-7.3.1, pluggy-1.0.0
rootdir: /Users/user/code/repo
plugins: pyfakefs-5.2.2
collected 1 item

test/repo/test/test_file.py::test_foo PASSED                                                                                                                                                                                                [100%]

================================================================================================================ 1 passed in 0.02s ================================================================================================================

Exposing the Bug

Now, let's break it 😈 by adding an empty test/setup.py:

repo
└── test
    ├── repo
    │   └── test
    │       └── test_file.py
    └── setup.py
(venv) ➜  repo pytest --log-cli-level=DEBUG test
=============================================================================================================== test session starts ===============================================================================================================
platform darwin -- Python 3.10.9, pytest-7.3.1, pluggy-1.0.0
rootdir: /Users/user/code/repo/test
plugins: pyfakefs-5.2.2
collected 1 item

test/repo/test/test_file.py::test_foo Traceback (most recent call last):
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/main.py", line 269, in wrap_session
    session.exitstatus = doit(config, session) or 0
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/main.py", line 323, in _main
    config.hook.pytest_runtestloop(session=session)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/main.py", line 348, in pytest_runtestloop
    item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/runner.py", line 114, in pytest_runtest_protocol
    runtestprotocol(item, nextitem=nextitem)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/runner.py", line 133, in runtestprotocol
    reports.append(call_and_report(item, "call", log))
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/runner.py", line 226, in call_and_report
    hook.pytest_runtest_logreport(report=report)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 557, in pytest_runtest_logreport
    line = self._locationline(rep.nodeid, *rep.location)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 882, in _locationline
    res = mkrel(nodeid)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/terminal.py", line 872, in mkrel
    line = self.config.cwd_relative_nodeid(nodeid)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1110, in cwd_relative_nodeid
    nodeid = bestrelpath(self.invocation_params.dir, fullpath)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/pathlib.py", line 741, in bestrelpath
    reldirectory = directory.relative_to(base)
  File "/Users/user/.pyenv/versions/3.10.9/lib/python3.10/pathlib.py", line 818, in relative_to
    raise ValueError("{!r} is not in the subpath of {!r}"
ValueError: '/Users/user/code/repo' is not in the subpath of '\\/Users/user/code/repo' OR one path is relative and the other is absolute.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/main.py", line 289, in wrap_session
    config.notify_exception(excinfo, config.option)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1100, in notify_exception
    res = self.hook.pytest_internalerror(excrepr=excrepr, excinfo=excinfo)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/capture.py", line 888, in pytest_internalerror
    self.stop_global_capturing()
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/capture.py", line 756, in stop_global_capturing
    self._global_capturing.stop_capturing()
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/capture.py", line 673, in stop_capturing
    self.out.done()
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/capture.py", line 509, in done
    os.close(self.targetfd_save)
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_os.py", line 1354, in wrapped
    return f(*args, **kwargs)
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_os.py", line 308, in close
    file_handle = self.filesystem.get_open_file(fd)
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 857, in get_open_file
    self.raise_os_error(errno.EBADF, str(file_des))
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 418, in raise_os_error
    raise OSError(err_no, message, filename)
OSError: [Errno 9] Bad file descriptor in the fake filesystem: '5'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1618, in get_object_from_normpath
    target = target.get_entry(component)  # type: ignore
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_file.py", line 542, in get_entry
    return self.entries[to_string(pathname_name)]
KeyError: 'Users'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 166, in main
    ret: Union[ExitCode, int] = config.hook.pytest_cmdline_main(
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 60, in _multicall
    return outcome.get_result()
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/path/to/venv/lib/python3.10/site-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/main.py", line 316, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/main.py", line 301, in wrap_session
    os.chdir(session.startpath)
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_os.py", line 1354, in wrapped
    return f(*args, **kwargs)
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_os.py", line 421, in chdir
    self.filesystem.confirmdir(path)
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 2810, in confirmdir
    self.resolve(target_directory, check_owner=check_owner),
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1692, in resolve
    return self.get_object_from_normpath(
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 1627, in get_object_from_normpath
    self.raise_os_error(errno.ENOENT, path)
  File "/path/to/venv/lib/python3.10/site-packages/pyfakefs/fake_filesystem.py", line 418, in raise_os_error
    raise OSError(err_no, message, filename)
FileNotFoundError: [Errno 2] No such file or directory in the fake filesystem: 'C:\\Users\\user\\code\\repo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/path/to/venv/bin/pytest", line 8, in <module>
    sys.exit(console_main())
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 189, in console_main
    code = main()
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 174, in main
    config._ensure_unconfigure()
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/config/__init__.py", line 1055, in _ensure_unconfigure
    fin()
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/capture.py", line 756, in stop_global_capturing
    self._global_capturing.stop_capturing()
  File "/path/to/venv/lib/python3.10/site-packages/_pytest/capture.py", line 670, in stop_capturing
    raise ValueError("was already stopped")
ValueError: was already stopped

Hope that helps, please let me know if you need anything else! 👌

Just noticed this error: OSError: [Errno 9] Bad file descriptor in the fake filesystem: '5'

@mrbean-bremen
Copy link
Member

Thanks for the effort! I will have a closer look as soon as I find the time - probably at the end of this week.
This also sounds like some pytest-related problem, e.g. something in the pytest code got patched that shouldn't - I will see if there is an easy solution (like adding something to skipped modules).

@mrbean-bremen
Copy link
Member

I had another look, and it seems that this can indeed be prevented by excluding some pytest modules from patching. I'm not completely sure about possible side effects, but I think that these modues shall never be patched. You can test this by modifying your test like this:

import pyfakefs.fake_filesystem
from pyfakefs.fake_filesystem_unittest import Patcher
from _pytest import capture, pathlib

Patcher.SKIPMODULES.add(capture)
Patcher.SKIPMODULES.add(pathlib)


def test_foo(fs) -> None:
    fs.os = pyfakefs.fake_filesystem.OSType.WINDOWS

The additional skip modules would be added to the pytest plugin, but you can test it locally this way.
Please let me know if this fixes the problem for you!

@jamesbraza
Copy link
Contributor Author

That works, thank you! 🥳

Just to share, it seems like we can get away with skipping just pathlib:

  • Skipping neither: doesn't solve the issue
  • Skipping pathlib alone: solves the issue
  • Skipping pathlib and capture: solves the issue
  • Skipping capture alone: doesn't solve the issue

@mrbean-bremen
Copy link
Member

Thanks - I will skip only pathlib in this case! I already had problems with pytest pathlib in some other issue...
I'll make a PR in a moment.

mrbean-bremen added a commit to mrbean-bremen/pyfakefs that referenced this issue May 16, 2023
- fixes some specific tests in namespace packages
- fixes pytest-dev#814
mrbean-bremen added a commit that referenced this issue May 17, 2023
- fixes some specific tests in namespace packages
- fixes #814
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants