1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//
/// An event that occurred during testing.
@_spi(ForToolsIntegrationOnly)
public struct Event: Sendable {
/// An enumeration describing the various kinds of event that can be observed.
public enum Kind: Sendable {
/// A test was discovered during test run planning.
///
/// This event is recorded once per discovered test when ``Runner/run()`` is
/// called. It does not indicate whether or not a test will run or be
/// skipped—only that the test was found by the testing library and is part
/// of the runner's plan.
///
/// This event is also posted once per test when `swift test list` is
/// called. In that case, events are posted for all discovered tests
/// regardless of whether or not they would run.
case testDiscovered
/// A test run started.
///
/// This event is posted when ``Runner/run()`` is called after
/// ``testDiscovered`` has been posted for all tests in the runner's plan.
case runStarted
/// An iteration of the test run started.
///
/// - Parameters:
/// - index: The index of the iteration. The first iteration has an index
/// of `0`.
///
/// This event is posted at the start of each test plan iteration.
///
/// By default, a test plan runs for one iteration, but the
/// ``Configuration/repetitionPolicy-swift.property`` property can be set to
/// allow for more iterations.
indirect case iterationStarted(_ index: Int)
/// A step in the runner plan started.
///
/// - Parameters:
/// - step: The step in the runner plan which started.
///
/// This event is posted when a ``Runner`` begins processing a
/// ``Runner/Plan/Step``. Processing this step may result in its associated
/// ``Test`` being run, skipped, or another action, so this event will only
/// be followed by a ``testStarted`` event if the step's test is run.
indirect case planStepStarted(_ step: Runner.Plan.Step)
/// A test started.
///
/// The test that started is contained in the ``Event/Context`` instance
/// that was passed to the event handler along with this event. Its ID is
/// available from this event's ``Event/testID`` property.
case testStarted
/// A test case started.
///
/// The test case that started is contained in the ``Event/Context``
/// instance that was passed to the event handler along with this event.
case testCaseStarted
/// A test case ended.
///
/// The test case that ended is contained in the ``Event/Context`` instance
/// that was passed to the event handler along with this event.
case testCaseEnded
/// An expectation was checked with `#expect()` or `#require()`.
///
/// - Parameters:
/// - expectation: The expectation which was checked.
///
/// By default, events of this kind are not generated because they occur
/// frequently in a typical test run and can generate significant
/// backpressure on the event handler.
///
/// Failed expectations also, unless expected to fail, generate events of
/// kind ``Event/Kind-swift.enum/issueRecorded(_:)``. Those events are
/// always posted to the current event handler.
///
/// To enable events of this kind, set
/// ``Configuration/deliverExpectationCheckedEvents`` to `true` before
/// running tests.
indirect case expectationChecked(_ expectation: Expectation)
/// An issue was recorded.
///
/// - Parameters:
/// - issue: The issue which was recorded.
indirect case issueRecorded(_ issue: Issue)
/// A test ended.
///
/// The test that ended is contained in the ``Event/Context`` instance that
/// was passed to the event handler along with this event. Its ID is
/// available from this event's ``Event/testID`` property.
case testEnded
/// A test was skipped.
///
/// - Parameters:
/// - skipInfo: A ``SkipInfo`` containing details about this skipped test.
///
/// The test that was skipped is contained in the ``Event/Context`` instance
/// that was passed to the event handler along with this event. Its ID is
/// available from this event's ``Event/testID`` property.
indirect case testSkipped(_ skipInfo: SkipInfo)
/// A step in the runner plan ended.
///
/// - Parameters:
/// - step: The step in the runner plan which ended.
///
/// This event is posted when a ``Runner`` finishes processing a
/// ``Runner/Plan/Step``.
indirect case planStepEnded(Runner.Plan.Step)
/// An iteration of the test run ended.
///
/// - Parameters:
/// - index: The index of the iteration. The first iteration has an index
/// of `0`.
///
/// This event is posted at the end of each test plan iteration.
///
/// By default, a test plan runs for one iteration, but the
/// ``Configuration/repetitionPolicy-swift.property`` property can be set to
/// allow for more iterations.
indirect case iterationEnded(_ index: Int)
/// A test run ended.
///
/// This event is the last event posted before ``Runner/run()`` returns.
case runEnded
}
/// The kind of event.
public var kind: Kind
/// The ID of the test for which this event occurred, if any.
///
/// If an event occurred independently of any test, or if the running test
/// cannot be determined, the value of this property is `nil`.
public var testID: Test.ID?
/// The ID of the test case for which this event occurred, if any.
///
/// If an event occurred independently of any test case, or if the running
/// test case cannot be determined, the value of this property is `nil`.
public var testCaseID: Test.Case.ID?
/// The instant at which the event occurred.
public var instant: Test.Clock.Instant
/// Initialize an instance of this type.
///
/// - Parameters:
/// - kind: The kind of event that occurred.
/// - testID: The ID of the test for which the event occurred, if any.
/// - testCaseID: The ID of the test case for which the event occurred, if
/// any.
/// - instant: The instant at which the event occurred. The default value
/// of this argument is `.now`.
///
/// When creating an event to be posted, use
/// ``post(_:for:testCase:instant:configuration)`` instead since that ensures
/// any task local-derived values in the associated ``Event/Context`` match
/// the event.
init(_ kind: Kind, testID: Test.ID?, testCaseID: Test.Case.ID?, instant: Test.Clock.Instant = .now) {
self.kind = kind
self.testID = testID
self.testCaseID = testCaseID
self.instant = instant
}
/// Post an ``Event`` with the specified values.
///
/// - Parameters:
/// - kind: The kind of event that occurred.
/// - test: The test for which the event occurred, if any.
/// - testCase: The test case for which the event occurred, if any.
/// - instant: The instant at which the event occurred. The default value
/// of this argument is `.now`.
/// - configuration: The configuration whose event handler should handle
/// this event. If `nil` is passed, the current task's configuration is
/// used, if known.
static func post(
_ kind: Kind,
for test: Test? = .current,
testCase: Test.Case? = .current,
instant: Test.Clock.Instant = .now,
configuration: Configuration? = nil
) {
// Create both the event and its associated context here at same point, to
// ensure their task local-derived values are the same.
let event = Event(kind, testID: test?.id, testCaseID: testCase?.id, instant: instant)
let context = Event.Context(test: test, testCase: testCase)
event._post(in: context, configuration: configuration)
}
}
// MARK: - Event handling
extension Event {
/// A function that handles events that occur while tests are running.
///
/// - Parameters:
/// - event: An event that needs to be handled.
/// - context: The context associated with the event.
public typealias Handler = @Sendable (_ event: borrowing Event, _ context: borrowing Context) -> Void
/// A type which provides context about a posted ``Event``.
///
/// An instance of this type is provided along with each ``Event`` that is
/// passed to an ``Event/Handler``.
public struct Context: Sendable {
/// The test for which this instance's associated ``Event`` occurred, if
/// any.
///
/// If an event occurred independently of any test, or if the running test
/// cannot be determined, the value of this property is `nil`.
public var test: Test?
/// The test case for which this instance's associated ``Event`` occurred,
/// if any.
///
/// The test case indicates which element in the iterated sequence is
/// associated with this event. For non-parameterized tests, a single test
/// case is synthesized. For test suite types (as opposed to test
/// functions), the value of this property is `nil`.
public var testCase: Test.Case?
/// Initialize a new instance of this type.
///
/// - Parameters:
/// - test: The test for which this instance's associated event occurred,
/// if any.
/// - testCase: The test case for which this instance's associated event
/// occurred, if any.
init(test: Test? = .current, testCase: Test.Case? = .current) {
self.test = test
self.testCase = testCase
}
}
/// Post this event to the currently-installed event handler.
///
/// - Parameters:
/// - context: The context associated with this event.
/// - configuration: The configuration whose event handler should handle
/// this event. If `nil` is passed, the current task's configuration is
/// used, if known.
///
/// Prefer using this function over invoking event handlers directly. If
/// `configuration` is not `nil`, `self` is passed to its
/// ``Configuration/eventHandler`` property. If `configuration` is `nil`, and
/// ``Configuration/current`` is _not_ `nil`, its event handler is used
/// instead. If there is no current configuration, the event is posted to
/// the event handlers of all configurations set as current across all tasks
/// in the process.
private borrowing func _post(in context: borrowing Context, configuration: Configuration? = nil) {
if let configuration = configuration ?? Configuration.current {
// The caller specified a configuration, or the current task has an
// associated configuration. Post to either configuration's event handler.
switch kind {
case .expectationChecked where !configuration.deliverExpectationCheckedEvents:
break
default:
configuration.handleEvent(self, in: context)
}
} else {
// The current task does NOT have an associated configuration. This event
// will be lost! Post it to every registered event handler to avoid that.
for configuration in Configuration.all {
_post(in: context, configuration: configuration)
}
}
}
}
#if !SWT_NO_SNAPSHOT_TYPES
// MARK: - Snapshotting
extension Event {
/// A serializable event that occurred during testing.
@_spi(ForToolsIntegrationOnly)
public struct Snapshot: Sendable, Codable {
/// The kind of event.
public var kind: Kind.Snapshot
/// The ID of the test for which this event occurred, if any.
///
/// If an event occurred independently of any test, or if the running test
/// cannot be determined, the value of this property is `nil`.
public var testID: Test.ID?
/// The ID of the test case for which this event occurred, if any.
///
/// If an event occurred independently of any test case, or if the running
/// test case cannot be determined, the value of this property is `nil`.
public var testCaseID: Test.Case.ID?
/// The instant at which the event occurred.
public var instant: Test.Clock.Instant
/// Snapshots an ``Event``.
///
/// - Parameters:
/// - event: The original ``Event`` to snapshot.
public init(snapshotting event: borrowing Event) {
kind = Event.Kind.Snapshot(snapshotting: event.kind)
testID = event.testID
testCaseID = event.testCaseID
instant = event.instant
}
}
}
extension Event.Kind {
/// A serializable enumeration describing the various kinds of event that can be observed.
public enum Snapshot: Sendable, Codable {
/// A test was discovered during test run planning.
///
/// This event is recorded once per discovered test when ``Runner/run()`` is
/// called. It does not indicate whether or not a test will run or be
/// skipped—only that the test was found by the testing library and is part
/// of the runner's plan.
///
/// This event is also posted once per test when `swift test list` is
/// called. In that case, events are posted for all discovered tests
/// regardless of whether or not they would run.
case testDiscovered
/// A test run started.
///
/// This event is posted when ``Runner/run()`` is called after
/// ``testDiscovered`` has been posted for all tests in the runner's plan.
case runStarted
/// An iteration of the test run started.
///
/// - Parameters:
/// - index: The index of the iteration. The first iteration has an index
/// of `0`.
///
/// This event is posted at the start of each test plan iteration.
///
/// By default, a test plan runs for one iteration, but the
/// ``Configuration/repetitionPolicy-swift.property`` property can be set to
/// allow for more iterations.
indirect case iterationStarted(_ index: Int)
/// A step in the runner plan started.
///
/// - Parameters:
/// - step: The step in the runner plan which started.
///
/// This is posted when a ``Runner`` begins processing a
/// ``Runner/Plan/Step``. Processing this step may result in its associated
/// ``Test`` being run, skipped, or another action, so this event will only
/// be followed by a ``testStarted`` event if the step's test is run.
case planStepStarted
/// A test started.
case testStarted
/// A test case started.
case testCaseStarted
/// A test case ended.
case testCaseEnded
/// An expectation was checked with `#expect()` or `#require()`.
///
/// - Parameters:
/// - expectation: The expectation which was checked.
///
/// By default, events of this kind are not generated because they occur
/// frequently in a typical test run and can generate significant
/// backpressure on the event handler.
///
/// Failed expectations also, unless expected to fail, generate events of
/// kind ``Event/Kind-swift.enum/issueRecorded(_:)``. Those events are
/// always posted to the current event handler.
///
/// To enable events of this kind, set
/// ``Configuration/deliverExpectationCheckedEvents`` to `true` before
/// running tests.
indirect case expectationChecked(_ expectation: Expectation.Snapshot)
/// An issue was recorded.
///
/// - Parameters:
/// - issue: The issue which was recorded.
indirect case issueRecorded(_ issue: Issue.Snapshot)
/// A test ended.
case testEnded
/// A test was skipped.
///
/// - Parameters:
/// - skipInfo: A ``SkipInfo`` containing details about this skipped test.
indirect case testSkipped(_ skipInfo: SkipInfo)
/// A step in the runner plan ended.
///
/// - Parameters:
/// - step: The step in the runner plan which ended.
///
/// This is posted when a ``Runner`` finishes processing a
/// ``Runner/Plan/Step``.
case planStepEnded
/// An iteration of the test run ended.
///
/// - Parameters:
/// - index: The index of the iteration. The first iteration has an index
/// of `0`.
///
/// This event is posted at the end of each test plan iteration.
///
/// By default, a test plan runs for one iteration, but the
/// ``Configuration/repetitionPolicy-swift.property`` property can be set to
/// allow for more iterations.
indirect case iterationEnded(_ index: Int)
/// A test run ended.
///
/// This is the last event posted before ``Runner/run()`` returns.
case runEnded
/// Snapshots an ``Event.Kind``.
/// - Parameter kind: The original ``Event.Kind`` to snapshot.
public init(snapshotting kind: Event.Kind) {
switch kind {
case .testDiscovered:
self = .testDiscovered
case .runStarted:
self = .runStarted
case let .iterationStarted(index):
self = .iterationStarted(index)
case .planStepStarted:
self = .planStepStarted
case .testStarted:
self = .testStarted
case .testCaseStarted:
self = .testCaseStarted
case .testCaseEnded:
self = .testCaseEnded
case let .expectationChecked(expectation):
let expectationSnapshot = Expectation.Snapshot(snapshotting: expectation)
self = Snapshot.expectationChecked(expectationSnapshot)
case let .issueRecorded(issue):
self = .issueRecorded(Issue.Snapshot(snapshotting: issue))
case .testEnded:
self = .testEnded
case let .testSkipped(skipInfo):
self = .testSkipped(skipInfo)
case .planStepEnded:
self = .planStepEnded
case let .iterationEnded(index):
self = .iterationEnded(index)
case .runEnded:
self = .runEnded
}
}
}
}
extension Event.Context {
/// A serializable type which provides context about a posted ``Event``.
///
@_spi(ForToolsIntegrationOnly)
public struct Snapshot: Sendable, Codable {
/// A snapshot of the test for which this instance's associated ``Event``
/// occurred, if any.
///
/// If an event occurred independently of any test, or if the running test
/// cannot be determined, the value of this property is `nil`.
public var test: Test.Snapshot?
/// A snapshot of the test case for which this instance's associated
/// ``Event`` occurred, if any.
///
/// The test case indicates which element in the iterated sequence is
/// associated with this event. For non-parameterized tests, a single test
/// case is synthesized. For test suite types (as opposed to test
/// functions), the value of this property is `nil`.
public var testCase: Test.Case.Snapshot?
/// Initialize a new instance of this type.
///
/// - Parameters:
/// - context: The context to snapshot.
public init(snapshotting context: borrowing Event.Context) {
test = context.test.map { Test.Snapshot(snapshotting: $0) }
testCase = context.testCase.map { Test.Case.Snapshot(snapshotting: $0) }
}
}
}
#endif
|