Skip to content

Commit

Permalink
[7.x] [junit] make sure that report paths are unique (#81255) (#81389)
Browse files Browse the repository at this point in the history
Co-authored-by: spalger <spalger@users.noreply.github.com>
  • Loading branch information
Spencer and spalger authored Oct 21, 2020
1 parent 5f9d281 commit 1174251
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import { readFileSync } from 'fs';
import del from 'del';
import execa from 'execa';
import xml2js from 'xml2js';
import { makeJunitReportPath } from '@kbn/test';
import { getUniqueJunitReportPath } from '@kbn/test';
import { REPO_ROOT } from '@kbn/utils';

const MINUTE = 1000 * 60;
const FIXTURE_DIR = resolve(__dirname, '__fixtures__');
const TARGET_DIR = resolve(FIXTURE_DIR, 'target');
const XML_PATH = makeJunitReportPath(FIXTURE_DIR, 'JUnit Reporter Integration Test');
const XML_PATH = getUniqueJunitReportPath(FIXTURE_DIR, 'JUnit Reporter Integration Test');

afterAll(async () => {
await del(TARGET_DIR);
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-test/src/jest/junit_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type { Config } from '@jest/types';
import { AggregatedResult, Test, BaseReporter } from '@jest/reporters';

import { escapeCdata } from '../mocha/xml';
import { makeJunitReportPath } from './report_path';
import { getUniqueJunitReportPath } from './report_path';

interface ReporterOptions {
reportName?: string;
Expand Down Expand Up @@ -115,7 +115,7 @@ export default class JestJUnitReporter extends BaseReporter {
});
});

const reportPath = makeJunitReportPath(rootDirectory, reportName);
const reportPath = getUniqueJunitReportPath(rootDirectory, reportName);
const reportXML = root.end();
mkdirSync(dirname(reportPath), { recursive: true });
writeFileSync(reportPath, reportXML, 'utf8');
Expand Down
18 changes: 14 additions & 4 deletions packages/kbn-test/src/jest/report_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@
* under the License.
*/

import { resolve } from 'path';
import Fs from 'fs';
import Path from 'path';

import { CI_PARALLEL_PROCESS_PREFIX } from '../ci_parallel_process_prefix';

export function makeJunitReportPath(rootDirectory: string, reportName: string) {
return resolve(
export function getUniqueJunitReportPath(
rootDirectory: string,
reportName: string,
counter?: number
): string {
const path = Path.resolve(
rootDirectory,
'target/junit',
process.env.JOB || '.',
`TEST-${CI_PARALLEL_PROCESS_PREFIX}${reportName}.xml`
`TEST-${CI_PARALLEL_PROCESS_PREFIX}${reportName}${counter ? `-${counter}` : ''}.xml`
);

return Fs.existsSync(path)
? getUniqueJunitReportPath(rootDirectory, reportName, (counter ?? 0) + 1)
: path;
}
7 changes: 3 additions & 4 deletions src/dev/mocha/__tests__/junit_report_generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import { parseString } from 'xml2js';
import del from 'del';
import Mocha from 'mocha';
import expect from '@kbn/expect';
import { makeJunitReportPath } from '@kbn/test';
import { getUniqueJunitReportPath } from '@kbn/test';

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

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

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

mocha.addFile(resolve(PROJECT_DIR, 'test.js'));
await new Promise((resolve) => mocha.run(resolve));
const report = await fcb((cb) =>
parseString(readFileSync(makeJunitReportPath(PROJECT_DIR, 'test')), cb)
);
const report = await fcb((cb) => parseString(readFileSync(XML_PATH), cb));

// test case results are wrapped in <testsuites></testsuites>
expect(report).to.eql({
Expand Down
4 changes: 2 additions & 2 deletions src/dev/mocha/junit_report_generation.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { writeFileSync, mkdirSync } from 'fs';
import { inspect } from 'util';

import xmlBuilder from 'xmlbuilder';
import { makeJunitReportPath } from '@kbn/test';
import { getUniqueJunitReportPath } from '@kbn/test';

import { getSnapshotOfRunnableLogs } from './log_cache';
import { escapeCdata } from '../xml';
Expand Down Expand Up @@ -140,7 +140,7 @@ export function setupJUnitReportGeneration(runner, options = {}) {
}
});

const reportPath = makeJunitReportPath(rootDirectory, reportName);
const reportPath = getUniqueJunitReportPath(rootDirectory, reportName);
const reportXML = builder.end();
mkdirSync(dirname(reportPath), { recursive: true });
writeFileSync(reportPath, reportXML, 'utf8');
Expand Down

0 comments on commit 1174251

Please sign in to comment.