Skip to content

Commit

Permalink
Allow prepareDependencies to override context
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencelis committed Jan 9, 2025
1 parent 38c5ef0 commit 10187ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
10 changes: 9 additions & 1 deletion Sources/Dependencies/DependencyValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,15 @@ public struct DependencyValues: Sendable {
else {
let context =
self.storage[ObjectIdentifier(DependencyContextKey.self)] as? DependencyContext
?? defaultContext
?? self.cachedValues.value(
for: DependencyContextKey.self,
context: defaultContext,
fileID: fileID,
filePath: filePath,
function: function,
line: line,
column: column
)

switch context {
case .live, .preview:
Expand Down
23 changes: 10 additions & 13 deletions Tests/DependenciesTests/DependencyValuesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,16 @@ final class DependencyValuesTests: XCTestCase {
XCTAssertEqual(now, Date(timeIntervalSinceReferenceDate: 0))
}

func testPrepareDependencies_setsLiveContext() {
prepareDependencies {
$0.context = .live
}
@Dependency(\.context) var context
XCTAssertEqual(context, .live)
@Dependency(FullDependency.self) var client
XCTAssertEqual(client.value, FullDependency.liveValue.value)
}

func testPrepareDependencies_setsDependency_LiveContext() {
withDependencies {
$0.context = .live
Expand Down Expand Up @@ -844,19 +854,6 @@ final class DependencyValuesTests: XCTestCase {
}
#endif

#if DEBUG && !os(Linux) && !os(WASI) && !os(Windows)
func testPrepareDependencies_PrepareContext() {
prepareDependencies { $0.context = .live }

XCTTODO(
"""
Currently 'context' cannot be overridden with 'prepareDependencies'.
""")
@Dependency(\.date) var date
_ = date()
}
#endif

func testPrepareDependencies_setDependencyEndpoint() {
prepareDependencies {
$0[ClientWithEndpoint.self].get = { @Sendable in 42 }
Expand Down

0 comments on commit 10187ba

Please sign in to comment.