Skip to content

Commit

Permalink
Fix prepareDependencies when using TestDependencyKey. (#315)
Browse files Browse the repository at this point in the history
* Fix prepareDependencies when using TestDependencyKey.

* wip

* wip
  • Loading branch information
mbrandonw authored Dec 3, 2024
1 parent 7d2eb4a commit 5526c8a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/Dependencies/DependencyValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,12 @@ public final class CachedValues: @unchecked Sendable {
return withIssueContext(fileID: fileID, filePath: filePath, line: line, column: column) {
let cacheKey = CacheKey(id: TypeIdentifier(key), context: context)
#if DEBUG
if context == .live, !DependencyValues.isSetting, !(key is any DependencyKey.Type) {
if
context == .live,
!DependencyValues.isSetting,
!(cached[cacheKey] != nil && cached[cacheKey]?.preparationID != nil),
!(key is any DependencyKey.Type)
{
reportIssue(
{
var dependencyDescription = ""
Expand Down
12 changes: 12 additions & 0 deletions Tests/DependenciesTests/DependencyValuesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,18 @@ final class DependencyValuesTests: XCTestCase {
XCTAssertEqual(now, Date(timeIntervalSinceReferenceDate: 0))
}

func testPrepareDependencies_setsDependency_LiveContext() {
withDependencies {
$0.context = .live
} operation: {
prepareDependencies {
$0[ClientWithEndpoint.self] = ClientWithEndpoint(get: { 1729 })
}
@Dependency(ClientWithEndpoint.self) var client
XCTAssertEqual(client.get(), 1729)
}
}

#if DEBUG && !os(Linux) && !os(WASI) && !os(Windows)
func testPrepareDependencies_MultiplePreparesWithNoAccessBetween() {
prepareDependencies {
Expand Down

0 comments on commit 5526c8a

Please sign in to comment.