Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Improve JUnit generation #273

Merged
merged 6 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/custom-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* This is an extension of MochaJunitReporter
* This is an extension of mocha-junit-reporter
*/

import xml from 'xml';
Expand All @@ -23,7 +23,7 @@
const Base = Mocha.reporters.Base;
const debug = Debug('mocha-junit-reporter');

let createStatsCollector: (arg0: any) => void;

Check warning on line 26 in src/custom-reporter.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
let mocha6plus = false;

try {
Expand All @@ -49,7 +49,7 @@
const INVALID_CHARACTERS_REGEX =
/[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007f-\u0084\u0086-\u009f\uD800-\uDFFF\uFDD0-\uFDFF\uFFFF\uC008]/g; //eslint-disable-line no-control-regex

function findReporterOptions(options: any): any {

Check warning on line 52 in src/custom-reporter.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 52 in src/custom-reporter.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
debug('Checking for options in', options);
if (!options) {
debug('No options provided');
Expand All @@ -75,7 +75,7 @@
}, {});
}

function configureDefaults(options: any): any {

Check warning on line 78 in src/custom-reporter.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type

Check warning on line 78 in src/custom-reporter.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
const config = findReporterOptions(options);
debug('options', config);
config.mochaFile = getSetting(
Expand Down Expand Up @@ -109,7 +109,7 @@
return config;
}

function updateOptionsForAntMode(options: any) {

Check warning on line 112 in src/custom-reporter.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
options.antHostname = getSetting(
options.antHostname,
'ANT_HOSTNAME',
Expand All @@ -121,7 +121,7 @@
}
}

function updateOptionsForJenkinsMode(options: any) {

Check warning on line 124 in src/custom-reporter.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
if (options.useFullSuiteTitle === undefined) {
options.useFullSuiteTitle = true;
}
Expand Down Expand Up @@ -149,10 +149,10 @@
* @param {function} transform - a transformation function to be used when loading values from the environment
*/
function getSetting(
value: any,

Check warning on line 152 in src/custom-reporter.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
key: string,
defaultVal: any,

Check warning on line 154 in src/custom-reporter.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
transform: any | undefined = undefined,

Check warning on line 155 in src/custom-reporter.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
) {
if (process.env[key] !== undefined) {
const envVal = process.env[key];
Expand Down Expand Up @@ -345,7 +345,6 @@
};

MochaJUnitReporter.prototype.getSauceTestcaseData = function (testcase: any) {
// console.log(testcase);
return {
id: testcase.order,
screenshot: 0,
Expand Down
6 changes: 3 additions & 3 deletions src/cypress-runner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mergeJunitFile } from './sauce-reporter';
import { mergeJUnitFile } from './sauce-reporter';
import path from 'path';
import fs from 'fs';
import {
Expand Down Expand Up @@ -38,15 +38,15 @@ async function report(
}

try {
mergeJunitFile(
mergeJUnitFile(
specFiles,
runCfg.resultsDir,
suiteName,
browserName,
platformName,
);
} catch (e) {
console.error('Failed to generate junit file:', e);
console.warn('Skipping JUnit file generation:', e);
}

try {
Expand Down
15 changes: 10 additions & 5 deletions src/sauce-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { escapeXML } from 'sauce-testrunner-utils';
import convert from 'xml-js';
import { XmlSuiteContainer } from './types';

export function mergeJunitFile(
export function mergeJUnitFile(
specFiles: any[],
resultsFolder: string,
testName: string,
Expand All @@ -22,16 +22,21 @@ export function mergeJunitFile(
} = { compact: true, spaces: 4 };
const testsuites = [];
for (let i = 0; i < specFiles.length; i++) {
const xmlData = fs.readFileSync(
path.join(resultsFolder, `${specFiles[i]}.xml`),
'utf8',
);
const specJUnitFile = path.join(resultsFolder, `${specFiles[i]}.xml`);
if (!fs.existsSync(specJUnitFile)) {
console.warn(
`JUnit file not found for spec: ${specFiles[i]}. Proceeding without it...`,
);
continue;
}
const xmlData = fs.readFileSync(specJUnitFile, 'utf8');
const jsObj = convert.xml2js(xmlData, opts) as XmlSuiteContainer;
if (jsObj.testsuites && jsObj.testsuites.testsuite) {
testsuites.push(...jsObj.testsuites.testsuite);
}
}
if (testsuites.length === 0) {
console.warn('JUnit file generation skipped: no test suites detected.');
return;
}

Expand Down
Loading