pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.98k stars 2.66k forks source link

Space-containing parameters to custom options mess with rootdir #7380

Open tucked opened 4 years ago

tucked commented 4 years ago

If a custom option is defined in a conftest.py that is more than 1 directory below $PWD, that option is not discovered if the parameter to that option contains a space.

attrs (19.3.0)
importlib-metadata (1.6.1)
more-itertools (8.4.0)
packaging (20.4)
pip (9.0.1)
pkg-resources (0.0.0)
pluggy (0.13.1)
py (1.8.2)
pyparsing (2.4.7)
pytest (5.4.3)
setuptools (39.0.1)
six (1.15.0)
wcwidth (0.2.4)
zipp (3.1.0)
$ venv/bin/pytest --version
This is pytest version 5.4.3, imported from /tmp/tmp.iNSbiDWuuE/venv/lib/python3.6/site-packages/pytest/__init__.py
$ tree -a
.
└── path
    └── to
        └── some
            └── tests
                ├── conftest.py
                └── test_fail.py

4 directories, 2 files
$ cat path/to/some/tests/conftest.py
def pytest_addoption(parser):
    parser.addoption(
        "--foo",
        action="store",
        required=True,
        help="ya know, foo!",
    )
    parser.addoption(
        "--bar",
        action="store",
        required=True,
        help="ya know, bar!",
    )
$ cat path/to/some/tests/test_fail.py
import pytest

def test_foo(pytestconfig):
    pass

path/to/some/tests/test_fail.py . [100%]

========================================================== 1 passed in 0.01s ==========================================================

- Great!

$ venv/bin/pytest --foo="what ever" --bar=baz path/to/some/tests/ ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...] pytest: error: unrecognized arguments: --bar=baz path/to/some/tests/ inifile: None rootdir: /tmp/tmp.iNSbiDWuuE

- Note: The only difference is `--foo="what ever"` instead of `--foo=whatever`.

$ mv path/to/some/tests/ . $ venv/bin/pytest --foo="what ever" --bar=baz tests/ ========================================================= test session starts =========================================================platform linux -- Python 3.6.8, pytest-5.4.3, py-1.8.2, pluggy-0.13.1 rootdir: /tmp/tmp.iNSbiDWuuE collected 1 item

tests/test_fail.py . [100%]

========================================================== 1 passed in 0.01s ==========================================================


- wat
tucked commented 4 years ago

The example does not seem to repro without the second custom option (i.e. --bar).

asottile commented 3 years ago

I cannot reproduce using your example

tucked commented 3 years ago

I can still reproduce with pytest 5.4.3 and 6.2.5 (on py36 and py39).

asottile commented 3 years ago

can you share a git repo or zip? what shell are you using?

tucked commented 3 years ago

Try this: https://github.com/tucked/pytest-7380

Shell is Bash 4.4.20(1)-release

asottile commented 3 years ago

seems pytest is detecting the --foo argument as the test collection root instead of treating it as an unknown argument in the initial argument parsing -- this prevents it from finding your deep conftest file (pytest path also skips the conftest)