Skip to content

Commit

Permalink
Fix (and improve) JaCoCo reports loading
Browse files Browse the repository at this point in the history
- Extract coverage from the last line counter (closes #5)
- Improve associated test
- Update dependencies
  • Loading branch information
GaelGirodon committed Nov 30, 2024
1 parent b9ee49d commit 0bf5599
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 69 deletions.
2 changes: 2 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Please create an issue and wait for a feedback
before submitting a pull request.
90 changes: 41 additions & 49 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
"@actions/glob": "^0.5.0"
},
"devDependencies": {
"@eslint/js": "^9.14.0",
"@vercel/ncc": "^0.38.2",
"@eslint/js": "^9.16.0",
"@vercel/ncc": "^0.38.3",
"c8": "^10.1.2",
"eslint": "^9.14.0",
"eslint": "^9.16.0",
"globals": "^15.12.0",
"mocha": "^10.8.2",
"mocha-junit-reporter": "^2.2.1"
Expand Down
12 changes: 5 additions & 7 deletions src/reports/jacoco.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ export async function getReports(root) {
for (const f of files) {
core.info(`Load JaCoCo report '${f}'`);
const contents = await fs.readFile(f, { encoding: 'utf8' });
const missedMatches = contents
.match(/(?<=<counter[^>]+type="LINE"[^>]+missed=")[0-9.]+(?=")/);
const coveredMatches = contents
.match(/(?<=<counter[^>]+type="LINE"[^>]+covered=")[0-9.]+(?=")/);
if (!missedMatches?.length || !coveredMatches?.length) {
const counter = contents.slice(-400)
.match(/<counter[^>]+type="LINE"[^>]+>/g)?.at(-1);
const missed = parseInt(counter?.match(/missed="([0-9.]+)"/)?.[1]);
const covered = parseInt(counter?.match(/covered="([0-9.]+)"/)?.[1]);
if (isNaN(missed) || isNaN(covered)) {
core.info('Report is not a valid JaCoCo report');
continue; // Invalid report file, trying the next one
}
const missed = parseInt(missedMatches.slice(-1)[0]);
const covered = parseInt(coveredMatches.slice(-1)[0]);
const coverage = covered * 100 / (covered + missed);
reports.push({ type: 'coverage', data: { coverage } });
break; // Successfully loaded a report file, can return now
Expand Down
2 changes: 1 addition & 1 deletion src/reports/jacoco.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('reports/jacoco', () => {
const reports = await getReports(join(process.cwd(), 'test/data/jacoco'));
assert.equal(reports.length, 1);
assert.deepEqual(reports, [
{ type: 'coverage', data: { coverage: 65.61056105610561 } }
{ type: 'coverage', data: { coverage: 65.51724137931035 } }
]);
});
});
Expand Down
62 changes: 53 additions & 9 deletions test/data/jacoco/jacoco.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,54 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC
"-//JACOCO//DTD Report 1.1//EN" "report.dtd">
<report name="Test">
<counter type="INSTRUCTION" missed="2170" covered="3853"/>
<counter type="BRANCH" missed="150" covered="150"/>
<counter type="LINE" missed="521" covered="994"/>
<counter type="COMPLEXITY" missed="300" covered="437"/>
<counter type="METHOD" missed="209" covered="378"/>
<counter type="CLASS" missed="19" covered="58"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.1//EN" "report.dtd">
<report name="demo">
<package name="com/example/demo">
<class name="com/example/demo/Application" sourcefilename="Application.java">
<method name="main" desc="([Ljava/lang/String;)V" line="10">
<counter type="INSTRUCTION" missed="9" covered="0"/>
<counter type="LINE" missed="3" covered="0"/>
<counter type="COMPLEXITY" missed="1" covered="0"/>
<counter type="METHOD" missed="1" covered="0"/>
</method>
<method name="&lt;init&gt;" desc="()V" line="14">
<counter type="INSTRUCTION" missed="17" covered="30"/>
<counter type="BRANCH" missed="4" covered="2"/>
<counter type="LINE" missed="4" covered="11"/>
<counter type="COMPLEXITY" missed="3" covered="1"/>
<counter type="METHOD" missed="0" covered="1"/>
</method>
<method name="start" desc="()V" line="33">
<counter type="INSTRUCTION" missed="14" covered="22"/>
<counter type="BRANCH" missed="4" covered="2"/>
<counter type="LINE" missed="3" covered="8"/>
<counter type="COMPLEXITY" missed="3" covered="1"/>
<counter type="METHOD" missed="0" covered="1"/>
</method>
<counter type="INSTRUCTION" missed="40" covered="52"/>
<counter type="BRANCH" missed="8" covered="4"/>
<counter type="LINE" missed="10" covered="19"/>
<counter type="COMPLEXITY" missed="7" covered="2"/>
<counter type="METHOD" missed="1" covered="2"/>
<counter type="CLASS" missed="0" covered="1"/>
</class>
<sourcefile name="Application.java">
<counter type="INSTRUCTION" missed="40" covered="52"/>
<counter type="BRANCH" missed="8" covered="4"/>
<counter type="LINE" missed="10" covered="19"/>
<counter type="COMPLEXITY" missed="7" covered="2"/>
<counter type="METHOD" missed="1" covered="2"/>
<counter type="CLASS" missed="0" covered="1"/>
</sourcefile>
<counter type="INSTRUCTION" missed="40" covered="52"/>
<counter type="BRANCH" missed="8" covered="4"/>
<counter type="LINE" missed="10" covered="19"/>
<counter type="COMPLEXITY" missed="7" covered="2"/>
<counter type="METHOD" missed="1" covered="2"/>
<counter type="CLASS" missed="0" covered="1"/>
</package>
<counter type="INSTRUCTION" missed="40" covered="52"/>
<counter type="BRANCH" missed="8" covered="4"/>
<counter type="LINE" missed="10" covered="19"/>
<counter type="COMPLEXITY" missed="7" covered="2"/>
<counter type="METHOD" missed="1" covered="2"/>
<counter type="CLASS" missed="0" covered="1"/>
</report>

0 comments on commit 0bf5599

Please sign in to comment.