pytest-dev / pytest-dev/pytest

parser.addoption has a buggy argument parser (relative path-like strings)

Open
#6,904 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: config
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

If I provide an existing relative path to pytest via parser.addoption then I get the following error message:

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --var-file
  inifile: None
  rootdir: /home/user/workspace

This does not happen to a relative path that is non-existent.

If you add an equal sign (=) instead of a blank space, this error does not occur.

This is slightly confusing to me as I do not see why parser.addoption cares about whether we're passing in a file (I would have thought it was just deal with the string). And if it does care about whether something is a file, why is it OK with a nonexistent file?

A minimal bit of code that will reproduce this, conftest.py

def pytest_addoption(parser):
    parser.addoption(
        "--var-file",
        action="store",
    )

Place this in a folder with an existing relative file above it, e.g.:

foo.txt
test/conftest.py
test/bar.txt

Enter the test folder and execute pytest --var-file ../foo.txt and observe the error message above.

Running pytest --var-file bar.txt will not produce an error message
Running pytest --var-file ../notthere.txt will not produce the error message
Running pytest --var-file=../foo.txt will not produce the error message

Output of pip freeze:

pip freeze
atomicwrites==1.3.0
attrs==19.3.0
importlib-metadata==1.5.0
more-itertools==8.2.0
packaging==20.3
pluggy==0.13.1
py==1.8.1
pyparsing==2.4.6
pytest==5.3.5
six==1.14.0
wcwidth==0.1.8
zipp==3.1.0
lsb_release -a
LSB Version:	n/a
Distributor ID:	ManjaroLinux
Description:	Manjaro Linux
Release:	18.1.5
Codename:	Juhraya
python --version
Python 3.8.1

Note, observed this in pytest 5.0.1 as well.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal conftest.py reproducer and the parser.addoption entry point, then run the three relative-path commands shown in the issue to compare their behavior. Done means an existing relative path passed with a space is accepted like the nonexistent path and equals-sign form, with regression coverage for the reported case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.