Skip to content
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

Add Types for JaCoCo XML object #95

Merged
merged 7 commits into from
Aug 31, 2024
Merged
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
3 changes: 3 additions & 0 deletions __tests__/__fixtures__/reports/empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Empty Report"/>
5 changes: 5 additions & 0 deletions __tests__/__fixtures__/reports/group/empty_group.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Empty Group">
<group name="Group1" />
</report>
5 changes: 5 additions & 0 deletions __tests__/__fixtures__/reports/group/group_with_counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only Group with Counter">
<group name="Group1"><counter type="INSTRUCTION" missed="1" covered="2"/></group>
</report>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only Group with empty Package">
<group name="Group1">
<package name="Package1" />
</group>
</report>
9 changes: 9 additions & 0 deletions __tests__/__fixtures__/reports/group/group_with_group.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only Group with Group">
<group name="Group1">
<group name="Group2">
<counter type="INSTRUCTION" missed="1" covered="2"/>
</group>
</group>
</report>
9 changes: 9 additions & 0 deletions __tests__/__fixtures__/reports/group/group_with_package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only Group with Package">
<group name="Group1">
<package name="Package1">
<counter type="INSTRUCTION" missed="1" covered="2"/>
</package>
</group>
</report>
5 changes: 5 additions & 0 deletions __tests__/__fixtures__/reports/only_counter.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only Counter">
<counter type="INSTRUCTION" missed="1" covered="2"/>
</report>
5 changes: 5 additions & 0 deletions __tests__/__fixtures__/reports/only_session.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only Session">
<sessioninfo id="session1" start="1620000000" dump="1620003600"/>
</report>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only Class with Counter">
<package name="com.example.package1">
<class name="com.example.package1.ExampleClass1">
<counter type="INSTRUCTION" missed="1" covered="2"/>
</class>
</package>
</report>
8 changes: 8 additions & 0 deletions __tests__/__fixtures__/reports/package/class/empty_class.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Empty Class">
<package name="com.example.package1">
<class name="com.example.package1.ExampleClass1"/>
<class name="com.example.package1.ExampleClass2" sourcefilename="ExampleClass2.kt"/>
</package>
</report>
14 changes: 14 additions & 0 deletions __tests__/__fixtures__/reports/package/class/only_method.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only Method">
<package name="com.example.package1">
<class name="com.example.package1.ExampleClass1">
<method name="exampleMethod1" desc="()V"/>
<method name="&lt;init&gt;" desc="()V" line="3"/>
<method name="validate" desc="(Ljava/lang/String;)Z" line="8">
<counter covered="1" missed="2" type="INSTRUCTION" />
<counter covered="3" missed="4" type="LINE" />
</method>
</class>
</package>
</report>
5 changes: 5 additions & 0 deletions __tests__/__fixtures__/reports/package/empty_package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Empty Package">
<package name="com.example.package1"/>
</report>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only Package with Counter">
<package name="com.example.package1">
<counter type="INSTRUCTION" missed="1" covered="2"/>
</package>
</report>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Empty SourceFile">
<package name="com.example.package1">
<sourcefile name="ExampleFile1.java"/>
</package>
</report>
13 changes: 13 additions & 0 deletions __tests__/__fixtures__/reports/package/sourcefile/only_line.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only Line">
<package name="com.example.package1">
<sourcefile name="ExampleFile1.java">
<line nr="10"/>
<line nr="18" mi="1"/>
<line nr="18" mi="1" ci="2"/>
<line nr="18" mi="1" ci="2" mb="3"/>
<line nr="18" mi="1" ci="2" mb="3" cb="4"/>
</sourcefile>
</package>
</report>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><!DOCTYPE report PUBLIC "-//JACOCO//DTD Report 1.0//EN"
"report.dtd">
<report name="Only SourceFile with Counter">
<package name="com.example.package1">
<sourcefile name="ExampleFile1.java">
<counter type="INSTRUCTION" missed="1" covered="2"/>
</sourcefile>
</package>
</report>
15 changes: 7 additions & 8 deletions __tests__/process.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fs from 'fs'
import parser from 'xml2js'
import * as process from '../src/process'
import {CHANGED_FILE, PROJECT} from './mocks.test'
import {ChangedFile} from '../src/models/github'
import {Report} from '../src/models/jacoco-types'
import {parseToReport} from '../src/util'

describe('process', function () {
describe('get file coverage', function () {
Expand Down Expand Up @@ -400,14 +401,13 @@ describe('process', function () {
})
})

/* eslint-disable @typescript-eslint/no-explicit-any */
async function getAggregateReport(): Promise<any[]> {
async function getAggregateReport(): Promise<Report[]> {
const reportPath = './__tests__/__fixtures__/aggregate-report.xml'
const report = await getReport(reportPath)
return [report]
}

async function getMultipleReports(): Promise<any[]> {
async function getMultipleReports(): Promise<Report[]> {
const testFolder = './__tests__/__fixtures__/multi_module'
return Promise.all(
fs.readdirSync(testFolder).map(async file => {
Expand All @@ -417,14 +417,13 @@ async function getMultipleReports(): Promise<any[]> {
)
}

async function getSingleReport(): Promise<any[]> {
async function getSingleReport(): Promise<Report[]> {
const reportPath = './__tests__/__fixtures__/report.xml'
const report = await getReport(reportPath)
return [report]
}

async function getReport(path: string): Promise<any[]> {
async function getReport(path: string): Promise<Report> {
const reportXml = await fs.promises.readFile(path, 'utf-8')
const json = await parser.parseStringPromise(reportXml)
return json['report']
return parseToReport(reportXml)
}
13 changes: 6 additions & 7 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as util from '../src/util'
import * as fs from 'fs'
import parser from 'xml2js'
import {Report} from '../src/models/jacoco-types'
import {parseToReport} from '../src/util'

jest.mock('@actions/core')
jest.mock('@actions/github')
Expand Down Expand Up @@ -56,7 +57,7 @@ describe('Util', function () {
describe('getFilesWithCoverage', function () {
it('should return valid output', async function () {
const reports = await getSingleReport()
const packages = reports[0]['package']
const packages = reports[0]['package'] ?? []
const files = util.getFilesWithCoverage(packages)
expect(files).toEqual([
{
Expand Down Expand Up @@ -239,15 +240,13 @@ describe('Util', function () {
})
})

/* eslint-disable @typescript-eslint/no-explicit-any */
async function getSingleReport(): Promise<any[]> {
async function getSingleReport(): Promise<Report[]> {
const reportPath = './__tests__/__fixtures__/report.xml'
const report = await getReport(reportPath)
return [report]
}

async function getReport(path: string): Promise<any[]> {
async function getReport(path: string): Promise<Report> {
const reportXml = await fs.promises.readFile(path, 'utf-8')
const json = await parser.parseStringPromise(reportXml)
return json['report']
return parseToReport(reportXml)
}
Loading
Loading