Skip to content

rootpath is wrongly parsed if a path is passed as a plugin argument #8846

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

Closed
elbehery95 opened this issue Jul 2, 2021 · 1 comment
Closed

Comments

@elbehery95
Copy link
Contributor

elbehery95 commented Jul 2, 2021

The following test exposes the issue

def test_root_dir(pytester, pytestconfig) -> None:
    pytester.makeconftest(
        """
        def pytest_addoption(parser):
            # assuming a user defined plugin that requires output directory
            parser.addoption("--some-dir")"""
    )
    pytester.makepyfile(
        f"""
        def test_pass(pytestconfig):
            assert str(pytestconfig.rootpath) == r"{pytester.path}"
        """
    )
    # not passing this value or specifying it in ini addopts wont affect root dir
    result = pytester.runpytest()
    assert result.ret == 0

    # passing CLI as --<arg>=<val> wont affect root dir as well
    result = pytester.runpytest(f"--some-dir={pytester.path.parent}")
    assert result.ret == 0

    # passing CLI as --<arg> <val> affects root dir 
    result = pytester.runpytest("--some-dir", str(pytester.path.parent))
    assert result.ret == 0   # <-- test will fail here

It is really strange why supplying both --arg=val and --arg val is parsed correctly but the last one affects the root dir

@nicoddemus
Copy link
Member

Hi @elbehery95,

Unfortunately this is a known issue: because plugins can add their own command-line options, pytest uses a feature to parse its options that sets aside "unknown options" to be processed later, and processes the known options at startup.

The problem here is that when first looking at --some-dir somepath, the command-line parser can't know beforehand if --some-dir takes a parameter or is a flag (for example, like --verbose is a flag), so the parser doesn't associate the two arguments, and pytest ends up interpreting somepath as an extra argument passed to it. When using --some-dir=somepath, there's no ambiguity because it is clear to the parser that --some-dir is taking the somepath argument.

Unfortunately we don't have a solution to this atm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants