nedbat / coveragepy

The code coverage tool for Python
https://coverage.readthedocs.io
Apache License 2.0
2.97k stars 429 forks source link

Coverage.py not reporting complexity metrics in cobertura.xml #1296

Open edmondop opened 2 years ago

edmondop commented 2 years ago

For any of the files in my project, the cobertura.xml contains always a 0 for complexity.

I have hard time in understanding if the value is not computed by coverage.py (and probably we would prefer a NA then) or if my code has really such a low complexity :)

nedbat commented 2 years ago

Coverage.py doesn't measure complexity, so the XML file has no information about it.

edmondop commented 2 years ago

Maybe just updating the docs on the reporter? Why reporting zero then?

nedbat commented 2 years ago

Can you do an experiment for me? If you remove the complexity attributes from the XML file, what will your reporting tool do with it? I don't know if the file will be considered valid.

edmondop commented 2 years ago

It won't be considered valid, if you open a coverage.py generated coverage.xml the first line says

<!-- Generated by coverage.py: https://coverage.readthedocs.io -->
<!-- Based on https://raw.githubusercontent.com/cobertura/web/master/htdocs/xml/coverage-04.dtd -->

but if you open that DTD, you see that complexity is required, but is not a number, is a string. So why not N/A?

If you accept contributions, here that would be my proposal https://github.com/nedbat/coveragepy/pull/1297

nedbat commented 2 years ago

Are you using a reporting tool that reads the XML file? What does it do with "N/A"?

edmondop commented 2 years ago

You are right, although the DTD does not dictate anything around the format of that specific field, making it by contract a free form string, tools probably make assumptions that the number can be parsed.

For example, I am parsing that file via irongut/CodeCoverageSummary@v1.0.2 github action which assumes it can be parsed into a double, and if cannot returns a 0.

https://github.com/irongut/CodeCoverageSummary/blob/master/src/CodeCoverageSummary/Program.cs#L170

Maybe we should file an issue to cobertura asking them to convert the DTD into XSD :) In coverage.py we can use a more explicit sentinel value, for example -1 , -99, or a negative large integer? something that obviously is not a valid complexity value

nedbat commented 2 years ago

Wouldn't you say that 0 is obviously not a valid complexity value? :)