Skip to content

Instantly share code, notes, and snippets.

@majirosstefan
Created September 19, 2021 10:33
Show Gist options
  • Save majirosstefan/440a2cff8f092fe402b1ac4b414fc4d3 to your computer and use it in GitHub Desktop.
Save majirosstefan/440a2cff8f092fe402b1ac4b414fc4d3 to your computer and use it in GitHub Desktop.
/*
IMPORTANT NOTE: Do not use console.log in script below,
as the all logged values will be returned to calling shell script.
*/
const xml2js = require('xml2js');
const yargs = require('yargs');
const argv = yargs
.command('convertXmlReport', 'Converts XML JUNIT report from cavy to JSON', {
data: {
description: 'JUNIT REPORT in XML to convert',
alias: 'fromXml',
type: 'string',
},
})
.option('xmlreport', {
alias: 'xmlreport',
description: 'XML report in JUNIT spec format',
type: 'string',
})
.option('subject', {
alias: 'subject',
description: 'subject - in email',
type: 'string',
})
.option('appname', {
alias: 'appname',
description: 'app name',
type: 'string',
})
.demand('convertXmlReport')
.demandOption('xmlreport')
.demandOption('appname')
.demandOption('subject')
.help()
.version('0.0.1')
.alias('help', 'h').argv;
// argv._ is an array containing each element not attached to an option(or flag) these elements are referred as commands in yargs.
// if (argv._.includes('data')) + we can access options directly
const xml = argv.xmlreport;
const subject = argv.subject;
const appname = argv.appname;
xml2js.parseString(
xml,
{mergeAttrs: true, explicitArray: false},
(err, result) => {
if (err) {
console.log(err);
process.stdout.write(-1);
}
let mappedResult = result.testsuite;
mappedResult.testcase = mappedResult.testcase.map(c => ({
...c,
passed: c.name.includes('works') && !c.failure ? true : false,
}));
mappedResult.appName = appname;
mappedResult.subject = subject;
let mappedJSON = JSON.stringify(mappedResult, null, 4);
process.stdout.write(mappedJSON);
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment