Skip to content

tests: check that PYTEST_ADDOPTS is not processed twice #478

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

Merged
merged 2 commits into from
Jul 24, 2020

Conversation

blueyed
Copy link
Contributor

@blueyed blueyed commented Oct 18, 2019

Since 953a3f0 options are parsed already, e.g. "-v" from
$PYTEST_ADDOPTS, and therefore $PYTEST_ADDOPTS should be unset for
child processes.

@blueyed
Copy link
Contributor Author

blueyed commented Oct 18, 2019

What about this note?
https://github.com/pytest-dev/pytest/blob/8db965c401dd46ff9438dd114b538197d0c31c43/src/_pytest/config/__init__.py#L668-L671

I.e. with pytest-xdist it becomes part of the invocation now.
And plugins handling PYTEST_ADDOPTS in a special way won't see it anymore with this PR.

@blueyed
Copy link
Contributor Author

blueyed commented Oct 18, 2019

Instead of unsetting it in pytest-xdist, pytest could also skip the env if invocation params are used (https://github.com/pytest-dev/pytest/blob/8db965c401dd46ff9438dd114b538197d0c31c43/src/_pytest/config/__init__.py#L870-L877). While that seems a bit magical, it might be a good/better solution.

Copy link
Member

@nicoddemus nicoddemus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@blueyed
Copy link
Contributor Author

blueyed commented Oct 18, 2019

@nicoddemus
Any input on my remarks?

@nicoddemus
Copy link
Member

I.e. with pytest-xdist it becomes part of the invocation now.
And plugins handling PYTEST_ADDOPTS in a special way won't see it anymore with this PR.

That's fine, the note is about the fact that InvocationParams already contains the options from PYTEST_ADDOPTS. I think your approach is sound. @RonnyPfannschmidt?

Instead of unsetting it in pytest-xdist, pytest could also skip the env if invocation params are used

Not sure what you mean, can you please clarify?

@nicoddemus
Copy link
Member

That's fine, the note is about the fact that InvocationParams already contains the options from PYTEST_ADDOPTS.

Actually I think I'm completely wrong here, InvocationParams is supposed to contain the unaltered command-line options passed to pytest.main()... I'll have to think about this later, now it's way past a reasonable bed time. :/

@blueyed blueyed changed the title Remove $PYTEST_ADDOPTS with newer pytest [RFC/WIP] Remove $PYTEST_ADDOPTS with newer pytest Oct 19, 2019
@RonnyPfannschmidt
Copy link
Member

I like this, but I think we should investigate parameters layouts
there is env and config based addopts,

We need a alternative to pytest. Main that has the control of all of them at the call site

nicoddemus added a commit to nicoddemus/pytest that referenced this pull request Oct 19, 2019
This avoids mutating the original list to reflect on InvocationParams,
which is supposed to be an immutable snapshot of the state of pytest.main()
at the moment of invocation (see pytest-dev/pytest-xdist#478).
nicoddemus added a commit to nicoddemus/pytest that referenced this pull request Oct 19, 2019
This avoids mutating the original list to reflect on InvocationParams,
which is supposed to be an immutable snapshot of the state of pytest.main()
at the moment of invocation (see pytest-dev/pytest-xdist#478).
@nicoddemus
Copy link
Member

nicoddemus commented Oct 19, 2019

OK I figure it out, the problem is that InvocationParams.args is a reference to the original args, and the PYTEST_ADDOPTS processing changes it in-place:

https://github.com/pytest-dev/pytest/blob/2e11ea61088a3d24da96205627c8bc4dcf3a70ff/src/_pytest/config/__init__.py#L867-L874

    def _preparse(self, args, addopts=True):
        if addopts:
            env_addopts = os.environ.get("PYTEST_ADDOPTS", "")
            if len(env_addopts):
                args[:] = (
                    self._validate_args(shlex.split(env_addopts), "via PYTEST_ADDOPTS")
                    + args
                )

That's why PYTEST_ADDOPTS ends up being "processed" twice.

The proper fix is to change InvocationParams.args to always be a copy, or even better, a tuple, to ensure immutability: pytest-dev/pytest#6008. With that patch, your test passes. 👍

@nicoddemus
Copy link
Member

After pytest-dev/pytest#6008 is merged, I think we should still get your test in, as it tightens it.

@RonnyPfannschmidt
Copy link
Member

lovely find and fix

nicoddemus added a commit to nicoddemus/pytest that referenced this pull request Oct 19, 2019
This avoids mutating the original list to reflect on InvocationParams,
which is supposed to be an immutable snapshot of the state of pytest.main()
at the moment of invocation (see pytest-dev/pytest-xdist#478).
nicoddemus added a commit to nicoddemus/pytest that referenced this pull request Oct 19, 2019
This avoids mutating the original list to reflect on InvocationParams,
which is supposed to be an immutable snapshot of the state of pytest.main()
at the moment of invocation (see pytest-dev/pytest-xdist#478).
@blueyed blueyed force-pushed the del-PYTEST_ADDOPTS branch from be1114e to c3b46c4 Compare October 20, 2019 16:05
@blueyed
Copy link
Contributor Author

blueyed commented Oct 20, 2019

Amended to keep the test only - expected to pass on pytestfeatures only for now.

@blueyed blueyed force-pushed the del-PYTEST_ADDOPTS branch from c3b46c4 to 6b13550 Compare October 20, 2019 16:08
@nicoddemus
Copy link
Member

expected to pass on pytestfeatures only for now.

Can we perhaps skip the test if InvocationParms.args is not a tuple? 🤔

@blueyed
Copy link
Contributor Author

blueyed commented Oct 21, 2019

Yeah, but no hurry I guess.

@blueyed blueyed changed the title [RFC/WIP] Remove $PYTEST_ADDOPTS with newer pytest tests: check that PYTEST_ADDOPTS is not processed twice Feb 22, 2020
@blueyed blueyed force-pushed the del-PYTEST_ADDOPTS branch from 0bb848f to dbaa7a5 Compare April 2, 2020 23:17
@nicoddemus nicoddemus force-pushed the del-PYTEST_ADDOPTS branch from dbaa7a5 to af31de6 Compare July 24, 2020 19:06
@nicoddemus nicoddemus merged commit 467ac33 into pytest-dev:master Jul 24, 2020
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

Successfully merging this pull request may close these issues.

3 participants