Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skipped test tracking to JUnit output. #549

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions Sources/Testing/Events/Recorder/Event.JUnitXMLRecorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
extension Event {
/// A type which handles ``Event`` instances and outputs representations of
/// them as JUnit-compatible XML.
///
/// The maintainers of JUnit do not publish a formal XML schema. A _de facto_
/// schema is described in the [JUnit repository](https://github.com/junit-team/junit5/blob/main/junit-platform-reporting/src/main/java/org/junit/platform/reporting/legacy/xml/XmlReportWriter.java).
@_spi(ForToolsIntegrationOnly)
public struct JUnitXMLRecorder: Sendable/*, ~Copyable*/ {
/// The write function for this event recorder.
Expand Down Expand Up @@ -43,6 +46,9 @@ extension Event {

/// Any issues recorded for the test.
var issues = [Issue]()

/// Information about the test if it was skipped.
var skipInfo: SkipInfo?
}

/// Data tracked on a per-test basis.
Expand Down Expand Up @@ -105,7 +111,12 @@ extension Event.JUnitXMLRecorder {
context.testData[keyPath]?.endInstant = instant
}
return nil
case .testSkipped where false == test?.isSuite:
case let .testSkipped(skipInfo) where false == test?.isSuite:
let id = test!.id
let keyPath = id.keyPathRepresentation
_context.withLock { context in
context.testData[keyPath] = _Context.TestData(id: id, startInstant: instant, skipInfo: skipInfo)
}
return nil
case let .issueRecorded(issue):
if issue.isKnown {
Expand All @@ -124,10 +135,13 @@ extension Event.JUnitXMLRecorder {
let issueCount = context.testData
.compactMap(\.value?.issues.count)
.reduce(into: 0, +=)
let skipCount = context.testData
.compactMap(\.value?.skipInfo)
.count
let durationNanoseconds = context.runStartInstant.map { $0.nanoseconds(until: instant) } ?? 0
let durationSeconds = Double(durationNanoseconds) / 1_000_000_000
return #"""
<testsuite name="TestResults" errors="0" tests="\#(context.testCount)" failures="\#(issueCount)" time="\#(durationSeconds)">
<testsuite name="TestResults" errors="0" tests="\#(context.testCount)" failures="\#(issueCount)" skipped="\#(skipCount)" time="\#(durationSeconds)">
\#(Self._xml(for: context.testData))
</testsuite>
</testsuites>
Expand Down Expand Up @@ -158,13 +172,25 @@ extension Event.JUnitXMLRecorder {
let name = id.nameComponents.last!
let durationNanoseconds = testData.startInstant.nanoseconds(until: testData.endInstant ?? .now)
let durationSeconds = Double(durationNanoseconds) / 1_000_000_000
if testData.issues.isEmpty {

// Build out any child nodes contained within this <testcase> node.
var minutiae = [String]()
for issue in testData.issues.lazy.map(String.init(describingForTest:)) {
minutiae.append(#" <failure message="\#(Self._escapeForXML(issue))" />"#)
}
if let skipInfo = testData.skipInfo {
if let comment = skipInfo.comment.map(String.init(describingForTest:)) {
minutiae.append(#" <skipped>\#(Self._escapeForXML(comment))</skipped>"#)
} else {
minutiae.append(#" <skipped />"#)
}
}

if minutiae.isEmpty {
result.append(#" <testcase classname="\#(className)" name="\#(name)" time="\#(durationSeconds)" />"#)
} else {
result.append(#" <testcase classname="\#(className)" name="\#(name)" time="\#(durationSeconds)">"#)
result += testData.issues.lazy
.map(String.init(describing:))
.map { #" <failure message="\#(Self._escapeForXML($0))" />"# }
result += minutiae
result.append(#" </testcase>"#)
}
} else {
Expand All @@ -183,14 +209,19 @@ extension Event.JUnitXMLRecorder {
///
/// - Returns: `character`, or a string containing its escaped form.
private static func _escapeForXML(_ character: Character) -> String {
if character == #"""# {
switch character {
Copy link

@jakepetroules jakepetroules Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> isn't required to be escaped in XML attributes, but & definitely is. Could you add that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna leave > out of pedantry, but yeah I'll add &.

case #"""#:
"&quot;"
} else if !character.isASCII {
case "<":
"&lt;"
case ">":
"&gt;"
case _ where !character.isASCII:
character.unicodeScalars.lazy
.map(\.value)
.map { "&#\($0);" }
.joined()
} else {
default:
String(character)
}
}
Expand Down