diff --git a/Package.resolved b/Package.resolved index 8f5d4c31..0fb0d955 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "ac879199bc109c96e02f389573ce5b101fa5c8a274b809fc57dba0d4736f5b6f", + "originHash" : "dc8209f1497546c820fac828af8d595b6882cb88310f412367280d8cde593506", "pins" : [ { "identity" : "combine-schedulers", @@ -76,10 +76,10 @@ { "identity" : "xctest-dynamic-overlay", "kind" : "remoteSourceControl", - "location" : "https://github.com/pointfreeco/xctest-dynamic-overlay", + "location" : "https://github.com/jflan-dd/xctest-dynamic-overlay", "state" : { - "revision" : "27d767d643fa2cf083d0a73d74fa84cacb53e85c", - "version" : "1.4.1" + "branch" : "jflan/override-test-id", + "revision" : "09a97f20368a8c47b1fd86c4be9beb49585414ea" } } ], diff --git a/Package@swift-6.0.swift b/Package@swift-6.0.swift index 25dfe592..07cada9a 100644 --- a/Package@swift-6.0.swift +++ b/Package@swift-6.0.swift @@ -29,7 +29,7 @@ let package = Package( .package(url: "https://github.com/pointfreeco/combine-schedulers", from: "1.0.2"), .package(url: "https://github.com/pointfreeco/swift-clocks", from: "1.0.4"), .package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.0.0"), - .package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.4.0"), + .package(url: "https://github.com/jflan-dd/xctest-dynamic-overlay", branch: "jflan/override-test-id"), .package(url: "https://github.com/swiftlang/swift-syntax", "509.0.0"..<"601.0.0-prerelease"), ], targets: [ diff --git a/Sources/DependenciesTestSupport/TestTrait.swift b/Sources/DependenciesTestSupport/TestTrait.swift index b5a3f4a7..74c66eb8 100644 --- a/Sources/DependenciesTestSupport/TestTrait.swift +++ b/Sources/DependenciesTestSupport/TestTrait.swift @@ -59,8 +59,10 @@ public var isRecursive: Bool { true } public func prepare(for test: Test) async throws { - testValuesByTestID.withValue { - self.updateValues(&$0[test.id, default: DependencyValues(context: .test)]) + TestContext.withTestID(test.id) { + testValuesByTestID.withValue { values in + self.updateValues(&values[test.id, default: DependencyValues(context: .test)]) + } } } } diff --git a/Tests/DependenciesTests/SwiftTestingTests.swift b/Tests/DependenciesTests/SwiftTestingTests.swift index f4f2b8d8..dba34744 100644 --- a/Tests/DependenciesTests/SwiftTestingTests.swift +++ b/Tests/DependenciesTests/SwiftTestingTests.swift @@ -48,6 +48,24 @@ #endif } + static let updateValues: @Sendable (inout DependencyValues) -> Void = { + $0[ValueProvidingKey.self].setValue(5) + } + + @Test(.dependencies(updateValues)) + func cachedTraitUpdate() { + @Dependency(ValueProvidingKey.self) var provider + + #expect(provider.value == 5, "Updates in made in .dependencies closure update cached dependency") + } + + @Test + func cacheIsolatedBetweenTests() { + @Dependency(ValueProvidingKey.self) var provider + + #expect(provider.value == 0) + } + @Test(.dependency(\.date.now, Date(timeIntervalSinceReferenceDate: 0))) func trait() { @Dependency(\.date.now) var now @@ -92,4 +110,28 @@ } } } + + private protocol ValueProviding: Sendable { + var value: Int { get } + + func setValue(_ value: Int) + } + + private final class ValueProvidingMock: ValueProviding { + let _value: LockIsolated + + var value: Int { _value.value } + + init(value: Int) { + _value = .init(value) + } + + func setValue(_ value: Int) { + _value.setValue(value) + } + } + + private enum ValueProvidingKey: TestDependencyKey { + static var testValue: ValueProviding { ValueProvidingMock(value: 0) } + } #endif