Skip to content

Commit c81cb3d

Browse files
authored
Support throwing dependency trait (#320)
For example: ```swift @test(.dependency(\.defaultDatabase, try .create()) ```
1 parent a0f3ffd commit c81cb3d

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

Sources/Dependencies/Traits/TestTrait.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
public struct _DependenciesTrait: Sendable {
2-
package let updateValues: @Sendable (inout DependencyValues) -> Void
2+
package let updateValues: @Sendable (inout DependencyValues) throws -> Void
33

4-
package init(_ updateValues: @escaping @Sendable (inout DependencyValues) -> Void) {
4+
package init(_ updateValues: @escaping @Sendable (inout DependencyValues) throws -> Void) {
55
self.updateValues = updateValues
66
}
77

Sources/DependenciesTestSupport/TestTrait.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@
4343
/// - value: A dependency value to override for the test.
4444
public static func dependency<Value>(
4545
_ keyPath: WritableKeyPath<DependencyValues, Value> & Sendable,
46-
_ value: @autoclosure @escaping @Sendable () -> Value
46+
_ value: @autoclosure @escaping @Sendable () throws -> Value
4747
) -> Self {
4848
Self {
49-
$0[keyPath: keyPath] = value()
49+
$0[keyPath: keyPath] = try value()
5050
}
5151
}
5252

@@ -79,9 +79,9 @@
7979
/// - keyPath: A key path to a dependency value.
8080
/// - value: A dependency value to override for the test.
8181
public static func dependency<Value: TestDependencyKey>(
82-
_ value: @autoclosure @escaping @Sendable () -> Value
82+
_ value: @autoclosure @escaping @Sendable () throws -> Value
8383
) -> Self where Value == Value.Value {
84-
Self { $0[Value.self] = value() }
84+
Self { $0[Value.self] = try value() }
8585
}
8686

8787
/// A trait that overrides a test's or suite's dependencies.
@@ -96,8 +96,8 @@
9696
public var isRecursive: Bool { true }
9797

9898
public func prepare(for test: Test) async throws {
99-
testValuesByTestID.withValue {
100-
self.updateValues(&$0[test.id, default: DependencyValues(context: .test)])
99+
try testValuesByTestID.withValue {
100+
try self.updateValues(&$0[test.id, default: DependencyValues(context: .test)])
101101
}
102102
}
103103
}

0 commit comments

Comments
 (0)