-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
645 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
'use strict'; | ||
const { | ||
ArrayPrototypeFilter, | ||
ArrayPrototypeMap, | ||
ArrayPrototypeJoin, | ||
ArrayPrototypePush, | ||
ArrayPrototypeSome, | ||
NumberPrototypeToFixed, | ||
ObjectEntries, | ||
RegExpPrototypeSymbolReplace, | ||
String, | ||
StringPrototypeRepeat, | ||
} = primordials; | ||
|
||
const { inspectWithNoCustomRetry } = require('internal/errors'); | ||
const { hostname } = require('os'); | ||
|
||
const inspectOptions = { __proto__: null, colors: false, breakLength: Infinity }; | ||
const HOSTNAME = hostname(); | ||
|
||
function escapeProperty(s = '') { | ||
return escapeContent(RegExpPrototypeSymbolReplace(/"/g, RegExpPrototypeSymbolReplace(/\n/g, s, ''), '\\"')); | ||
} | ||
|
||
function escapeContent(s = '') { | ||
return RegExpPrototypeSymbolReplace(/</g, RegExpPrototypeSymbolReplace(/>/g, s, '>'), '<'); | ||
} | ||
|
||
function treeToXML(tree) { | ||
if (typeof tree === 'string') { | ||
return `${escapeContent(tree)}\n`; | ||
} | ||
const { | ||
tag, props, nesting, children, | ||
} = tree; | ||
const propsString = ArrayPrototypeJoin(ArrayPrototypeMap(ObjectEntries(props) | ||
, ({ 0: key, 1: value }) => `${key}="${escapeProperty(String(value))}"`) | ||
, ' '); | ||
const indent = StringPrototypeRepeat('\t', nesting + 1); | ||
if (!children?.length) { | ||
return `${indent}<${tag} ${propsString}/>\n`; | ||
} | ||
const childrenString = ArrayPrototypeJoin(ArrayPrototypeMap(children ?? [], treeToXML), ''); | ||
return `${indent}<${tag} ${propsString}>\n${childrenString}${indent}</${tag}>\n`; | ||
} | ||
|
||
function isFailure(node) { | ||
return (node?.children && ArrayPrototypeSome(node.children, (c) => c.tag === 'failure')) || node?.props?.failures; | ||
} | ||
|
||
function isSkipped(node) { | ||
return (node?.children && ArrayPrototypeSome(node.children, (c) => c.tag === 'skipped')) || node?.props?.failures; | ||
} | ||
|
||
module.exports = async function* junitReporter(source) { | ||
yield '<?xml version="1.0" encoding="utf-8"?>\n'; | ||
yield '<testsuites>\n'; | ||
let currentSuite = null; | ||
const roots = []; | ||
|
||
function startTest(event) { | ||
const originalSuite = currentSuite; | ||
currentSuite = { | ||
__proto__: null, | ||
props: { __proto__: null, name: event.data.name }, | ||
nesting: event.data.nesting, | ||
parent: currentSuite, | ||
children: [], | ||
}; | ||
if (originalSuite?.children) { | ||
ArrayPrototypePush(originalSuite.children, currentSuite); | ||
} | ||
if (!currentSuite.parent) { | ||
ArrayPrototypePush(roots, currentSuite); | ||
} | ||
} | ||
|
||
for await (const event of source) { | ||
switch (event.type) { | ||
case 'test:start': { | ||
startTest(event); | ||
break; | ||
} | ||
case 'test:pass': | ||
case 'test:fail': { | ||
if (!currentSuite) { | ||
startTest({ __proto__: null, data: { __proto__: null, name: 'root', nesting: 0 } }); | ||
} | ||
if (currentSuite.props.name !== event.data.name || | ||
currentSuite.nesting !== event.data.nesting) { | ||
startTest(event); | ||
} | ||
const currentTest = currentSuite; | ||
if (currentSuite?.nesting === event.data.nesting) { | ||
currentSuite = currentSuite.parent; | ||
} | ||
currentTest.props.time = NumberPrototypeToFixed(event.data.details.duration_ms / 1000, 6); | ||
if (currentTest.children.length > 0) { | ||
currentTest.tag = 'testsuite'; | ||
currentTest.props.disabled = 0; | ||
currentTest.props.errors = 0; | ||
currentTest.props.tests = currentTest.children.length; | ||
currentTest.props.failures = ArrayPrototypeFilter(currentTest.children, isFailure).length; | ||
currentTest.props.skipped = ArrayPrototypeFilter(currentTest.children, isSkipped).length; | ||
currentTest.props.hostname = HOSTNAME; | ||
} else { | ||
currentTest.tag = 'testcase'; | ||
currentTest.props.classname = event.data.classname ?? 'test'; | ||
if (event.data.skip) { | ||
ArrayPrototypePush(currentTest.children, { | ||
__proto__: null, nesting: event.data.nesting + 1, tag: 'skipped', | ||
props: { __proto__: null, type: 'skipped', message: event.data.skip }, | ||
}); | ||
} | ||
if (event.data.todo) { | ||
ArrayPrototypePush(currentTest.children, { | ||
__proto__: null, nesting: event.data.nesting + 1, tag: 'skipped', | ||
props: { __proto__: null, type: 'todo', message: event.data.todo }, | ||
}); | ||
} | ||
if (event.type === 'test:fail') { | ||
const error = event.data.details?.error; | ||
currentTest.children.push({ | ||
__proto__: null, | ||
nesting: event.data.nesting + 1, | ||
tag: 'failure', | ||
props: { __proto__: null, type: error?.failureType || error?.code, message: error?.message ?? '' }, | ||
children: [inspectWithNoCustomRetry(error, inspectOptions)], | ||
}); | ||
currentTest.failures = 1; | ||
currentTest.props.failure = error?.message ?? ''; | ||
} | ||
} | ||
break; | ||
} | ||
case 'test:diagnostic': | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
for (const suite of roots) { | ||
yield treeToXML(suite); | ||
} | ||
yield '</testsuites>\n'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
require('../../../common'); | ||
const fixtures = require('../../../common/fixtures'); | ||
const spawn = require('node:child_process').spawn; | ||
|
||
spawn(process.execPath, | ||
['--no-warnings', '--test-reporter', 'junit', fixtures.path('test-runner/output/output.js')], { stdio: 'inherit' }); |
Oops, something went wrong.