Skip to content

Fix prepareDependencies when using TestDependencyKey. #315

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

Merged
merged 3 commits into from
Dec 3, 2024
Merged
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
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),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do not need to warn if there is something in the cache that came from preparing dependencies.

!(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
Loading