Skip to content

Option to use relative file paths in report #963

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

Open
teake opened this issue Mar 24, 2020 · 12 comments
Open

Option to use relative file paths in report #963

teake opened this issue Mar 24, 2020 · 12 comments
Labels
enhancement New feature or request

Comments

@teake
Copy link
Contributor

teake commented Mar 24, 2020

Is your feature request related to a problem? Please describe.
The new relative_files config options toggle whether relative paths are stored in the data files, and also how they're reported in XML reports. However, file paths are still absolute in the CLI report (i.e. the one shown with coverage report).

Describe the solution you'd like
A possibility to have the CLI report respect relative_files and show relative paths, either by default or through a toggle.

Describe alternatives you've considered
Using sed to remove the prefix in reported file paths.

Additional context
Related issue: #948.

@teake teake added the enhancement New feature or request label Mar 24, 2020
@teake
Copy link
Contributor Author

teake commented Mar 24, 2020

Also: thanks for the great package!

@warsaw
Copy link

warsaw commented Jun 4, 2020

I think this issue describes the problem I am having too. I reached for relative_files but it didn't solve my problem. For $reasons, our build system wants to run coverage (through pytest) in a different directory, against the the installed site-packages location of our code inside a venv. While I'm not super happy about that, I can't change it. But this leads to ugly and useless absolute paths in the coverage console report:

---------- coverage: platform darwin, python 3.7.7-final-0 -----------
Name                                                                                                                                                         Stmts   Miss Branch BrPart  Cover   Missing
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/Users/bwarsaw/projects/xx/xxxx-xxxxxxx_trunk/build/xxxx-xxxxxxx/environments/satellites/project/lib/python3.7/site-packages/xxxxxxxx/xxxxxxx/__init__.py        0      0      0      0   100%
/Users/bwarsaw/projects/xx/xxxx-xxxxxxx_trunk/build/xxxx-xxxxxxx/environments/satellites/project/lib/python3.7/site-packages/xxxxxxxx/xxxxxxx/constants.py      19      0      0      0   100%
/Users/bwarsaw/projects/xx/xxxx-xxxxxxx_trunk/build/xxxx-xxxxxxx/environments/satellites/project/lib/python3.7/site-packages/xxxxxxxx/xxxxxxx/exception.py       5      0      0      0   100%
/Users/bwarsaw/projects/xx/xxxx-xxxxxxx_trunk/build/xxxx-xxxxxxx/environments/satellites/project/lib/python3.7/site-packages/xxxxxxxx/xxxxxxx/xxxxxxx.py        67     10     36      7    82%   89->90, 90, 101->102, 102, 115->121, 134->135, 135, 137->161, 141->142, 142, 144->145, 145, 161-162, 168-171
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL                                                                                                                                                           91     10     36      7    85%

I would love to find a way to chop off everything left of and including the site-packages component.

@cnzhujie
Copy link

This problem also happened in the html report

@nedbat nedbat added this to the 5.1 milestone Jul 13, 2020
@nedbat nedbat modified the milestones: 5.next, 6.0 Jun 3, 2021
archlinux-github pushed a commit to archlinux/archlinux-keyring that referenced this issue Nov 1, 2021
pyproject.toml:
Set specific source for `toolscoverage.paths` (as we only provide one
module).
Set `tools.coverage.run.relative_files` to true (although it is still
bug riddled and does not seem to work as advertized in regards to xml
output: nedbat/coveragepy#963,
nedbat/coveragepy#1147).
Set `tool.coverage.xml.output`, so we don't have to provide it on the
commandline.

Makefile:
Do not provide an output file to the coverage xml call, as we do that in
configuration now.
@nedbat
Copy link
Owner

nedbat commented Nov 6, 2021

See also #277.

@nedbat
Copy link
Owner

nedbat commented Nov 6, 2021

Is it enough to use the relative file as stored?

@teake
Copy link
Contributor Author

teake commented Nov 8, 2021

I think so. But where are they stored? I cannot find any relative paths in the data file. I'm running 6.1.1 on Python 3.10.0 via

$ MY_DIR=$(pwd)
$ cd /tmp
$ PYTHONPATH=$MY_DIR coverage run --rcfile=$MY_DIR/.coveragerc --source=$MY_DIR -m pytest $MY_DIR/tests

with relative_files = True in .coveragerc. Am I overlooking something?

@nedbat nedbat added the next label Nov 13, 2021
@nedbat nedbat removed this from the Next milestone Nov 13, 2021
archlinux-github pushed a commit to archlinux/archlinux-keyring that referenced this issue Nov 30, 2021
pyproject.toml:
Set specific source for `toolscoverage.paths` (as we only provide one
module).
Set `tools.coverage.run.relative_files` to true (although it is still
bug riddled and does not seem to work as advertized in regards to xml
output: nedbat/coveragepy#963,
nedbat/coveragepy#1147).
Set `tool.coverage.xml.output`, so we don't have to provide it on the
commandline.

Makefile:
Do not provide an output file to the coverage xml call, as we do that in
configuration now.
archlinux-github pushed a commit to archlinux/archlinux-keyring that referenced this issue Nov 30, 2021
pyproject.toml:
Set specific source for `toolscoverage.paths` (as we only provide one
module).
Set `tools.coverage.run.relative_files` to true (although it is still
bug riddled and does not seem to work as advertized in regards to xml
output: nedbat/coveragepy#963,
nedbat/coveragepy#1147).
Set `tool.coverage.xml.output`, so we don't have to provide it on the
commandline.

Makefile:
Do not provide an output file to the coverage xml call, as we do that in
configuration now.
@ofek
Copy link

ofek commented Jan 2, 2022

Would this account for src-layout structures? I would like src/pkg to become just pkg.

@adam-azarchs
Copy link

As others have already noted, this applies to the html report type as well as the command line report. It also applies to the lcov report, which is important for integrating into bazel (to avoid needing this hack). I do believe from my experimentation that the relative file as stored is good enough there.

@nedbat nedbat removed the next label Dec 26, 2024
@alexeagle
Copy link

I spent some time studying the situation for lcov, as relates to Bazel. In fact coveragepy already tries to make the SF: line in lcov report to be a relative path, calling relative_filename() here
https://github.com/nedbat/coveragepy/blob/master/coverage/lcovreport.py#L180

However they are relative to the working directory at the time the cov.start() initializes, here
https://github.com/nedbat/coveragepy/blob/master/coverage/files.py#L41
and Bazel actions run under a "sandbox" folder as the working directory, which contains symlinks to the real source tree. The canonical filename of the source files is passed through os.path.realpath via this call
https://github.com/nedbat/coveragepy/blob/master/coverage/files.py#L86

So, like issues with many tools under Bazel, it's the symlink handling that's going wrong in producing the desired report, possibly not the same issue as the OP here.

@jab
Copy link

jab commented Feb 9, 2025

possibly not the same issue as the OP here

related: #1674 (the issue I opened last time I ran into something like aspect-build/rules_py#524)

@nedbat
Copy link
Owner

nedbat commented Feb 9, 2025

@alexeagle have you tried using a [paths] configuration to map files from one location to another? https://coverage.readthedocs.io/en/7.6.11/config.html#paths

@alexeagle
Copy link

Thanks @nedbat - no I haven't tried that. I'm not sure when/if I'll get time but it looks promising.

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

No branches or pull requests

8 participants