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

Running pytest with "--pyargs" will result in Items with empty "parent.nodeid" if run from a different root directory #2775

Closed
cboelsen opened this issue Sep 13, 2017 · 1 comment
Labels
type: bug problem that needs to be addressed

Comments

@cboelsen
Copy link

When running pytest with "--pyargs" from a directory that isn't a parent directory of the packages under test, then the Items created will have an empty parent.nodeid (i.e. "::test_function").

This is because the _makeid() method in FSCollector compares the path of the Module to the rootdir:

relpath = self.fspath.relto(self.config.rootdir)
@blueyed
Copy link
Contributor

blueyed commented Feb 10, 2020

I think comparing it / making it relative to the rootdir is what is expected, but it should make it relative always (see #6701 for a followup).

bluetech added a commit to bluetech/pytest that referenced this issue Jul 23, 2023
…n to rootdir and startdir

The first change relates to how the path part of the `node.nodeid`
itself (the part before the `::`).

Previously, if the node path was not below the rootdir, then we'd format
it relative to the initial paths (= paths given in the command line),
specifically the first one which contains the path. This was added in
14b6380 as a fix for an issue with
out-of-rootdir `--pyargs` (pytest-dev#2775) which would result in an empty nodeid
path.

This change broke an important property of the nodeid, that it is always
relative to the rootdir, or said differently, that the nodeid cannot be
relied upon to be the root of all nodeids. This causes various issues
that crop up when such invariants are broken, like ambiguous nodeids,
duplicate nodeids, loss of context when printing nodeids (which
directory are they relative to?), which leads to incorrect printing,
etc.

Instead, change back to always using the rootdir, but now allowing `..`
(parent directory) segments in the nodeid path. This is kind of ugly,
but running tests outside the rootdir is discouraged anyway, and this
makes it clear.

The second change relates to how the nodeid path is formatted to
printing by the terminal plugin. The terminal plugin doesn't print the
nodeid directly, instead it makes it relative to the startpath (the
working directory when pytest is started). The change is to normalize
this path, which can become things `<rootdir>/../<rootdir>/test_it.py`
after the first change, and which normalizes to just `test_it.py`
(without resolving symlinks).

TODO: Fixes issues...
bluetech added a commit to bluetech/pytest that referenced this issue Jul 23, 2023
…n to rootdir and startdir

The first change relates to how the path part of the `node.nodeid`
itself (the part before the `::`).

Previously, if the node path was not below the rootdir, then we'd format
it relative to the initial paths (= paths given in the command line),
specifically the first one which contains the path. This was added in
14b6380 as a fix for an issue with
out-of-rootdir `--pyargs` (pytest-dev#2775) which would result in an empty nodeid
path.

This change broke an important property of the nodeid, that it is always
relative to the rootdir, or said differently, that the nodeid cannot be
relied upon to be the root of all nodeids. This causes various issues
that crop up when such invariants are broken, like ambiguous nodeids,
duplicate nodeids, loss of context when printing nodeids (which
directory are they relative to?), which leads to incorrect printing,
etc.

Instead, change back to always using the rootdir, but now allowing `..`
(parent directory) segments in the nodeid path. This is kind of ugly,
but running tests outside the rootdir is discouraged anyway, and this
makes it clear.

The second change relates to how the nodeid path is formatted to
printing by the terminal plugin. The terminal plugin doesn't print the
nodeid directly, instead it makes it relative to the startpath (the
working directory when pytest is started). The change is to normalize
this path, which can become things `<rootdir>/../<rootdir>/test_it.py`
after the first change, and which normalizes to just `test_it.py`
(without resolving symlinks).

TODO: Fixes issues...
TODO: Changelog
TODO: Issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

3 participants