Skip to content

Commit

Permalink
fix: add ava to junit parser
Browse files Browse the repository at this point in the history
  • Loading branch information
arirubinstein committed Jan 25, 2023
1 parent b2a96d4 commit 9a28827
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions scripts/ava-to-junit.mjs
@@ -0,0 +1,82 @@
#! /usr/bin/env node
import fs from 'fs';
import process from 'node:process';

const resultsBody = fs.readFileSync(process.argv[2] || 'results.txt', 'utf-8');
const packageTestRegex = new RegExp(`> (.+?)@(.+?) test$`, 'gms');
const resultRegex = new RegExp(
`^ ([✔✘-])(( \\[skip\\])?( \\[todo\\])?( \\[expected fail\\])?( \\[fail\\]:)?)(.+?)(\\(([\\d. smd]+)\\))?$`,
'gms',
);
console.log(`<?xml version="1.0" encoding="UTF-8" ?>
<testsuites>`);

function durationToSeconds(duration){
if (duration === null || duration === undefined) {
return 0.01;
}
if (/^[\d.]+s$/.exec(duration)) {
const sec = parseInt(duration.replaceAll('s', ''), 10);
return sec;
}

if (/^\dm [\d.]+s$/.exec(duration)) {
const spl = duration.split(' ');
const min = parseInt(spl[0].replaceAll('m', ''), 10);
const sec = min * 60.0 + parseFloat(spl[1].replaceAll('s', ''), 10);
return sec;
}

if (/^\d+ms$/.exec(duration)) {
const ms = parseFloat(duration.replaceAll('ms', ''), 10);
return ms / 1000.0;
}
return duration;
}


let currentSuite = null;
resultsBody.split(/\n/).forEach(element => {
const suiteName = packageTestRegex.exec(element);
if (suiteName !== null) {
if (currentSuite !== null) {
console.log('</testsuite>');
}
currentSuite = suiteName[1].replaceAll('@', '').replaceAll('/', '.');
console.log(`<testsuite name="${currentSuite}">`);
}
const resultMatch = resultRegex.exec(element);
if (resultMatch !== null) {
const [
_fullMatch,
statusCode,
_annotation,
_todo,
_skip,
_expectedFail,
_failed,
testname,
_fullduration,
duration,
] = resultMatch;
const collapsedName = testname
.trim()
.replaceAll(' › ', '.')
.replaceAll(/[^a-zA-Z0-9]/g, '_');
const fullName = currentSuite.concat('.', collapsedName);

console.log(
` <testcase name="${fullName}"`.concat(
statusCode !== '-' ? ` time="${durationToSeconds(duration)}"` : '',
`>`,
),
);
if (statusCode === '✘') {
console.log(` <failure />`);
} else if (statusCode === '-') {
console.log(` <skipped />`);
}
console.log(` </testcase>`);
}
});
console.log('</testsuite></testsuites>');

0 comments on commit 9a28827

Please sign in to comment.