Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override Test ID to ensure trait dependencies are cached #293

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "ac879199bc109c96e02f389573ce5b101fa5c8a274b809fc57dba0d4736f5b6f",
"originHash" : "dc8209f1497546c820fac828af8d595b6882cb88310f412367280d8cde593506",
"pins" : [
{
"identity" : "combine-schedulers",
Expand Down Expand Up @@ -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"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
6 changes: 4 additions & 2 deletions Sources/DependenciesTestSupport/TestTrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
}
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions Tests/DependenciesTests/SwiftTestingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -92,4 +110,28 @@
}
}
}

private protocol ValueProviding: Sendable {
var value: Int { get }

func setValue(_ value: Int)
}

private final class ValueProvidingMock: ValueProviding {
let _value: LockIsolated<Int>

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