Skip to content
Open
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
1 change: 1 addition & 0 deletions Sources/Testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ add_library(Testing
Traits/Tags/Tag+Macro.swift
Traits/Tags/Tag+Predefined.swift
Traits/TimeLimitTrait.swift
Traits/TaskLocalTrait.swift
Traits/Trait.swift)
target_link_libraries(Testing PRIVATE
_TestDiscovery
Expand Down
55 changes: 55 additions & 0 deletions Sources/Testing/Traits/TaskLocalTrait.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 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
//

extension Trait {
Comment thread
harlanhaskins marked this conversation as resolved.
/// Constructs a trait that binds a task local value for the duration of a test
/// or suite.
///
/// - Parameters:
/// - taskLocal: The task local to bind the value to.
/// - value: The value to bind to `taskLocal`.
///
/// This value will only be evaluated if the test this
/// trait is applied to runs, and it will be unbound from
/// the task local and released once the trait is finished
/// providing scope for the test.
///
/// - Note: You must define the task local outside the test target where the trait is used.
public static func taskLocal<Value: Sendable>(
_ taskLocal: TaskLocal<Value>,
_ value: @autoclosure @escaping @Sendable () throws -> Value
) -> Self
where Self == TaskLocalTrait<Value> {
Comment thread
grynspan marked this conversation as resolved.
TaskLocalTrait(taskLocal: taskLocal, value: value)
}
}

/// A type that that binds a task local value for the duration of a test or suite.
///
/// To add this trait to a test, use ``Trait/taskLocal(_:_:)``.
public struct TaskLocalTrait<Value: Sendable>: SuiteTrait, TestTrait, TestScoping {
/// This trait's task local.
fileprivate var taskLocal: TaskLocal<Value>

/// This trait's value.
fileprivate var value: @Sendable () throws -> Value

public var isRecursive: Bool { true }

public func provideScope(
for test: Test,
testCase: Test.Case?,
performing function: @Sendable () async throws -> Void
) async throws {
try await taskLocal.withValue(value()) {
try await function()
}
}
}
41 changes: 41 additions & 0 deletions Tests/TestingTests/Traits/TaskLocalTraitTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2026 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
//

@testable @_spi(Experimental) @_spi(ForToolsIntegrationOnly) import Testing

@Suite("Task Local Tests", .tags(.traitRelated))
struct TaskLocalTests {
@Test(
".taskLocal trait",
.taskLocal(local, true)
)
func taskLocalBinding() throws {
#expect(local.wrappedValue == true)
}

@Suite(.serialized, .taskLocal(stateLocal, State())) struct MutableLocal {
@Test func run1() {
#expect(stateLocal.wrappedValue.count == 0)
stateLocal.wrappedValue.count += 1
#expect(stateLocal.wrappedValue.count == 1)
}
@Test func run2() {
#expect(stateLocal.wrappedValue.count == 0)
stateLocal.wrappedValue.count += 1
#expect(stateLocal.wrappedValue.count == 1)
}
}
}

private let local = TaskLocal(wrappedValue: false)
private class State: @unchecked Sendable {
var count = 0
}
private let stateLocal = TaskLocal(wrappedValue: State())
Loading