From cb9cc1515e5ac9dff2d38df2a60e0f72dc52b027 Mon Sep 17 00:00:00 2001 From: agracio Date: Mon, 18 Nov 2024 23:47:50 +0000 Subject: [PATCH] updating TRX code --- package-lock.json | 4 +- package.json | 2 +- src/converter.js | 2 + src/junit.js | 4 +- src/trx-junit.xslt | 47 ++- src/xslt.js | 6 +- tests/common.js | 9 +- tests/converter.trx.test.js | 6 + tests/data/result/trx-sample-junit.xml | 33 ++ tests/data/result/trx-sample-mochawesome.json | 371 ++++++++++++++++++ tests/data/source/trx-sample.trx | 150 +++++++ 11 files changed, 607 insertions(+), 27 deletions(-) create mode 100644 tests/data/result/trx-sample-junit.xml create mode 100644 tests/data/result/trx-sample-mochawesome.json create mode 100644 tests/data/source/trx-sample.trx diff --git a/package-lock.json b/package-lock.json index a2d3643..c40fb80 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mochawesome-converter", - "version": "1.0.1", + "version": "1.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "mochawesome-converter", - "version": "1.0.1", + "version": "1.0.2", "license": "MIT", "dependencies": { "lodash": "^4.17.21", diff --git a/package.json b/package.json index 1659009..2aab5a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mochawesome-converter", - "version": "1.0.1", + "version": "1.0.2", "description": "Test report files to Mochawesome and JUnit converter", "main": "src/converter.js", "types": "index.d.ts", diff --git a/src/converter.js b/src/converter.js index c7dd222..2ae306f 100644 --- a/src/converter.js +++ b/src/converter.js @@ -18,6 +18,8 @@ async function convert(options){ let config = conf.config(options); + //console.log(`Converting file ${options.testFile} using '${options.testType}' converter.`); + switch (config.testType) { case 'junit': await junit.convert(config, null); diff --git a/src/junit.js b/src/junit.js index 19ece08..8dd16b0 100644 --- a/src/junit.js +++ b/src/junit.js @@ -86,7 +86,7 @@ function parseXml(options, xml){ json = parser.toJson(xml, xmlParserOptions); } catch (e){ - throw `\nCould not read JSON from converted input ${options.testFile}.\n ${e.message}`; + throw `\nCould not parse JSON from converted XML ${options.testFile}.\n ${e.message}`; } if(!json || !json.testsuites || !json.testsuites.length){ @@ -107,7 +107,7 @@ function parseXml(options, xml){ fs.writeFileSync(path.join(options.reportDir, fileName), JSON.stringify(json, null, 2), 'utf8'); } - if(!json.testsuites[0].testsuite || !json.testsuites[0].testsuite.length || json.testsuites[0].testsuite.length ===0){ + if(!json.testsuites[0].testsuite || !json.testsuites[0].testsuite.length || json.testsuites[0].testsuite.length === 0){ console.log('No test suites found, skipping Mochawesome file creation.'); } diff --git a/src/trx-junit.xslt b/src/trx-junit.xslt index e827c38..ba03edc 100644 --- a/src/trx-junit.xslt +++ b/src/trx-junit.xslt @@ -66,7 +66,20 @@ - + + + + + + + + + + + + + + @@ -80,7 +93,7 @@ @@ -94,20 +107,28 @@ - + - + - - - - - - - - - + + + + + + + + + + + + + + + + + diff --git a/src/xslt.js b/src/xslt.js index 0004fe4..f3d6011 100644 --- a/src/xslt.js +++ b/src/xslt.js @@ -21,7 +21,7 @@ async function processXml(options, xmlString){ parsedXml = xmlFormat(xmlString, {forceSelfClosingEmptyTag: true}) } catch (e) { - throw `\nXML parsed from ${options.testFile} is invalid \n${e.message}`; + throw `\nXML parsed from ${options.testFile} is empty or invalid \n${e.message}`; } if(options.junit && options.testType !== 'trx'){ @@ -38,8 +38,8 @@ async function processXml(options, xmlString){ * @param {string} xsltFile */ async function convert(options, xsltFile){ - - let xsltString = fs.readFileSync(path.join(__dirname,xsltFile)).toString(); + //console.log(`Processing ${options.testFile} using XSLT ${xsltFile}`); + let xsltString = fs.readFileSync(path.join(__dirname, xsltFile)).toString(); let xmlString = fs.readFileSync(options.testFile).toString(); const xslt = new xsltProcessor.Xslt(); diff --git a/tests/common.js b/tests/common.js index b2e9417..eea4e1d 100644 --- a/tests/common.js +++ b/tests/common.js @@ -15,19 +15,15 @@ function removeTempDir(){ } } -function getFilename(file){ - return `${path.parse(file).name}-mochawesome.json` -} - /** - * @returns {TestReportConverterOptions} options + * @returns {TestReportConverterOptions} */ function createOptions(file, type){ return { testFile: path.join(__dirname, `data/source/${file}`), testType: type, reportDir: outDir, - reportFilename: getFilename(file), + reportFilename:`${path.parse(file).name}-mochawesome.json`, junit: true, junitReportFilename: `${path.parse(file).name}-junit.xml`, } @@ -66,6 +62,7 @@ function compare(options, reportFilename, compareJunit){ let createdReport = fs.readFileSync(path.join(compareDir, reportFilename ?? options.reportFilename), 'utf8'); let report = fs.readFileSync(path.join(reportDir, reportFilename ?? options.reportFilename), 'utf8'); + expect(createdReport.replaceAll(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/g,'')).toBe(report.replaceAll(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/g,'')); if(compareJunit){ diff --git a/tests/converter.trx.test.js b/tests/converter.trx.test.js index ea26582..c15c87f 100644 --- a/tests/converter.trx.test.js +++ b/tests/converter.trx.test.js @@ -60,4 +60,10 @@ describe("TRX converter tests", () => { common.compare(options, undefined, true); }); + test('convert trx-sample.trx', async() => { + let options = common.createOptions('trx-sample.trx', 'trx') + await converter(options); + common.compare(options, undefined, true); + }); + }); \ No newline at end of file diff --git a/tests/data/result/trx-sample-junit.xml b/tests/data/result/trx-sample-junit.xml new file mode 100644 index 0000000..72f4909 --- /dev/null +++ b/tests/data/result/trx-sample-junit.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + at AccountTest2.AccountTest.MTestFail() in c:\Jenkins_Home\jobs\NUnit_Quick_Learning\workspace\AccountTest2\AccountMTest.cs:line 253 + + + + + at AccountTest2.AccountTest.MTestInconclusive() in c:\Jenkins_Home\jobs\NUnit_Quick_Learning\workspace\AccountTest2\AccountMTest.cs:line 247 + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/data/result/trx-sample-mochawesome.json b/tests/data/result/trx-sample-mochawesome.json new file mode 100644 index 0000000..352b7d6 --- /dev/null +++ b/tests/data/result/trx-sample-mochawesome.json @@ -0,0 +1,371 @@ +{ + "stats": { + "suites": 3, + "tests": 15, + "passes": 12, + "pending": 2, + "failures": 1, + "testsRegistered": 15, + "passPercent": 80, + "pendingPercent": 13.333333333333334, + "other": 0, + "hasOther": false, + "skipped": 0, + "hasSkipped": false, + "duration": 149 + }, + "results": [ + { + "uuid": "a6a4f527-9968-49aa-846e-a2c0beddca33", + "title": "", + "fullFile": "", + "file": "", + "beforeHooks": [], + "afterHooks": [], + "tests": [], + "suites": [ + { + "uuid": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "title": "AccountTest2.AccountTest", + "file": "", + "beforeHooks": [], + "afterHooks": [], + "tests": [ + { + "title": "AccountConstructorTest", + "fullTitle": "AccountTest2.AccountTest", + "duration": 52, + "state": "passed", + "speed": "slow", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "b2265a2c-fdb7-4d84-bd5f-803e3f6ee310", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "AddToBalanceTest", + "fullTitle": "AccountTest2.AccountTest", + "duration": 16, + "state": "passed", + "speed": "slow", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "bfbeb7d2-e763-4ccd-ab98-1c062052a510", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "ApplyInterestTest", + "fullTitle": "AccountTest2.AccountTest", + "duration": 3, + "state": "passed", + "speed": "fast", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "d18aefee-049c-43b9-928e-d03540a6d404", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "ApplyPenaltyTest", + "fullTitle": "AccountTest2.AccountTest", + "duration": 3, + "state": "passed", + "speed": "fast", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "2bac477a-ac0c-46b3-ba19-a0a5d38a8034", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "BalanceTest", + "fullTitle": "AccountTest2.AccountTest", + "duration": 1, + "state": "passed", + "speed": "fast", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "5be5af08-a462-488e-b13d-652dd9906ba5", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "CalculateInterestTest", + "fullTitle": "AccountTest2.AccountTest", + "duration": 2, + "state": "passed", + "speed": "fast", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "23e6efd8-883e-428b-96d5-ec1d66019ac9", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "DepositTest", + "fullTitle": "AccountTest2.AccountTest", + "duration": 3, + "state": "passed", + "speed": "fast", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "8fc3928c-1b12-4642-972c-dc6a5a8e36d2", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "MTestFail", + "fullTitle": "AccountTest2.AccountTest", + "duration": 60, + "state": "failed", + "speed": "slow", + "pass": false, + "fail": true, + "pending": false, + "context": null, + "code": null, + "err": { + "message": "Assert.Fail failed. Always fails", + "estack": "at AccountTest2.AccountTest.MTestFail() in c:\\Jenkins_Home\\jobs\\NUnit_Quick_Learning\\workspace\\AccountTest2\\AccountMTest.cs:line 253", + "diff": null + }, + "uuid": "1d0a141f-d88f-4050-9a33-531f08f56f84", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "MTestInconclusive", + "fullTitle": "AccountTest2.AccountTest", + "duration": 2, + "state": "pending", + "speed": "fast", + "pass": false, + "fail": false, + "pending": true, + "context": "[\"skipped: Assert.Inconclusive failed. Inconclusive result\"]", + "code": null, + "err": {}, + "uuid": "44eef5ae-948d-4981-bb21-03c981bc4ec9", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "MTestNotExecuted", + "fullTitle": "AccountTest2.AccountTest", + "duration": 1, + "state": "pending", + "speed": "fast", + "pass": false, + "fail": false, + "pending": true, + "context": null, + "code": null, + "err": {}, + "uuid": "58fee468-e5d6-41fd-ad3e-b01ee9b5b154", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "MinimumBalanceTest", + "fullTitle": "AccountTest2.AccountTest", + "duration": 1, + "state": "passed", + "speed": "fast", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "c980d3c5-d5a3-4ac0-89f0-9872cb1ead1d", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "TransferFundsTest", + "fullTitle": "AccountTest2.AccountTest", + "duration": 3, + "state": "passed", + "speed": "fast", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "5da3a46b-86bd-488d-955d-933d74e25240", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + }, + { + "title": "WithdrawTest", + "fullTitle": "AccountTest2.AccountTest", + "duration": 2, + "state": "passed", + "speed": "fast", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "0d6f20ac-6ed0-4d70-82b7-8d3becf6bed6", + "parentUUID": "e0f10d7c-1049-48bc-8ab6-a6cb67cd4779", + "isHook": false, + "skipped": false + } + ], + "suites": [], + "passes": [ + "b2265a2c-fdb7-4d84-bd5f-803e3f6ee310", + "bfbeb7d2-e763-4ccd-ab98-1c062052a510", + "d18aefee-049c-43b9-928e-d03540a6d404", + "2bac477a-ac0c-46b3-ba19-a0a5d38a8034", + "5be5af08-a462-488e-b13d-652dd9906ba5", + "23e6efd8-883e-428b-96d5-ec1d66019ac9", + "8fc3928c-1b12-4642-972c-dc6a5a8e36d2", + "c980d3c5-d5a3-4ac0-89f0-9872cb1ead1d", + "5da3a46b-86bd-488d-955d-933d74e25240", + "0d6f20ac-6ed0-4d70-82b7-8d3becf6bed6" + ], + "failures": [ + "1d0a141f-d88f-4050-9a33-531f08f56f84" + ], + "pending": [ + "44eef5ae-948d-4981-bb21-03c981bc4ec9", + "58fee468-e5d6-41fd-ad3e-b01ee9b5b154" + ], + "skipped": [], + "duration": 144, + "root": false, + "rootEmpty": false, + "_timeout": 10000 + }, + { + "uuid": "f92cc173-542f-4ba3-b1ca-f6e87b38c771", + "title": "AccountTest2.InsufficientFundsExceptionTest", + "file": "", + "beforeHooks": [], + "afterHooks": [], + "tests": [ + { + "title": "InsufficientFundsExceptionConstructorTest", + "fullTitle": "AccountTest2.InsufficientFundsExceptionTest", + "duration": 3, + "state": "passed", + "speed": "fast", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "7b3cbd3e-cc85-443a-a462-c79e7ad04e1d", + "parentUUID": "f92cc173-542f-4ba3-b1ca-f6e87b38c771", + "isHook": false, + "skipped": false + } + ], + "suites": [], + "passes": [ + "7b3cbd3e-cc85-443a-a462-c79e7ad04e1d" + ], + "failures": [], + "pending": [], + "skipped": [], + "duration": 3, + "root": false, + "rootEmpty": false, + "_timeout": 10000 + }, + { + "uuid": "d855004e-0a87-4d72-9a15-8cb53b6de97e", + "title": "AccountTest2.OverdrawnExceptionTest", + "file": "", + "beforeHooks": [], + "afterHooks": [], + "tests": [ + { + "title": "OverdrawnExceptionConstructorTest", + "fullTitle": "AccountTest2.OverdrawnExceptionTest", + "duration": 3, + "state": "passed", + "speed": "fast", + "pass": true, + "fail": false, + "pending": false, + "context": null, + "code": null, + "err": {}, + "uuid": "1739c66c-5638-4f35-b581-f4a34c844cd2", + "parentUUID": "d855004e-0a87-4d72-9a15-8cb53b6de97e", + "isHook": false, + "skipped": false + } + ], + "suites": [], + "passes": [ + "1739c66c-5638-4f35-b581-f4a34c844cd2" + ], + "failures": [], + "pending": [], + "skipped": [], + "duration": 3, + "root": false, + "rootEmpty": false, + "_timeout": 10000 + } + ], + "passes": [], + "failures": [], + "pending": [], + "skipped": [], + "duration": 0, + "root": true, + "rootEmpty": true, + "_timeout": 10000 + } + ] +} \ No newline at end of file diff --git a/tests/data/source/trx-sample.trx b/tests/data/source/trx-sample.trx new file mode 100644 index 0000000..84630fd --- /dev/null +++ b/tests/data/source/trx-sample.trx @@ -0,0 +1,150 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Assert.Fail failed. Always fails + at AccountTest2.AccountTest.MTestFail() in c:\Jenkins_Home\jobs\NUnit_Quick_Learning\workspace\AccountTest2\AccountMTest.cs:line 253 + + + + + + + + Assert.Inconclusive failed. Inconclusive result + at AccountTest2.AccountTest.MTestInconclusive() in c:\Jenkins_Home\jobs\NUnit_Quick_Learning\workspace\AccountTest2\AccountMTest.cs:line 247 + + + + + + + + + + + + + + \ No newline at end of file