diff --git a/Sources/Testing/ExitTests/ExitTest.Condition.swift b/Sources/Testing/ExitTests/ExitTest.Condition.swift index 10f2a6ff0..d2c637d79 100644 --- a/Sources/Testing/ExitTests/ExitTest.Condition.swift +++ b/Sources/Testing/ExitTests/ExitTest.Condition.swift @@ -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 } @@ -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 @@ -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 @@ -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): diff --git a/Sources/Testing/ExitTests/StatusAtExit.swift b/Sources/Testing/ExitTests/StatusAtExit.swift index 26514ffa5..ea5e287c7 100644 --- a/Sources/Testing/ExitTests/StatusAtExit.swift +++ b/Sources/Testing/ExitTests/StatusAtExit.swift @@ -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))" + } + } +}