coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

Option to use relative file paths in report

Open teake opened this issue 4 years ago • 8 comments

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 avatar Mar 24 '20 15:03 teake

Also: thanks for the great package!

teake avatar Mar 24 '20 15:03 teake

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.

warsaw avatar Jun 04 '20 21:06 warsaw

This problem also happened in the html report

cnzhujie avatar Jul 13 '20 02:07 cnzhujie

See also #277.

nedbat avatar Nov 06 '21 22:11 nedbat

Is it enough to use the relative file as stored?

nedbat avatar Nov 06 '21 22:11 nedbat

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?

teake avatar Nov 08 '21 09:11 teake

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

ofek avatar Jan 02 '22 20:01 ofek

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.

adam-azarchs avatar Aug 31 '22 21:08 adam-azarchs