Skip to content

Commit 55f82ed

Browse files
authored
Add CustomStringConvertible conformance to ExitTest.Condition and StatusAtExit. (#1073)
This PR adds `CustomStringConvertible` conformance to these data/value types so that they are presented reasonably in expectation failure messages or when printed. Strictly speaking, API changes should go through a review, but there's not really anything to review here, so with the consent of the Testing Workgroup we can amend the exit tests proposal to retroactively include these conformances. See also: swiftlang/swift-evolution#2789 ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 197d6b3 commit 55f82ed

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

Sources/Testing/ExitTests/ExitTest.Condition.swift

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ extension ExitTest {
2727
/// The exit test must exit with a particular exit status.
2828
case statusAtExit(StatusAtExit)
2929

30+
/// The exit test must exit successfully.
31+
case success
32+
3033
/// The exit test must exit with any failure.
3134
case failure
3235
}
@@ -46,15 +49,7 @@ extension ExitTest.Condition {
4649
/// A condition that matches when a process terminates successfully with exit
4750
/// code `EXIT_SUCCESS`.
4851
public static var success: Self {
49-
// Strictly speaking, the C standard treats 0 as a successful exit code and
50-
// potentially distinct from EXIT_SUCCESS. To my knowledge, no modern
51-
// operating system defines EXIT_SUCCESS to any value other than 0, so the
52-
// distinction is academic.
53-
#if !SWT_NO_EXIT_TESTS
54-
.exitCode(EXIT_SUCCESS)
55-
#else
56-
fatalError("Unsupported")
57-
#endif
52+
Self(_kind: .success)
5853
}
5954

6055
/// A condition that matches when a process terminates abnormally with any
@@ -122,6 +117,29 @@ extension ExitTest.Condition {
122117
}
123118
}
124119

120+
// MARK: - CustomStringConvertible
121+
122+
@_spi(Experimental)
123+
#if SWT_NO_EXIT_TESTS
124+
@available(*, unavailable, message: "Exit tests are not available on this platform.")
125+
#endif
126+
extension ExitTest.Condition: CustomStringConvertible {
127+
public var description: String {
128+
#if !SWT_NO_EXIT_TESTS
129+
switch _kind {
130+
case .failure:
131+
".failure"
132+
case .success:
133+
".success"
134+
case let .statusAtExit(statusAtExit):
135+
String(describing: statusAtExit)
136+
}
137+
#else
138+
fatalError("Unsupported")
139+
#endif
140+
}
141+
}
142+
125143
// MARK: - Comparison
126144

127145
#if SWT_NO_EXIT_TESTS
@@ -139,7 +157,13 @@ extension ExitTest.Condition {
139157
/// Two exit test conditions can be compared; if either instance is equal to
140158
/// ``failure``, it will compare equal to any instance except ``success``.
141159
func isApproximatelyEqual(to statusAtExit: StatusAtExit) -> Bool {
160+
// Strictly speaking, the C standard treats 0 as a successful exit code and
161+
// potentially distinct from EXIT_SUCCESS. To my knowledge, no modern
162+
// operating system defines EXIT_SUCCESS to any value other than 0, so the
163+
// distinction is academic.
142164
return switch (self._kind, statusAtExit) {
165+
case let (.success, .exitCode(exitCode)):
166+
exitCode == EXIT_SUCCESS
143167
case let (.failure, .exitCode(exitCode)):
144168
exitCode != EXIT_SUCCESS
145169
case (.failure, .signal):

Sources/Testing/ExitTests/StatusAtExit.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,19 @@ public enum StatusAtExit: Sendable {
7171
@available(*, unavailable, message: "Exit tests are not available on this platform.")
7272
#endif
7373
extension StatusAtExit: Equatable {}
74+
75+
// MARK: - CustomStringConvertible
76+
@_spi(Experimental)
77+
#if SWT_NO_PROCESS_SPAWNING
78+
@available(*, unavailable, message: "Exit tests are not available on this platform.")
79+
#endif
80+
extension StatusAtExit: CustomStringConvertible {
81+
public var description: String {
82+
switch self {
83+
case let .exitCode(exitCode):
84+
".exitCode(\(exitCode))"
85+
case let .signal(signal):
86+
".signal(\(signal))"
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)