10
10
11
11
private import _TestingInternals
12
12
13
- @_spi ( Experimental)
14
13
#if SWT_NO_EXIT_TESTS
15
14
@available ( * , unavailable, message: " Exit tests are not available on this platform. " )
16
15
#endif
@@ -19,13 +18,29 @@ extension ExitTest {
19
18
///
20
19
/// Values of this type are used to describe the conditions under which an
21
20
/// exit test is expected to pass or fail by passing them to
22
- /// ``expect(exitsWith:observing:_:sourceLocation:performing:)`` or
23
- /// ``require(exitsWith:observing:_:sourceLocation:performing:)``.
21
+ /// ``expect(processExitsWith:observing:_:sourceLocation:performing:)`` or
22
+ /// ``require(processExitsWith:observing:_:sourceLocation:performing:)``.
23
+ ///
24
+ /// ## Topics
25
+ ///
26
+ /// ### Successful exit conditions
27
+ ///
28
+ /// - ``success``
29
+ ///
30
+ /// ### Failing exit conditions
31
+ ///
32
+ /// - ``failure``
33
+ /// - ``exitCode(_:)``
34
+ /// - ``signal(_:)``
35
+ ///
36
+ /// @Metadata {
37
+ /// @Available(Swift, introduced: 6.2)
38
+ /// }
24
39
public struct Condition : Sendable {
25
40
/// An enumeration describing the possible conditions for an exit test.
26
41
private enum _Kind : Sendable , Equatable {
27
42
/// The exit test must exit with a particular exit status.
28
- case statusAtExit ( StatusAtExit )
43
+ case exitStatus ( ExitStatus )
29
44
30
45
/// The exit test must exit successfully.
31
46
case success
@@ -41,49 +56,77 @@ extension ExitTest {
41
56
42
57
// MARK: -
43
58
44
- @_spi ( Experimental)
45
59
#if SWT_NO_EXIT_TESTS
46
60
@available ( * , unavailable, message: " Exit tests are not available on this platform. " )
47
61
#endif
48
62
extension ExitTest . Condition {
49
- /// A condition that matches when a process terminates successfully with exit
50
- /// code `EXIT_SUCCESS`.
63
+ /// A condition that matches when a process exits normally.
64
+ ///
65
+ /// This condition matches the exit code `EXIT_SUCCESS`.
66
+ ///
67
+ /// @Metadata {
68
+ /// @Available(Swift, introduced: 6.2)
69
+ /// }
51
70
public static var success : Self {
52
71
Self ( _kind: . success)
53
72
}
54
73
55
- /// A condition that matches when a process terminates abnormally with any
56
- /// exit code other than `EXIT_SUCCESS` or with any signal.
74
+ /// A condition that matches when a process exits abnormally
75
+ ///
76
+ /// This condition matches any exit code other than `EXIT_SUCCESS` or any
77
+ /// signal that causes the process to exit.
78
+ ///
79
+ /// @Metadata {
80
+ /// @Available(Swift, introduced: 6.2)
81
+ /// }
57
82
public static var failure : Self {
58
83
Self ( _kind: . failure)
59
84
}
60
85
61
- public init ( _ statusAtExit: StatusAtExit ) {
62
- self . init ( _kind: . statusAtExit( statusAtExit) )
86
+ /// Initialize an instance of this type that matches the specified exit
87
+ /// status.
88
+ ///
89
+ /// - Parameters:
90
+ /// - exitStatus: The particular exit status this condition should match.
91
+ ///
92
+ /// @Metadata {
93
+ /// @Available(Swift, introduced: 6.2)
94
+ /// }
95
+ public init ( _ exitStatus: ExitStatus ) {
96
+ self . init ( _kind: . exitStatus( exitStatus) )
63
97
}
64
98
65
99
/// Creates a condition that matches when a process terminates with a given
66
100
/// exit code.
67
101
///
68
102
/// - Parameters:
69
- /// - exitCode: The exit code yielded by the process.
103
+ /// - exitCode: The exit code reported by the process.
70
104
///
71
- /// The C programming language defines two [ standard exit codes](https://en.cppreference.com/w/c/program/EXIT_status),
72
- /// `EXIT_SUCCESS` and `EXIT_FAILURE`. Platforms may additionally define their
73
- /// own non-standard exit codes:
105
+ /// The C programming language defines two standard exit codes, `EXIT_SUCCESS`
106
+ /// and `EXIT_FAILURE`. Platforms may additionally define their own
107
+ /// non-standard exit codes:
74
108
///
75
109
/// | Platform | Header |
76
110
/// |-|-|
77
111
/// | macOS | [`<stdlib.h>`](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/_Exit.3.html), [`<sysexits.h>`](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/sysexits.3.html) |
78
- /// | Linux | [`<stdlib.h>`](https://sourceware. org/glibc/manual/latest/html_node/Exit-Status. html), `<sysexits.h>` |
112
+ /// | Linux | [`<stdlib.h>`](https://www.kernel. org/doc/man-pages/online/pages/man3/exit.3. html), [ `<sysexits.h>`](https://www.kernel.org/doc/man-pages/online/pages/man3/sysexits.h.3head.html) |
79
113
/// | FreeBSD | [`<stdlib.h>`](https://man.freebsd.org/cgi/man.cgi?exit(3)), [`<sysexits.h>`](https://man.freebsd.org/cgi/man.cgi?sysexits(3)) |
80
114
/// | OpenBSD | [`<stdlib.h>`](https://man.openbsd.org/exit.3), [`<sysexits.h>`](https://man.openbsd.org/sysexits.3) |
81
115
/// | Windows | [`<stdlib.h>`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/exit-success-exit-failure) |
82
116
///
117
+ /// @Comment {
118
+ /// See https://en.cppreference.com/w/c/program/EXIT_status for more
119
+ /// information about exit codes defined by the C standard.
120
+ /// }
121
+ ///
83
122
/// On macOS, FreeBSD, OpenBSD, and Windows, the full exit code reported by
84
- /// the process is yielded to the parent process. Linux and other POSIX-like
123
+ /// the process is reported to the parent process. Linux and other POSIX-like
85
124
/// systems may only reliably report the low unsigned 8 bits (0–255) of
86
125
/// the exit code.
126
+ ///
127
+ /// @Metadata {
128
+ /// @Available(Swift, introduced: 6.2)
129
+ /// }
87
130
public static func exitCode( _ exitCode: CInt ) -> Self {
88
131
#if !SWT_NO_EXIT_TESTS
89
132
Self ( . exitCode( exitCode) )
@@ -92,22 +135,30 @@ extension ExitTest.Condition {
92
135
#endif
93
136
}
94
137
95
- /// Creates a condition that matches when a process terminates with a given
96
- /// signal.
138
+ /// Creates a condition that matches when a process exits with a given signal.
97
139
///
98
140
/// - Parameters:
99
- /// - signal: The signal that terminated the process.
141
+ /// - signal: The signal that caused the process to exit .
100
142
///
101
- /// The C programming language defines a number of [ standard signals](https://en.cppreference.com/w/c/program/SIG_types).
102
- /// Platforms may additionally define their own non-standard signal codes:
143
+ /// The C programming language defines a number of standard signals. Platforms
144
+ /// may additionally define their own non-standard signal codes:
103
145
///
104
146
/// | Platform | Header |
105
147
/// |-|-|
106
148
/// | macOS | [`<signal.h>`](https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/signal.3.html) |
107
- /// | Linux | [`<signal.h>`](https://sourceware. org/glibc/manual/latest/html_node/Standard-Signals .html) |
149
+ /// | Linux | [`<signal.h>`](https://www.kernel. org/doc/man-pages/online/pages/man7/signal.7 .html) |
108
150
/// | FreeBSD | [`<signal.h>`](https://man.freebsd.org/cgi/man.cgi?signal(3)) |
109
151
/// | OpenBSD | [`<signal.h>`](https://man.openbsd.org/signal.3) |
110
152
/// | Windows | [`<signal.h>`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/signal-constants) |
153
+ ///
154
+ /// @Comment {
155
+ /// See https://en.cppreference.com/w/c/program/SIG_types for more
156
+ /// information about signals defined by the C standard.
157
+ /// }
158
+ ///
159
+ /// @Metadata {
160
+ /// @Available(Swift, introduced: 6.2)
161
+ /// }
111
162
public static func signal( _ signal: CInt ) -> Self {
112
163
#if !SWT_NO_EXIT_TESTS
113
164
Self ( . signal( signal) )
@@ -131,8 +182,8 @@ extension ExitTest.Condition: CustomStringConvertible {
131
182
" .failure "
132
183
case . success:
133
184
" .success "
134
- case let . statusAtExit ( statusAtExit ) :
135
- String ( describing: statusAtExit )
185
+ case let . exitStatus ( exitStatus ) :
186
+ String ( describing: exitStatus )
136
187
}
137
188
#else
138
189
fatalError ( " Unsupported " )
@@ -149,19 +200,19 @@ extension ExitTest.Condition {
149
200
/// Check whether or not an exit test condition matches a given exit status.
150
201
///
151
202
/// - Parameters:
152
- /// - statusAtExit : An exit status to compare against.
203
+ /// - exitStatus : An exit status to compare against.
153
204
///
154
- /// - Returns: Whether or not `self` and `statusAtExit ` represent the same
155
- /// exit condition.
205
+ /// - Returns: Whether or not `self` and `exitStatus ` represent the same exit
206
+ /// condition.
156
207
///
157
208
/// Two exit test conditions can be compared; if either instance is equal to
158
209
/// ``failure``, it will compare equal to any instance except ``success``.
159
- func isApproximatelyEqual( to statusAtExit : StatusAtExit ) -> Bool {
210
+ func isApproximatelyEqual( to exitStatus : ExitStatus ) -> Bool {
160
211
// Strictly speaking, the C standard treats 0 as a successful exit code and
161
212
// potentially distinct from EXIT_SUCCESS. To my knowledge, no modern
162
213
// operating system defines EXIT_SUCCESS to any value other than 0, so the
163
214
// distinction is academic.
164
- return switch ( self . _kind, statusAtExit ) {
215
+ return switch ( self . _kind, exitStatus ) {
165
216
case let ( . success, . exitCode( exitCode) ) :
166
217
exitCode == EXIT_SUCCESS
167
218
case let ( . failure, . exitCode( exitCode) ) :
@@ -170,7 +221,7 @@ extension ExitTest.Condition {
170
221
// All terminating signals are considered failures.
171
222
true
172
223
default :
173
- self . _kind == . statusAtExit ( statusAtExit )
224
+ self . _kind == . exitStatus ( exitStatus )
174
225
}
175
226
}
176
227
}
0 commit comments