-
-
Notifications
You must be signed in to change notification settings - Fork 438
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
Option to use relative file paths in .coverage file #597
Comments
This sounds like something the |
Original comment by Andrei Fokau (Bitbucket: andreif, GitHub: andreif) I am not sure how to use it. I have now
|
Make sure this is right: "The first value must be an actual file path on the machine where the reporting will happen, so that source code can be found." Also, this is used when combining data files. Do you use "coverage combine"? |
Original comment by Andrei Fokau (Bitbucket: andreif, GitHub: andreif) @nedbat No, I don't use combine. I'll try to debug it later. It seems that absolute/relative paths issue can be related also to Cobertura issue since the paths are relative there. If you don't plan to use absolute paths in Cobertura XML, then I guess |
While the specific issue is about It would be helpful for when coverage gets run in a changed cwd with subprocess handling. Currently you have to set an environment variable, e.g. tox.ini: setenv =
coverage: COVERAGE_HOME={toxinidir} coverage.rc: [run]
source =
$COVERAGE_HOME/src
$COVERAGE_HOME/tests Since this should be working in a backward compatible way, I suggest either With the first approach |
@nedbat pytest-dev/pytest-cov#329 should be solved by this feature |
I've got a very similar use-case here as well, where the data file is generated at some point by a CI pipeline, and read & processed down the stream. But because those two steps can run on different workers, the absolute path (which contains the worker unique ID) renders the coverage file unusable. I discovered and played a little with the |
I'm having the same problem, the absolute path was
I think this would probably be fixed if the coverage sources were relative instead of absolute. |
@Wonskcalb @LasseGravesen Would your problem be solved by writing a |
@nedbat I can't get that to do what you suggest. If I'm running on a machine where the path is should my config look something like this?
The
Where I would want the line To fix this with sed, I would run the command: and then I'd end up with a file
|
@LasseGravesen I think you want this in your config file:
The first path should be a real path that exists where the reporting will happen. Does this work for you? |
@nedbat The problem is that the real path is Here's a test repo, that's currently set to report on |
@LasseGravesen Thanks for the example, we are getting closer. When I run your example, I have a .coverage file with /app/app/helpers.py in it. What is the step that fails after that? What are you doing that doesn't work? |
@nedbat The problem is that I don't want a .coverage file with |
What is this other program? .coverage files are not a public interface to the coverage data. I didn't expect anything else to be reading them. Would running "coverage combine" on the "/c/Users/username" system work here to re-map the paths? |
Tools like Sonar are reading the coverage files and others like pytest report. |
This is an issue that we are running into as well. We are creating coverage reports in Docker that of course does not have the same path as the host. So the report has a source that does not correspond to the host and can not be read by PyCharm in our case. |
I would like to see a good argument for why absolute paths are better than relative paths in the first place, other than them being legacy. At least I can't immediately come up with anything that wouldn't be better off using relative paths instead. |
Just to be clear: I am not absolutely refusing to support relative paths. I am trying to understand the details of the problem. I don't know what the implications of relative paths would be throughout coverage.py, and I'm trying to not open new cans of worms at the moment. Has anyone tried modifying the code to see the extent of the change? |
I think the biggest problem is that you might want to use the coverage in other contexts aside from the one it was generated in, this is especially true with various CI tooling and Docker for example. In the case of a Docker development environment, it's not enough to substitute the path generated by coveragepy with the actual absolute path as suggested in #597 (comment), since that will most likely not work on other developer's machines. What would be needed is a way (a flag for example) to generate coverage that maps to the correct source files regardless of where the project is located at, as long as the project structure is the same. This could be achieved for example by looking for paths relative to the configuration file, or optionally allow project root to be defined manually with a CLI argument (which probably also should support relative paths). |
Maybe the most sane way to map the relative paths is to map them relative to the coverage output path, so as long as that file is in the correct place (as defined by the output configuration), it would map to the correct files. |
@nedbat |
I don't think so, but that might just be the fact that I'm not sure how to use that command.
|
OK, I've implemented experimental support for relative filenames. If you can install it from GitHub and give it a try, that would help me know whether to merge it to master.
Let me know if it works for the Docker cases. I'll also be interested to hear if it works for non-Docker cases :) |
This is now available in coverage.py 5.0b2. |
Thank you for the change @nedbat! |
thanks ned, this works great! (my use-case is also docker) for lines and branches, though not for created a separate issue #900 |
Thanks @nedbat , this worked very well for me. |
Apologies for replying to an old thread, but this was the top result when searching for an issue I was having with uploading coverage to Code Climate and the solution was to remove the prefix argument from my |
This should fix the coveralls reporting, see nedbat/coveragepy#597 (comment)
Originally reported by Andrei Fokau (Bitbucket: andreif, GitHub: andreif)
I test my project in a Docker container and then examine covered lines in PyCharm on my host machine. Currently PyCharm can only load XML reports in Cobertura format and does actually have an issue due to expecting absolute file paths there. JetBrains guys are going to fix the issue I believe, but I wonder if we can add an option in coverage.py to produce .coverage file with relative paths so that one could run tests in a Docker container and then produce report on host machine? If .coverage contains relative paths, PyCharm would hopefully be able to load them directly without producing intermediate XML files.
The text was updated successfully, but these errors were encountered: