chore: ensure the file operations are async in the junit reporter (#24577)

This commit is contained in:
Marcin Strzyz 2023-08-07 01:03:01 -07:00 committed by GitHub
parent a0211924d3
commit 53fd4bedb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,7 +58,7 @@ class JUnitReporter extends EmptyReporter {
const children: XMLEntry[] = []; const children: XMLEntry[] = [];
for (const projectSuite of this.suite.suites) { for (const projectSuite of this.suite.suites) {
for (const fileSuite of projectSuite.suites) for (const fileSuite of projectSuite.suites)
children.push(this._buildTestSuite(projectSuite.title, fileSuite)); children.push(await this._buildTestSuite(projectSuite.title, fileSuite));
} }
const tokens: string[] = []; const tokens: string[] = [];
@ -82,21 +82,21 @@ class JUnitReporter extends EmptyReporter {
if (this.outputFile) { if (this.outputFile) {
assert(this.config.configFile || path.isAbsolute(this.outputFile), 'Expected fully resolved path if not using config file.'); assert(this.config.configFile || path.isAbsolute(this.outputFile), 'Expected fully resolved path if not using config file.');
const outputFile = this.config.configFile ? path.resolve(path.dirname(this.config.configFile), this.outputFile) : this.outputFile; const outputFile = this.config.configFile ? path.resolve(path.dirname(this.config.configFile), this.outputFile) : this.outputFile;
fs.mkdirSync(path.dirname(outputFile), { recursive: true }); await fs.promises.mkdir(path.dirname(outputFile), { recursive: true });
fs.writeFileSync(outputFile, reportString); await fs.promises.writeFile(outputFile, reportString);
} else { } else {
console.log(reportString); console.log(reportString);
} }
} }
private _buildTestSuite(projectName: string, suite: Suite): XMLEntry { private async _buildTestSuite(projectName: string, suite: Suite): Promise<XMLEntry> {
let tests = 0; let tests = 0;
let skipped = 0; let skipped = 0;
let failures = 0; let failures = 0;
let duration = 0; let duration = 0;
const children: XMLEntry[] = []; const children: XMLEntry[] = [];
suite.allTests().forEach(test => { for (const test of suite.allTests()){
++tests; ++tests;
if (test.outcome() === 'skipped') if (test.outcome() === 'skipped')
++skipped; ++skipped;
@ -104,8 +104,9 @@ class JUnitReporter extends EmptyReporter {
++failures; ++failures;
for (const result of test.results) for (const result of test.results)
duration += result.duration; duration += result.duration;
this._addTestCase(suite.title, test, children); await this._addTestCase(suite.title, test, children);
}); }
this.totalTests += tests; this.totalTests += tests;
this.totalSkipped += skipped; this.totalSkipped += skipped;
this.totalFailures += failures; this.totalFailures += failures;
@ -128,7 +129,7 @@ class JUnitReporter extends EmptyReporter {
return entry; return entry;
} }
private _addTestCase(suiteName: string, test: TestCase, entries: XMLEntry[]) { private async _addTestCase(suiteName: string, test: TestCase, entries: XMLEntry[]) {
const entry = { const entry = {
name: 'testcase', name: 'testcase',
attributes: { attributes: {
@ -189,13 +190,13 @@ class JUnitReporter extends EmptyReporter {
for (const attachment of result.attachments) { for (const attachment of result.attachments) {
if (!attachment.path) if (!attachment.path)
continue; continue;
const attachmentPath = path.relative(this.config.rootDir, attachment.path);
try { try {
const attachmentPath = path.relative(this.config.rootDir, attachment.path); await fs.promises.access(attachment.path);
if (fs.existsSync(attachment.path)) systemOut.push(`\n[[ATTACHMENT|${attachmentPath}]]\n`);
systemOut.push(`\n[[ATTACHMENT|${attachmentPath}]]\n`); } catch {
else systemErr.push(`\nWarning: attachment ${attachmentPath} is missing`);
systemErr.push(`\nWarning: attachment ${attachmentPath} is missing`);
} catch (e) {
} }
} }
} }