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

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

Open
themanifold opened this issue Mar 12, 2020 · 6 comments
Labels
topic: config related to config handling, argument parsing and config file

Comments

@themanifold
Copy link

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.

@themanifold themanifold changed the title parser.addoption has a buggy relative path implementation parser.addoption has a buggy argument parser (relative path-like strings) Mar 12, 2020
@RonnyPfannschmidt
Copy link
Member

at first glance this looks like this is not a initial conftest, i think pytest needs to warn more about plugin registration with the addoption hook when its already too late for that

if you move it one folder up it should work

@Zac-HD Zac-HD added the topic: config related to config handling, argument parsing and config file label Mar 12, 2020
@themanifold
Copy link
Author

@RonnyPfannschmidt Sorry, I'm not super experienced with pytest (or confest). I'm not sure what you mean by your message.

I don't understand why pytest is trying to register a plugin when I use the addoption hook to create a unrelated flag.

When you say move "it" up one folder, do you mean confest? I can't really do that in my example because the file I'm referring to is not part of the test project - conftest belongs in the test folder IMO.

@RonnyPfannschmidt
Copy link
Member

i mean moving the conftest up one folder, pytest no longer scans all of the conftests in all of the folders because people did horrid stuff, sorry about that one

@themanifold
Copy link
Author

OK, I understand your work around, but why does putting an equal sign there make it work?

@RonnyPfannschmidt
Copy link
Member

@themanifold with the =, pytest doesnt consider it as a existing path contributiong to finding the rootdir

this is a issue with finding the correct rootdir as only conftests in it are considered as initial conftests

if you cringe from the explanation thats natural, i also cringed when writing it (the horrors of bugfix collusion)

@adrian-arg
Copy link

adrian-arg commented Jan 10, 2021

I'm getting the same behavior
I'm building a little framework for a demo and I have the webdrivers in a folder called webdrivers.

If I use something like --driver_path webdrivers/chromedriver, it breaks with the error:

"ERROR: usage: _jb_pytest_runner.py [options] [file_or_dir] [file_or_dir] [...]
_jb_pytest_runner.py: error: unrecognized arguments: --driver_path
inifile: /home/adrian/Projects/myframework/pytest.ini
rootdir: /home/adrian/Projects/myframework"

Using it like this makes it work as stated above
--driver_path=webdrivers/chromedriver

Using anything that doesn't match a folder works as well
--driver_path linux/chromedriver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: config related to config handling, argument parsing and config file
Projects
None yet
Development

No branches or pull requests

4 participants