UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

161 lines (160 loc) 7.23 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NUnitXmlUtil = void 0; const pfs = require("../../../util/misc/promisfied-fs"); const xml_util_1 = require("./xml-util"); const fs = require("fs"); const xmldom_1 = require("@xmldom/xmldom"); class NUnitXmlUtil extends xml_util_1.XmlUtil { mergeXmlResults(pathToArchive) { return __awaiter(this, void 0, void 0, function* () { const tempPath = yield pfs.mkTempDir("appcenter-uitestreports"); let mainXml = null; const self = this; const outputXml = this.getMergeXmlResultsPromise(pathToArchive, tempPath, (fullPath, relativePath) => { const xml = new xmldom_1.DOMParser(xml_util_1.XmlUtil.DOMParserConfig).parseFromString(fs.readFileSync(fullPath, "utf-8"), "text/xml"); let name = "unknown"; const matches = relativePath.match("^(.*)[_-]nunit[_-]report"); if (matches && matches.length > 1) { name = matches[1].replace(/\./gi, "_"); } self.appendToTestNameTransformation(xml, `_${name}`); self.removeIgnoredTransformation(xml); self.removeEmptySuitesTransformation(xml); if (mainXml) { if (this.isNUnit3(xml)) { mainXml = self.combineNUnit3(mainXml, xml); } else { mainXml = self.combineNUnit2(mainXml, xml); } } else { mainXml = xml; } }, (resolve) => { resolve(mainXml); }); return outputXml; }); } getArchiveName() { return "nunit_xml_zip.zip"; } combineNUnit2(xml1, xml2) { this.combineResultsAttribute(xml1, xml2, "total"); this.combineResultsAttribute(xml1, xml2, "errors"); this.combineResultsAttribute(xml1, xml2, "failures"); this.combineResultsAttribute(xml1, xml2, "not-run"); this.combineResultsAttribute(xml1, xml2, "inconclusive"); this.combineResultsAttribute(xml1, xml2, "ignored"); this.combineResultsAttribute(xml1, xml2, "skipped"); this.combineResultsAttribute(xml1, xml2, "invalid"); const testSuitesParent = this.collectAllElements(xml1.documentElement, "test-results")[0]; const testSuites = this.collectChildren(xml2.documentElement, "test-suite"); testSuites.forEach((child) => { testSuitesParent.appendChild(child); }); return xml1; } combineNUnit3(xml1, xml2) { this.combineResultsAttribute(xml1, xml2, "testcasecount"); this.combineResultsAttribute(xml1, xml2, "total"); this.combineResultsAttribute(xml1, xml2, "passed"); this.combineResultsAttribute(xml1, xml2, "failed"); this.combineResultsAttribute(xml1, xml2, "inconclusive"); this.combineResultsAttribute(xml1, xml2, "skipped"); this.combineResultsAttribute(xml1, xml2, "asserts"); const testSuitesParent = this.collectAllElements(xml1.documentElement, "test-run")[0]; const testSuites = this.collectChildren(xml2.documentElement, "test-suite"); testSuites.forEach((child) => { testSuitesParent.appendChild(child); }); return xml1; } appendToTestNameTransformation(xml, text) { const testCases = this.collectAllElements(xml.documentElement, "test-case"); testCases.forEach((testCase) => { const name = testCase.attributes.getNamedItem("name"); if (name) { name.value = `${name.value}${text}`; } }); } removeIgnoredTransformation(xml) { const testResults = this.collectAllElements(xml.documentElement, "test-results"); testResults.forEach((testResult) => { const ignoredAttr = testResult.attributes.getNamedItem("ignored"); if (ignoredAttr) { const notRunAttr = testResult.attributes.getNamedItem("not-run"); if (notRunAttr) { const notRun = Number(notRunAttr.value); const ignored = Number(ignoredAttr.value); notRunAttr.value = String(notRun - ignored); } ignoredAttr.value = "0"; } }); const elements = this.collectAllElements(xml.documentElement, "test-case"); elements.forEach((element) => { const resultAttr = element.attributes.getNamedItem("result"); if ((resultAttr && resultAttr.value === "Ignored") || resultAttr.value === "Skipped") { element.parentNode.removeChild(element); } }); } removeEmptySuitesTransformation(xml) { const elements = this.collectAllElements(xml.documentElement, "test-suite"); elements.forEach((element) => { if (this.countChildren(element) <= 1) { element.parentNode.removeChild(element); } }); } combineResultsAttribute(xml1, xml2, attributeName) { this.addResultsAttribute(xml1, attributeName, this.getResultsAttribute(xml2, attributeName)); } getResultsAttribute(xml, attributeName) { const testResults = this.getResultsNode(xml); if (!testResults) { return 0; } const attr = testResults.attributes.getNamedItem(attributeName); if (attr.value) { return Number(attr.value); } return 0; } addResultsAttribute(xml, attributeName, value) { const currentValue = this.getResultsAttribute(xml, attributeName); const testResults = this.getResultsNode(xml); if (!testResults) { return; } const attr = testResults.attributes.getNamedItem(attributeName); if (attr) { attr.value = String(currentValue + value); } } getResultsNode(xml) { let testResults = this.collectAllElements(xml.documentElement, "test-results"); if (testResults.length === 0) { testResults = this.collectAllElements(xml.documentElement, "test-run"); } return testResults[0]; } isNUnit3(xml) { const testResults = this.collectAllElements(xml.documentElement, "test-results"); return testResults.length === 0; } } exports.NUnitXmlUtil = NUnitXmlUtil;