Skip to content

Commit 02c42bc

Browse files
authored
chore(ci): Use relative paths in junit XML file attribute (#7642)
* Use relative names in XML exports * Fix mocha
1 parent e2dc064 commit 02c42bc

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

.mochamultireporterrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ const reporterEnabled = ['spec']
44

55
// eslint-disable-next-line eslint-rules/eslint-process-env
66
if (process.env.CI) {
7-
reporterEnabled.push('mocha-junit-reporter')
7+
reporterEnabled.push('./scripts/junit-reporter.js')
88
}
99

1010
module.exports = {
1111
reporterEnabled,
12-
mochaJunitReporterReporterOptions: {
12+
scriptsJunitReporterJsReporterOptions: {
1313
mochaFile: `./node-${process.versions.node}-junit.xml`,
1414
},
1515
}

scripts/junit-reporter.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict'
2+
3+
const path = require('node:path')
4+
const MochaJUnitReporter = require('mocha-junit-reporter')
5+
6+
const root = path.resolve(__dirname, '..')
7+
8+
/**
9+
* Thin wrapper around mocha-junit-reporter that rewrites absolute `file`
10+
* attributes to paths relative to the repository root.
11+
*/
12+
class JUnitReporter extends MochaJUnitReporter {
13+
getTestsuiteData (suite) {
14+
const testSuite = super.getTestsuiteData(suite)
15+
const attrs = testSuite.testsuite[0]._attr
16+
if (attrs.file) {
17+
attrs.file = path.relative(root, attrs.file)
18+
}
19+
return testSuite
20+
}
21+
}
22+
23+
module.exports = JUnitReporter

scripts/mocha-parallel-files.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ async function main () {
216216
const runFileScript = path.join(repoRoot, 'scripts', 'mocha-run-file.js')
217217

218218
const junitTmpDir = path.join(repoRoot, '.junit-tmp')
219-
const junitOutFile = path.join(repoRoot, multiMochaRc.mochaJunitReporterReporterOptions.mochaFile)
219+
const junitOutFile = path.join(repoRoot, multiMochaRc.scriptsJunitReporterJsReporterOptions.mochaFile)
220220
const junitTmpFiles = []
221221
const emitJunit = Boolean(process.env.CI)
222222

@@ -348,7 +348,7 @@ async function main () {
348348
const reporterOptions = emitJunit
349349
? {
350350
reporterEnabled: ['spec', 'xunit'],
351-
xunitReporterOptions: { output: /** @type {string} */ (junitShard) },
351+
xunitReporterOptions: { output: /** @type {string} */ (junitShard), showRelativePaths: true },
352352
}
353353
: {
354354
reporterEnabled: ['spec'],

0 commit comments

Comments
 (0)