Skip to content

Commit 5003569

Browse files
authored
Remove insecure setters (#214)
It is currently possible to override `fireAndForget`, `assert`, and `precondition`, but it is not safe to do so. You could accidentally introduce bad code into a release. Because these helpers are specifically for making test behavior predictable, we should not allow custom behavior that can leak into a live app.
1 parent 350e1e1 commit 5003569

File tree

3 files changed

+3
-49
lines changed

3 files changed

+3
-49
lines changed

Sources/Dependencies/DependencyValues/Assert.swift

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ extension DependencyValues {
2222
/// }
2323
/// ```
2424
public var assert: any AssertionEffect {
25-
get { self[AssertKey.self] }
26-
set { self[AssertKey.self] = newValue }
25+
self[AssertKey.self]
2726
}
2827

2928
/// A dependency for failing an assertion.
@@ -56,8 +55,7 @@ extension DependencyValues {
5655
/// }
5756
/// ```
5857
public var precondition: any AssertionEffect {
59-
get { self[PreconditionKey.self] }
60-
set { self[PreconditionKey.self] = newValue }
58+
self[PreconditionKey.self]
6159
}
6260
}
6361

@@ -164,34 +162,3 @@ private enum PreconditionKey: DependencyKey {
164162
public static let liveValue: any AssertionEffect = LivePreconditionEffect()
165163
public static let testValue: any AssertionEffect = TestAssertionEffect()
166164
}
167-
168-
/// An ``AssertionEffect`` that invokes the given closure.
169-
public struct AnyAssertionEffect: AssertionEffect {
170-
private let assert:
171-
@Sendable (
172-
_ condition: @autoclosure () -> Bool,
173-
_ message: @autoclosure () -> String,
174-
_ file: StaticString,
175-
_ line: UInt
176-
) -> Void
177-
178-
public init(
179-
_ assert: @escaping @Sendable (
180-
_ condition: @autoclosure () -> Bool,
181-
_ message: @autoclosure () -> String,
182-
_ file: StaticString,
183-
_ line: UInt
184-
) -> Void
185-
) {
186-
self.assert = assert
187-
}
188-
189-
public func callAsFunction(
190-
_ condition: @autoclosure () -> Bool,
191-
_ message: @autoclosure () -> String,
192-
file: StaticString,
193-
line: UInt
194-
) {
195-
self.assert(condition(), message(), file, line)
196-
}
197-
}

Sources/Dependencies/DependencyValues/FireAndForget.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ extension DependencyValues {
4545
/// Now this is easy to test. We just have to `await` for the code to finish, and once it does
4646
/// we can verify that the email was sent.
4747
public var fireAndForget: FireAndForget {
48-
get { self[FireAndForgetKey.self] }
49-
set { self[FireAndForgetKey.self] = newValue }
48+
self[FireAndForgetKey.self]
5049
}
5150
}
5251

Tests/DependenciesTests/AssertTests.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,4 @@ final class AssertTests: XCTestCase {
3838
}
3939
}
4040
#endif
41-
42-
func testCustom() {
43-
let expectation = self.expectation(description: "assert")
44-
withDependencies {
45-
$0.assert = AnyAssertionEffect { condition, message, file, line in
46-
expectation.fulfill()
47-
}
48-
} operation: {
49-
assert(true)
50-
self.wait(for: [expectation], timeout: 0)
51-
}
52-
}
5341
}

0 commit comments

Comments
 (0)