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

mutation badge support #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ matching and valid report file.

➡️ `{repo}-[{ref}-]lcov-coverage.json`

### Mutation

Write the mutation report to a file matching:

- `**/mutations.xml`
- `**/mutation.json`

This is the default format and location with Cobertura, but most code coverage
tools support this format too, natively or using an additional reporter:

- **Maven**: `mvn org.pitest:pitest-maven:mutationCoverage` → `target/pit-reports/mutations.xml`
- **Stryker with Npm**: `stryker run stryker.conf.json` → `reports/mutation/mutation.json`


The mutation will be extracted from the `status` attribute of the
`<mutation>` tag from xml file or `status` attribute from json file.

➡️ `{repo}-[{ref}-]mutation-mutation.json`

## Notes

Storing badge JSON files on a Gist may seem tedious, but:
Expand Down
3 changes: 2 additions & 1 deletion src/badges/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as core from '@actions/core';
import * as tests from './tests.js';
import * as coverage from './coverage.js';
import * as mutation from './mutation.js';

/**
* Available badge generators
* @type {{ [key: string]: { buildBadge: BadgeGenerator } }}
*/
const generators = { tests, coverage };
const generators = { tests, coverage, mutation };

/**
* Build a badge file from a report.
Expand Down
23 changes: 23 additions & 0 deletions src/badges/mutation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Build a mutation badge.
* @param {MutationReportData} data Mutation report data
* @returns {BadgeContent} Badge content
*/
export function buildBadge(data) {
const content = {};
content.message = `${Math.floor(data.mutation)}%`;
if (data.mutation <= 0) {
content.color = 'red';
} else if (data.mutation < 50) {
content.color = 'orange';
} else if (data.mutation < 80) {
content.color = 'yellow';
} else if (data.mutation < 90) {
content.color = 'yellowgreen';
} else if (data.mutation < 95) {
content.color = 'green';
} else {
content.color = 'brightgreen';
}
return content;
}
22 changes: 22 additions & 0 deletions src/badges/mutation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from 'assert/strict';
import { buildBadge } from './mutation.js';

describe('badges/mutation', () => {
describe('#buildBadge()', () => {
const tests = [
{ data: { mutation: 0 }, expected: { message: '0%', color: 'red' } },
{ data: { mutation: 45.2 }, expected: { message: '45%', color: 'orange' } },
{ data: { mutation: 75.8 }, expected: { message: '75%', color: 'yellow' } },
{ data: { mutation: 85.0 }, expected: { message: '85%', color: 'yellowgreen' } },
{ data: { mutation: 93.6 }, expected: { message: '93%', color: 'green' } },
{ data: { mutation: 98.7 }, expected: { message: '98%', color: 'brightgreen' } },
{ data: { mutation: 100 }, expected: { message: '100%', color: 'brightgreen' } }
];
for (const { data, expected } of tests) {
it(`should return a ${expected.color} "${expected.message}" badge for ${data.mutation}% mutation`, () => {
const actual = buildBadge(data);
assert.deepEqual(actual, expected);
});
}
});
});
3 changes: 2 additions & 1 deletion src/reports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import * as junit from './junit.js';
import * as cobertura from './cobertura.js';
import * as jacoco from './jacoco.js';
import * as lcov from './lcov.js';
import * as mutation from './mutation.js';

/**
* Available report loaders
* @type {{ [key: string]: { getReports: ReportsLoader } }}
*/
const loaders = { go, junit, cobertura, jacoco, lcov };
const loaders = { go, junit, cobertura, jacoco, lcov, mutation };

/**
* Load all available reports in the current workspace.
Expand Down
60 changes: 60 additions & 0 deletions src/reports/mutation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as core from '@actions/core';
import { promises as fs } from 'fs';
import { join } from 'path';
import { globNearest } from '../util/index.js';

/**
* Load mutation reports using Mutation format.
* @param {string} root Root search directory
* @returns Mutation mutation report
*/
export async function getReports(root) {
core.info('Load Mutation mutation report');
const patterns = [
join(root, '**/mutations.xml'),
join(root, '**/mutation.json'),
];
const files = await globNearest(patterns);
/** @type {Omit<MutationReport, 'format'>[]} */
const reports = [];
for (const f of files) {
core.info(`Load Mutation report '${f}'`);
const contents = await fs.readFile(f, { encoding: 'utf8' });

const mutationMatches = contents.match(/(?<=<mutation[ ]+detected=['"].*['"][ ]+status=')/);
const mutationJsonMatches = contents.match(/(Stryker)/i);

if (mutationMatches?.length === 0 && mutationJsonMatches?.length === 0) {
core.info('Report is not a valid Mutation report');
continue;
}

let killedMatches;
let survivedMatches;
let noCoverageMatches;

if (mutationMatches?.length === 1) {
killedMatches = contents.match(/(mutation.*status=['"]KILLED['"])/ig);
survivedMatches = contents.match(/(mutation.*status=['"]SURVIVED['"])/ig);
noCoverageMatches = contents.match(/(mutation.*status=['"]NO_COVERAGE['"])/ig);
}
else if (mutationJsonMatches?.length >= 1) {
killedMatches = contents.match(/(['"]status['"]\:['"]Killed['"])/ig);
survivedMatches = contents.match(/(['"]status['"]\:['"]Survived['"])/ig);
noCoverageMatches = contents.match(/(['"]status['"]\:['"]NoCoverage['"])/ig);
}

const killed = parseInt((killedMatches)?.length ?? '0');
const survived = parseInt((survivedMatches)?.length ?? '0');
const noCoverage = parseInt((noCoverageMatches)?.length ?? '0');

const total = (killed + survived + noCoverage) === 0 ? 1 : (killed + survived + noCoverage)
const mutation = parseFloat(parseFloat(killed * 100 / total).toFixed(2));
reports.push({ type: 'mutation', data: { mutation } });
break;
}

core.info(`Loaded ${reports.length} Mutation report(s)`);

return reports;
}
22 changes: 22 additions & 0 deletions src/reports/mutation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from 'assert/strict';
import { join } from 'path';
import { getReports } from './mutation.js';

describe('reports/mutation', () => {
describe('#getReports()', () => {
it('should return a mutation report from pitest', async () => {
const reports = await getReports(join(process.cwd(), 'test/data/mutation/pitest'));
assert.equal(reports.length, 1);
assert.deepEqual(reports, [
{ type: 'mutation', data: { mutation: 9.58 } }
]);
});
it('should return a mutation report from stryker', async () => {
const reports = await getReports(join(process.cwd(), 'test/data/mutation/stryker'));
assert.equal(reports.length, 1);
assert.deepEqual(reports, [
{ type: 'mutation', data: { mutation: 13.79 } }
]);
});
});
});
2 changes: 2 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
* @typedef {{ type: string, format: string, data: ReportData }} Report A loaded report
* @typedef {{ type: 'tests', data: TestReportData } & Report} TestReport A loaded test report
* @typedef {{ type: 'coverage', data: CoverageReportData } & Report} CoverageReport A loaded coverage report
* @typedef {{ type: 'mutation', data: MutationReportData } & Report} MutationReport A loaded mutation report
* @typedef {{ [key: string]: number }} ReportData Report data
* @typedef {{ tests?: number, passed: number, failed: number, skipped: number }} TestReportData Test report data
* @typedef {{ coverage: number }} CoverageReportData Coverage report data
* @typedef {{ mutation: number }} MutationReportData Mutation report data
* @typedef {(root: string) => Promise<Omit<Report, 'format'>[]>} ReportsLoader A report(s) loader
*/

Expand Down
Loading