Skip to content

Commit

Permalink
updating TRX code
Browse files Browse the repository at this point in the history
  • Loading branch information
agracio committed Nov 18, 2024
1 parent 8eb4f0b commit cb9cc15
Show file tree
Hide file tree
Showing 11 changed files with 607 additions and 27 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/junit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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.');
}

Expand Down
47 changes: 34 additions & 13 deletions src/trx-junit.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,20 @@
<xsl:for-each select="//vs:UnitTest">
<xsl:variable name="currentTestId" select="@id"/>
<xsl:if test="$currentTestId = $testId" >
<xsl:variable name="className" select="vs:TestMethod/@className"/>
<xsl:variable name="className">
<xsl:choose>
<xsl:when test="contains(vs:TestMethod/@className, ',')">
<xsl:value-of select="substring-before(vs:TestMethod/@className, ',')" />
</xsl:when>
<xsl:when test="vs:TestMethod/storage">
<xsl:value-of select="vs:TestMethod/@storage" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="vs:TestMethod/@className" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- <xsl:variable name="className" select="vs:TestMethod/@className"/>-->
<xsl:variable name="name">
<xsl:choose>
<xsl:when test="substring-after($testName, $className)=''">
Expand All @@ -80,7 +93,7 @@
<testcase
classname="{$className}"
name="{$name}"
status="{replace(replace(replace($outcome,'Error','Failed'),'NotExecuted','Skipped'), 'Inconclusive', 'Failed')}"
status="{replace(replace(replace($outcome,'Error','Failed'),'NotExecuted','Skipped'), 'Inconclusive', 'Skipped')}"
time="{$testDuration}"
>
<xsl:if test="contains($outcome, 'Failed')">
Expand All @@ -94,20 +107,28 @@
</error>
</xsl:if>
<xsl:if test="contains($outcome, 'Inconclusive')">
<error message="{$message}">
<skipped message="{$message}">
<xsl:value-of select="$stacktrace" />
</error>
</skipped>
</xsl:if>
<xsl:if test="contains($outcome, 'NotExecuted')">
<xsl:if test="$message != ''">
<skipped message="{$message}"/>
</xsl:if>
<xsl:if test="$stdout != ''">
<skipped message="{$stdout}"/>
</xsl:if>
<xsl:if test="$stdout = '' and $message = ''">
<skipped/>
</xsl:if>
<xsl:choose>
<xsl:when test="$stdout != ''">
<skipped message="{$stdout}"/>
</xsl:when>
<xsl:otherwise>
<skipped message="{$message}"/>
</xsl:otherwise>
</xsl:choose>
<!-- <xsl:if test="$message != ''">-->
<!-- <skipped message="{$message}"/>-->
<!-- </xsl:if>-->
<!-- <xsl:if test="$stdout != ''">-->
<!-- <skipped message="{$stdout}"/>-->
<!-- </xsl:if>-->
<!-- <xsl:if test="$stdout = '' and $message = ''">-->
<!-- <skipped/>-->
<!-- </xsl:if>-->
</xsl:if>
<xsl:if test="$stderr">
<system-err>
Expand Down
6 changes: 3 additions & 3 deletions src/xslt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'){
Expand All @@ -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();
Expand Down
9 changes: 3 additions & 6 deletions tests/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
}
Expand Down Expand Up @@ -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){
Expand Down
6 changes: 6 additions & 0 deletions tests/converter.trx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

});
33 changes: 33 additions & 0 deletions tests/data/result/trx-sample-junit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<testsuites tests="15" failures="1" errors="0" skipped="2" time="0.1488619">
<testsuite name="AccountTest2.AccountTest" tests="13" failures="1" skipped="2" time="0.1439396">
<testcase classname="AccountTest2.AccountTest" name="AccountConstructorTest" status="Passed" time="0.0510768"/>
<testcase classname="AccountTest2.AccountTest" name="AddToBalanceTest" status="Passed" time="0.0151591"/>
<testcase classname="AccountTest2.AccountTest" name="ApplyInterestTest" status="Passed" time="0.0025955"/>
<testcase classname="AccountTest2.AccountTest" name="ApplyPenaltyTest" status="Passed" time="0.0029271"/>
<testcase classname="AccountTest2.AccountTest" name="BalanceTest" status="Passed" time="0.0007950"/>
<testcase classname="AccountTest2.AccountTest" name="CalculateInterestTest" status="Passed" time="0.0019225"/>
<testcase classname="AccountTest2.AccountTest" name="DepositTest" status="Passed" time="0.0024338"/>
<testcase classname="AccountTest2.AccountTest" name="MTestFail" status="Failed" time="0.0598048">
<failure message="Assert.Fail failed. Always fails">
at AccountTest2.AccountTest.MTestFail() in c:\Jenkins_Home\jobs\NUnit_Quick_Learning\workspace\AccountTest2\AccountMTest.cs:line 253
</failure>
</testcase>
<testcase classname="AccountTest2.AccountTest" name="MTestInconclusive" status="Skipped" time="0.0015675">
<skipped message="Assert.Inconclusive failed. Inconclusive result">
at AccountTest2.AccountTest.MTestInconclusive() in c:\Jenkins_Home\jobs\NUnit_Quick_Learning\workspace\AccountTest2\AccountMTest.cs:line 247
</skipped>
</testcase>
<testcase classname="AccountTest2.AccountTest" name="MTestNotExecuted" status="Skipped" time="0.0005682">
<skipped/>
</testcase>
<testcase classname="AccountTest2.AccountTest" name="MinimumBalanceTest" status="Passed" time="0.0005534"/>
<testcase classname="AccountTest2.AccountTest" name="TransferFundsTest" status="Passed" time="0.0027771"/>
<testcase classname="AccountTest2.AccountTest" name="WithdrawTest" status="Passed" time="0.0017588"/>
</testsuite>
<testsuite name="AccountTest2.InsufficientFundsExceptionTest" tests="1" failures="0" skipped="0" time="0.0027408">
<testcase classname="AccountTest2.InsufficientFundsExceptionTest" name="InsufficientFundsExceptionConstructorTest" status="Passed" time="0.0027408"/>
</testsuite>
<testsuite name="AccountTest2.OverdrawnExceptionTest" tests="1" failures="0" skipped="0" time="0.0021815">
<testcase classname="AccountTest2.OverdrawnExceptionTest" name="OverdrawnExceptionConstructorTest" status="Passed" time="0.0021815"/>
</testsuite>
</testsuites>
Loading

0 comments on commit cb9cc15

Please sign in to comment.