Skip to content

Commit 3db19d6

Browse files
Spencerspalger
andauthored
[junit] make sure that report paths are unique (#81255)
Co-authored-by: spalger <spalger@users.noreply.github.com>
1 parent 93473e4 commit 3db19d6

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

packages/kbn-test/src/jest/integration_tests/junit_reporter.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ import { readFileSync } from 'fs';
2424
import del from 'del';
2525
import execa from 'execa';
2626
import xml2js from 'xml2js';
27-
import { makeJunitReportPath } from '@kbn/test';
27+
import { getUniqueJunitReportPath } from '@kbn/test';
2828
import { REPO_ROOT } from '@kbn/utils';
2929

3030
const MINUTE = 1000 * 60;
3131
const FIXTURE_DIR = resolve(__dirname, '__fixtures__');
3232
const TARGET_DIR = resolve(FIXTURE_DIR, 'target');
33-
const XML_PATH = makeJunitReportPath(FIXTURE_DIR, 'JUnit Reporter Integration Test');
33+
const XML_PATH = getUniqueJunitReportPath(FIXTURE_DIR, 'JUnit Reporter Integration Test');
3434

3535
afterAll(async () => {
3636
await del(TARGET_DIR);

packages/kbn-test/src/jest/junit_reporter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type { Config } from '@jest/types';
2727
import { AggregatedResult, Test, BaseReporter } from '@jest/reporters';
2828

2929
import { escapeCdata } from '../mocha/xml';
30-
import { makeJunitReportPath } from './report_path';
30+
import { getUniqueJunitReportPath } from './report_path';
3131

3232
interface ReporterOptions {
3333
reportName?: string;
@@ -115,7 +115,7 @@ export default class JestJUnitReporter extends BaseReporter {
115115
});
116116
});
117117

118-
const reportPath = makeJunitReportPath(rootDirectory, reportName);
118+
const reportPath = getUniqueJunitReportPath(rootDirectory, reportName);
119119
const reportXML = root.end();
120120
mkdirSync(dirname(reportPath), { recursive: true });
121121
writeFileSync(reportPath, reportXML, 'utf8');

packages/kbn-test/src/jest/report_path.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,24 @@
1717
* under the License.
1818
*/
1919

20-
import { resolve } from 'path';
20+
import Fs from 'fs';
21+
import Path from 'path';
22+
2123
import { CI_PARALLEL_PROCESS_PREFIX } from '../ci_parallel_process_prefix';
2224

23-
export function makeJunitReportPath(rootDirectory: string, reportName: string) {
24-
return resolve(
25+
export function getUniqueJunitReportPath(
26+
rootDirectory: string,
27+
reportName: string,
28+
counter?: number
29+
): string {
30+
const path = Path.resolve(
2531
rootDirectory,
2632
'target/junit',
2733
process.env.JOB || '.',
28-
`TEST-${CI_PARALLEL_PROCESS_PREFIX}${reportName}.xml`
34+
`TEST-${CI_PARALLEL_PROCESS_PREFIX}${reportName}${counter ? `-${counter}` : ''}.xml`
2935
);
36+
37+
return Fs.existsSync(path)
38+
? getUniqueJunitReportPath(rootDirectory, reportName, (counter ?? 0) + 1)
39+
: path;
3040
}

packages/kbn-test/src/mocha/__tests__/junit_report_generation.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ import { parseString } from 'xml2js';
2525
import del from 'del';
2626
import Mocha from 'mocha';
2727
import expect from '@kbn/expect';
28-
import { makeJunitReportPath } from '@kbn/test';
28+
import { getUniqueJunitReportPath } from '@kbn/test';
2929

3030
import { setupJUnitReportGeneration } from '../junit_report_generation';
3131

3232
const PROJECT_DIR = resolve(__dirname, 'fixtures/project');
3333
const DURATION_REGEX = /^\d+\.\d{3}$/;
3434
const ISO_DATE_SEC_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$/;
35+
const XML_PATH = getUniqueJunitReportPath(PROJECT_DIR, 'test');
3536

3637
describe('dev/mocha/junit report generation', () => {
3738
afterEach(() => {
@@ -50,9 +51,7 @@ describe('dev/mocha/junit report generation', () => {
5051

5152
mocha.addFile(resolve(PROJECT_DIR, 'test.js'));
5253
await new Promise((resolve) => mocha.run(resolve));
53-
const report = await fcb((cb) =>
54-
parseString(readFileSync(makeJunitReportPath(PROJECT_DIR, 'test')), cb)
55-
);
54+
const report = await fcb((cb) => parseString(readFileSync(XML_PATH), cb));
5655

5756
// test case results are wrapped in <testsuites></testsuites>
5857
expect(report).to.eql({

packages/kbn-test/src/mocha/junit_report_generation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { writeFileSync, mkdirSync } from 'fs';
2222
import { inspect } from 'util';
2323

2424
import xmlBuilder from 'xmlbuilder';
25-
import { makeJunitReportPath } from '@kbn/test';
25+
import { getUniqueJunitReportPath } from '@kbn/test';
2626

2727
import { getSnapshotOfRunnableLogs } from './log_cache';
2828
import { escapeCdata } from '../';
@@ -140,7 +140,7 @@ export function setupJUnitReportGeneration(runner, options = {}) {
140140
}
141141
});
142142

143-
const reportPath = makeJunitReportPath(rootDirectory, reportName);
143+
const reportPath = getUniqueJunitReportPath(rootDirectory, reportName);
144144
const reportXML = builder.end();
145145
mkdirSync(dirname(reportPath), { recursive: true });
146146
writeFileSync(reportPath, reportXML, 'utf8');

0 commit comments

Comments
 (0)