Skip to content

Add CustomStringConvertible conformance to ExitTest.Condition and StatusAtExit. #1073

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

Merged
merged 1 commit into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
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
42 changes: 33 additions & 9 deletions Sources/Testing/ExitTests/ExitTest.Condition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ extension ExitTest {
/// The exit test must exit with a particular exit status.
case statusAtExit(StatusAtExit)

/// The exit test must exit successfully.
case success

/// The exit test must exit with any failure.
case failure
}
Expand All @@ -46,15 +49,7 @@ extension ExitTest.Condition {
/// A condition that matches when a process terminates successfully with exit
/// code `EXIT_SUCCESS`.
public static var success: Self {
// Strictly speaking, the C standard treats 0 as a successful exit code and
// potentially distinct from EXIT_SUCCESS. To my knowledge, no modern
// operating system defines EXIT_SUCCESS to any value other than 0, so the
// distinction is academic.
#if !SWT_NO_EXIT_TESTS
.exitCode(EXIT_SUCCESS)
#else
fatalError("Unsupported")
#endif
Self(_kind: .success)
}

/// A condition that matches when a process terminates abnormally with any
Expand Down Expand Up @@ -122,6 +117,29 @@ extension ExitTest.Condition {
}
}

// MARK: - CustomStringConvertible

@_spi(Experimental)
#if SWT_NO_EXIT_TESTS
@available(*, unavailable, message: "Exit tests are not available on this platform.")
#endif
extension ExitTest.Condition: CustomStringConvertible {
public var description: String {
#if !SWT_NO_EXIT_TESTS
switch _kind {
case .failure:
".failure"
case .success:
".success"
case let .statusAtExit(statusAtExit):
String(describing: statusAtExit)
}
#else
fatalError("Unsupported")
#endif
}
}

// MARK: - Comparison

#if SWT_NO_EXIT_TESTS
Expand All @@ -139,7 +157,13 @@ extension ExitTest.Condition {
/// Two exit test conditions can be compared; if either instance is equal to
/// ``failure``, it will compare equal to any instance except ``success``.
func isApproximatelyEqual(to statusAtExit: StatusAtExit) -> Bool {
// Strictly speaking, the C standard treats 0 as a successful exit code and
// potentially distinct from EXIT_SUCCESS. To my knowledge, no modern
// operating system defines EXIT_SUCCESS to any value other than 0, so the
// distinction is academic.
return switch (self._kind, statusAtExit) {
case let (.success, .exitCode(exitCode)):
exitCode == EXIT_SUCCESS
case let (.failure, .exitCode(exitCode)):
exitCode != EXIT_SUCCESS
case (.failure, .signal):
Expand Down
16 changes: 16 additions & 0 deletions Sources/Testing/ExitTests/StatusAtExit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,19 @@ public enum StatusAtExit: Sendable {
@available(*, unavailable, message: "Exit tests are not available on this platform.")
#endif
extension StatusAtExit: Equatable {}

// MARK: - CustomStringConvertible
@_spi(Experimental)
#if SWT_NO_PROCESS_SPAWNING
@available(*, unavailable, message: "Exit tests are not available on this platform.")
#endif
extension StatusAtExit: CustomStringConvertible {
public var description: String {
switch self {
case let .exitCode(exitCode):
".exitCode(\(exitCode))"
case let .signal(signal):
".signal(\(signal))"
}
}
}