Skip to content

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Sep 22, 2025

This PR changes the semantics of Test.cancel() such that it only cancels the current test case if the current test is parameterized. This PR also removes Test.Case.cancel(), but we may opt to add Test.Case.cancelAll() or some such in the future to re-add an interface that cancels an entire parameterized test.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

This PR changes the semantics of `Test.cancel()` such that it only cancels the
current test case if the current test is parameterized. This PR also removes
`Test.Case.cancel()`, but we may opt to add `Test.Case.cancelAll()` or some such
in the future to re-add an interface that cancels an entire parameterized test.
@grynspan grynspan added this to the Swift 6.x (main) milestone Sep 22, 2025
@grynspan grynspan self-assigned this Sep 22, 2025
@grynspan grynspan added the enhancement New feature or request label Sep 22, 2025
@grynspan grynspan added the concurrency 🔀 Swift concurrency/sendability issues label Sep 22, 2025
@grynspan grynspan added the public-api Affects public API label Sep 22, 2025
configuration.eventHandler = { event, eventContext in
switch event.kind {
case .issueRecorded, .valueAttached, .testCancelled, .testCaseCancelled:
case .issueRecorded, .valueAttached, .testCancelled:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exit tests no longer need to be concerned with .testCaseCancelled events.

static func withCurrent<R>(_ test: Self, perform body: () async throws -> R) async rethrows -> R {
var runtimeState = Runner.RuntimeState.current ?? .init()
runtimeState.test = test
runtimeState.testCase = nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change looks out-of-place, but it ensures that when we set the current test, we clear the current test case. This has no effect on third-party tests but ensures when we run nested tests of our own that they don't get their states intermingled (which breaks Test.cancel()'s new semantics.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. This is probably something we'd probably want even if we'd never sought to add test cancellation

@grynspan grynspan merged commit 3a82994 into main Sep 23, 2025
24 checks passed
@grynspan grynspan deleted the jgrynspan/change-test.cancel-semantics branch September 23, 2025 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
concurrency 🔀 Swift concurrency/sendability issues enhancement New feature or request public-api Affects public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants