Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Add phorge-json format.

## 3.3.2 (2024-05-26)
* Improve error message as to why parsing the Cobertura report failed.

Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ TOTAL;794;264;66.75%;
```

```shell
$ pycobertura show --format markdown coverage.xml
$ pycobertura show --format markdown coverage.xml
| Filename | Stmts | Miss | Cover | Missing |
|-----------------------------------|---------|--------|---------|--------------------|
| pycobertura/__init__.py | 2 | 0 | 100.00% | |
Expand Down Expand Up @@ -214,6 +214,19 @@ If you run it in GitHub Actions/Apps, the above log generates check annotations.

![Example output of github-annotation formatted pycobertura show command](images/example_github_annotation_show.png)

The following shows how to generate a JSON in the [Phorge/Phabricator coverage
format](https://we.phorge.it/book/phorge/article/arcanist_coverage/#building-coverage-support).

```shell
$ pycobertura show --format phorge-json tests/dummy.source1/coverage.xml
{
"dummy/__init__.py": "",
"dummy/dummy.py": "CCNCUU",
"dummy/dummy2.py": "CC",
"dummy/dummy4.py": "UUNUUU"
}
```

### Command `diff`

You can also use the `diff` command to show the difference between two coverage
Expand Down
2 changes: 2 additions & 0 deletions pycobertura/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
MarkdownReporter,
JsonReporter,
YamlReporter,
PhorgeJsonReporter,
HtmlReporterDelta,
TextReporterDelta,
CsvReporterDelta,
Expand All @@ -31,6 +32,7 @@
"json": JsonReporter,
"yaml": YamlReporter,
"github-annotation": GitHubAnnotationReporter,
"phorge-json": PhorgeJsonReporter,
}


Expand Down
22 changes: 22 additions & 0 deletions pycobertura/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,28 @@ def generate(self):
)


class PhorgeJsonReporter(Reporter):
"""
A reporter that outputs a JSON object in the Phorge/Phabricator
coverage format. It is required to have the source files available.
"""

def generate_one(self, filename):
statuses = self.cobertura.line_statuses(filename)
with self.cobertura.filesystem.open(filename) as f:
lines = ["N" for _ in f]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do something like ["N"] * len(f)? On my phone now so I can't test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, as the file handle is not an array at all.

] python -c 'with open("setup.cfg") as f: print(len(f))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: object of type '_io.TextIOWrapper' has no len()

] python -c 'with open("setup.cfg") as f: print(len(["N" for _ in f]))'
50

for lineNum, covered in statuses:
lines[lineNum - 1] = "C" if covered else "U"

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a long overdue PR that will introduce partially covered lines. Is there a character you'd like to use when I merge this PR eventually?

@tusooa tusooa Jul 1, 2024

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per phorge docs, it does not support partially covered lines. A line can only be set to covered, uncovered, not executable, or unreachable.

return "".join(lines)

def generate(self):
filenames = self.cobertura.files(ignore_regex=self.ignore_regex)
res = {}
for filename in filenames:
res[filename] = self.generate_one(filename)
return json.dumps(res, indent=4)


class DeltaReporter:
def __init__(
self,
Expand Down
38 changes: 38 additions & 0 deletions tests/test_reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,41 @@ def test_delta_reporter__single_file_coverage_changed():
"Cover": "+16.67%"
}
}


def test_phorge_json_reporter():
from pycobertura.reporters import PhorgeJsonReporter

cobertura = make_cobertura('tests/dummy.source1/coverage.xml')
report = PhorgeJsonReporter(cobertura)

assert report.generate() == """\
{
"dummy/__init__.py": "",
"dummy/dummy.py": "CCNCUU",

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is N?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is described in https://we.phorge.it/book/phorge/article/arcanist_coverage/#building-coverage-support (I have put this link into README): N means "not executable"; C means "covered" and "U" means "not covered"

"dummy/dummy2.py": "CC",
"dummy/dummy4.py": "UUNUUU"
}"""


def test_phorge_json_reporter__no_source():
"""
This checks that if a file is not found, the PhorgeJsonReporter
always raises an exception. This is because the phorge-json
format only wants coverage information for each line, and this
does not make sense if a source file is not found.
"""
from pycobertura.reporters import PhorgeJsonReporter
import pycobertura.filesystem

cobertura = make_cobertura('tests/cobertura.xml')
report = PhorgeJsonReporter(cobertura)

exception = None

try:
report.generate()
except pycobertura.filesystem.FileSystem.FileNotFound as e:
exception = e

assert exception is not None