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

Exception importing conftest if the repo is on a Windows mapped network drive for pytest>=5.1.0 #5825

Open
paul-ko opened this issue Sep 6, 2019 · 0 comments
Labels
platform: windows windows platform-specific problem type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously

Comments

@paul-ko
Copy link

paul-ko commented Sep 6, 2019

Basic description

I've ran into an issue with executing tests using relative paths on pytest versions after 5.0.1: there is an ImportMismatchError when pytest attempts to import conftest.py. This occurs only if the codebase is on a Windows mapped network drive.

I haven't checked on MacOS or Linux, but I have a feeling this doesn't affect them because it would have been reported earlier. (I searched open issues here and didn't find anything that this seems to duplicate - apologies if I overlooked something.)

This appears to be distinct from the issue reported in #5819, both by dint of the versions involved and the nature of the exceptions.

One thing I observe comparing the output from 5.1.2 (not working) and 5.0.1 (working) is the root dir:

5.1.2: rootdir: \\giant\src\git\conftest-repro
5.0.1: rootdir: K:\p\git\conftest-repro

pip list output

$ pip list
Package            Version
------------------ -------
atomicwrites       1.3.0
attrs              19.1.0
colorama           0.4.1
importlib-metadata 0.20
more-itertools     7.2.0
packaging          19.1
pip                19.1.1
pluggy             0.12.0
py                 1.8.0
pyparsing          2.4.2
pytest             5.1.2
setuptools         41.0.1
six                1.12.0
wcwidth            0.1.7
wheel              0.33.4
zipp               0.6.0
WARNING: You are using pip version 19.1.1, however version 19.2.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

Platform info

platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0

OS version: Windows Server 2012R2

I've also reproduced this issue on 5.1.0. However, it fails to reproduce on 4.6.2 and 5.0.1.

Detailed description and repro info

I've created the repo https://github.com/paul-ko/conftest-repro to reproduce this issue. The structure of its Python code is as follows:

conftest-repro
+ contestrepro
  + test
    - __init__.py
    - conftest.py
    - test_bar.py
    - test_foo.py

To set up for repro, do the following:

  1. Clone the repo to a mapped network drive on Windows.
  2. Create a virtualenv (my practice is to run virtualenv venv from the base directory of the repo)
  3. Activate that virtualenv
  4. Install pytest (using the repo requirements.txt to install the latest, or pytest==x.x.x to install a specific version)

For the sake of this example, let's say you've cloned it to K:\p\git\conftest-repro, which corresponds to the network drive \\giant\src\git\conftest-repro.

To repro, simply run pytest conftestrepro from the base directory of the repository.
You should see an exception like this:

$ pytest conftestrepro
============================= test session starts =============================
platform win32 -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: \\giant\src\git\conftest-repro
collected 0 items / 1 errors

=================================== ERRORS ====================================
________________________ ERROR collecting test session ________________________
venv\lib\site-packages\_pytest\config\__init__.py:440: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('\\\\giant\\src\\git\\conftest-repro\\conftestrepro\\test\\conftest.py')

During handling of the above exception, another exception occurred:
venv\lib\site-packages\_pytest\config\__init__.py:446: in _importconftest
    mod = conftestpath.pyimport()
venv\lib\site-packages\py\_path\local.py:721: in pyimport
    raise self.ImportMismatchError(modname, modfile, self)
E   py._path.local.LocalPath.ImportMismatchError: ('conftestrepro.test.conftest', 'k:\\p\\git\\conftest-repro\\conftestrepro\\test\\conftest.py', local('\\\\giant\\src\\git\\conftest-repro\\conftestrepro\\test\\conftest.py'))

During handling of the above exception, another exception occurred:
venv\lib\site-packages\py\_path\common.py:377: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
venv\lib\site-packages\py\_path\common.py:418: in gen
    dirs = self.optsort([p for p in entries
venv\lib\site-packages\py\_path\common.py:419: in <listcomp>
    if p.check(dir=1) and (rec is None or rec(p))])
venv\lib\site-packages\_pytest\main.py:606: in _recurse
    ihook = self.gethookproxy(dirpath)
venv\lib\site-packages\_pytest\main.py:424: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
venv\lib\site-packages\_pytest\config\__init__.py:420: in _getconftestmodules
    mod = self._importconftest(conftestpath)
venv\lib\site-packages\_pytest\config\__init__.py:454: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: (local('\\\\giant\\src\\git\\conftest-repro\\conftestrepro\\test\\conftest.py'), (<class 'py._path.local.LocalPath.ImportMismatc                  hError'>, ImportMismatchError('conftestrepro.test.conftest', 'k:\\p\\git\\conftest-repro\\conftestrepro\\test\\conftest.py', local('\\\\giant\\src\\git\\conftest-                  repro\\conftestrepro\\test\\conftest.py')), <traceback object at 0x00000019ABE00E88>))
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
============================== 1 error in 0.40s ===============================

You can avoid this issue in a few ways:

  1. Specify an absolute path instead of a relative path. This seems to work whether the absolute path is a UNC path or a path using the mapped drive letter.
  2. Move the repo to a non-network drive, if that's a viable option for you.
  3. Downgrade pytest to a pre-5.1.0 version.

Omitting the path entirely bypasses this issue, but when I do that the custom argument declared in conftest.py is no longer usable.

Output running after downgrading to 5.0.1:

$ pytest conftestrepro
============================= test session starts =============================
platform win32 -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: K:\p\git\conftest-repro
collected 2 items

conftestrepro\test\test_bar.py .                                         [ 50%]
conftestrepro\test\test_foo.py .                                         [100%]

========================== 2 passed in 0.23 seconds ===========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: windows windows platform-specific problem type: bug problem that needs to be addressed type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

No branches or pull requests

2 participants