const fs = require("fs"); const glob = require("glob"); const parseXml = require("@rgrove/parse-xml"); const { promisify } = require("util"); const globpromise = promisify(glob) function doTransform(output="status"){ this.totalTests = 0; this.failedTests = 0; this.slowestCount = 5; this.failedTestDetails = []; this.allTests = []; this.outputText = "" this.getOutput = async()=>{ const files = await globpromise("**/TEST*.xml") for (let index = 0; index < files.length; index++) { var xmlDoc = parseXml.parseXml(fs.readFileSync(files[index]).toString()); var suites = xmlDoc.children.filter((child)=>child.name=="testsuite") suites.forEach((suite) => { this.totalTests += parseInt(suite.attributes.tests); this.failedTests += parseInt(suite.attributes.failures); var tests = suite.children.filter((child)=>child.name=="testcase") tests.forEach((test) => { this.allTests.push(test) test.children.forEach((failure) => { console.log(failure) if (failure.name === "failure") { this.failedTestDetails.push( suite.attributes.name + "/" + test.attributes.name + "\n\n```\n" + failure.text + "\n```" ); } }); }); }); } if (output === "text") { this.outputText = outputText(this); } else if (output === "summary") { this.outputText = outputSummary(this); } else if (output === "slowest") { this.outputText = outputSlowest(this, this.slowestCount); } else { if (failedTests > 0) { console.log("Failing tests detected, so returning a non-zero exit code"); process.exit(1); } else if (totalTests == 0) { console.log("No tests detected, so returning a non-zero exit code"); process.exit(1); } } return this.outputText } } function outputText(transform) { const title = "### Test Failures:"; let body = "" if (transform.failedTestDetails.length > 0) { for (let index = 0; index < transform.failedTestDetails.length; index++) { body += "- " + transform.failedTestDetails[index] + "\n"; } } else if (transform.totalTests > 0) { body = "No failing tests, awesome!"; } else { body = "No tests found."; } return `${title} ${body}` } function outputSummary(transform) { const title = "### Testing Summary:"; let body = ""; body += "- " + transform.totalTests + " Total test(s)\n"; body += "- " + (transform.totalTests - transform.failedTests) + " Successful test(s)\n"; body += "- " + transform.failedTests + " Failed test(s)"; return `${title} ${body}` } function outputSlowest(transform, slowestCount) { transform.allTests.sort((a, b) => parseFloat(b.attributes.time) - parseFloat(a.attributes.time)) let slowestTests = transform.allTests.slice(0, slowestCount); const title = `### ${slowestCount} Slowest Tests`; let body = "|Test name|Test duration(seconds)|\n"; body+="|:----|:----|\n"; slowestTests.forEach(test => { body+= `| ${test.attributes.name} | ${parseFloat(test.attributes.time).toFixed(2)} |\n`; }); return `${title} ${body}` } module.exports = doTransform