Skip to content

Commit 732871f

Browse files
authored
Don't report errors as issues in testing (#144)
* Don't report errors as issues in testing There are better ways to assert against shared errors, but it is still handy to surface these in DEBUG builds. * fix
1 parent 813b8b5 commit 732871f

File tree

3 files changed

+29
-71
lines changed

3 files changed

+29
-71
lines changed

Sources/Sharing/Internal/Reference.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,11 @@ final class _PersistentReference<Key: SharedReaderKey>:
254254
withMutation(keyPath: \._loadError) {
255255
lock.withLock { _loadError = newValue }
256256
}
257-
if let newValue {
258-
reportIssue(newValue)
259-
}
257+
#if DEBUG
258+
if !isTesting, let newValue {
259+
reportIssue(newValue)
260+
}
261+
#endif
260262
}
261263
}
262264

@@ -371,9 +373,11 @@ extension _PersistentReference: MutableReference, Equatable where Key: SharedKey
371373
withMutation(keyPath: \._saveError) {
372374
lock.withLock { _saveError = newValue }
373375
}
374-
if let newValue {
375-
reportIssue(newValue)
376-
}
376+
#if DEBUG
377+
if !isTesting, let newValue {
378+
reportIssue(newValue)
379+
}
380+
#endif
377381
}
378382
}
379383

Tests/SharingTests/ErrorThrowingTests.swift

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import Testing
2020
}
2121
}
2222
@Shared(Key()) var value = 0
23-
await withKnownIssue {
24-
await #expect(throws: SaveError.self) {
25-
try await $value.save()
26-
}
23+
await #expect(throws: SaveError.self) {
24+
try await $value.save()
2725
}
2826
#expect($value.saveError is SaveError)
2927
}
@@ -42,12 +40,8 @@ import Testing
4240
struct LoadError: Error {}
4341
}
4442

45-
withKnownIssue {
46-
@SharedReader(Key()) var count = 0
47-
#expect($count.loadError != nil)
48-
} matching: {
49-
$0.description == "Caught error: LoadError()"
50-
}
43+
@SharedReader(Key()) var count = 0
44+
#expect($count.loadError != nil)
5145
}
5246

5347
@Test func userInitiatedLoadError() async {
@@ -71,12 +65,8 @@ import Testing
7165

7266
@SharedReader(Key()) var value = 0
7367

74-
await withKnownIssue {
75-
await #expect(throws: LoadError.self) {
76-
try await $value.load()
77-
}
78-
} matching: {
79-
$0.description == "Caught error: LoadError()"
68+
await #expect(throws: LoadError.self) {
69+
try await $value.load()
8070
}
8171
}
8272

@@ -117,11 +107,7 @@ import Testing
117107

118108
let key = Key()
119109
@SharedReader(key) var count = 0
120-
withKnownIssue {
121-
key.subscriber?.yield(throwing: Key.LoadError())
122-
} matching: {
123-
$0.description == "Caught error: LoadError()"
124-
}
110+
key.subscriber?.yield(throwing: Key.LoadError())
125111
#expect($count.loadError != nil)
126112

127113
key.subscriber?.yield(1)
@@ -147,11 +133,7 @@ import Testing
147133
}
148134

149135
@Shared(Key()) var count = 0
150-
withKnownIssue {
151-
$count.withLock { $0 -= 1 }
152-
} matching: {
153-
$0.description == "Caught error: SaveError()"
154-
}
136+
$count.withLock { $0 -= 1 }
155137
#expect($count.saveError != nil)
156138

157139
$count.withLock { $0 += 1 }
@@ -180,18 +162,10 @@ import Testing
180162

181163
let key = Key()
182164
@Shared(key) var count = 0
183-
withKnownIssue {
184-
key.subscriber?.yield(throwing: Key.LoadError())
185-
} matching: {
186-
$0.description == "Caught error: LoadError()"
187-
}
165+
key.subscriber?.yield(throwing: Key.LoadError())
188166
#expect($count.loadError != nil)
189167

190-
withKnownIssue {
191-
$count.withLock { $0 -= 1 }
192-
} matching: {
193-
$0.description == "Caught error: SaveError()"
194-
}
168+
$count.withLock { $0 -= 1 }
195169
#expect($count.loadError != nil)
196170
#expect($count.saveError != nil)
197171

@@ -200,11 +174,7 @@ import Testing
200174
#expect($count.loadError == nil)
201175
#expect($count.saveError != nil)
202176

203-
withKnownIssue {
204-
key.subscriber?.yield(throwing: Key.LoadError())
205-
} matching: {
206-
$0.description == "Caught error: LoadError()"
207-
}
177+
key.subscriber?.yield(throwing: Key.LoadError())
208178
#expect($count.loadError != nil)
209179
#expect($count.saveError != nil)
210180

@@ -233,9 +203,7 @@ import Testing
233203
#expect($count.loadError == nil)
234204

235205
struct LoadError: Error {}
236-
withKnownIssue {
237-
key.continuation.withLock { $0?.resume(throwing: LoadError()) }
238-
}
206+
key.continuation.withLock { $0?.resume(throwing: LoadError()) }
239207

240208
#expect(!$count.isLoading)
241209
#expect($count.loadError != nil)
@@ -277,9 +245,7 @@ import Testing
277245
$count.withLock { $0 += 1 }
278246

279247
struct SaveError: Error {}
280-
withKnownIssue {
281-
key.continuation.withLock { $0?.resume(throwing: SaveError()) }
282-
}
248+
key.continuation.withLock { $0?.resume(throwing: SaveError()) }
283249

284250
#expect($count.saveError != nil)
285251

Tests/SharingTests/FileStorageTests.swift

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,11 @@
136136
try withDependencies {
137137
$0.defaultFileStorage = .inMemory(fileSystem: fileSystem)
138138
} operation: {
139-
try withKnownIssue {
140-
@Shared(.fileStorage(.fileURL)) var users = [User]()
141-
let loadError = try #require($users.loadError)
142-
#expect(loadError is DecodingError)
143-
$users.withLock { $0.append(User(id: 1, name: "Blob")) }
144-
#expect($users.loadError == nil)
145-
} matching: {
146-
$0.error is DecodingError
147-
}
139+
@Shared(.fileStorage(.fileURL)) var users = [User]()
140+
let loadError = try #require($users.loadError)
141+
#expect(loadError is DecodingError)
142+
$users.withLock { $0.append(User(id: 1, name: "Blob")) }
143+
#expect($users.loadError == nil)
148144
}
149145
}
150146

@@ -418,15 +414,7 @@
418414
try? FileManager.default.removeItem(at: .fileURL)
419415
try Data("corrupted".utf8).write(to: .fileURL)
420416
@Shared(value: 0) var count: Int
421-
withKnownIssue {
422-
$count = Shared(wrappedValue: 0, .fileStorage(.fileURL))
423-
} matching: {
424-
$0.description.hasPrefix("""
425-
Caught error: dataCorrupted(Swift.DecodingError.Context(codingPath: [], \
426-
debugDescription: "The given data was not valid JSON.", underlyingError: \
427-
Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Unexpected character
428-
""")
429-
}
417+
$count = Shared(wrappedValue: 0, .fileStorage(.fileURL))
430418
#expect(count == 0)
431419
$count.withLock { $0 = 1 }
432420
try await Task.sleep(for: .seconds(0.01))

0 commit comments

Comments
 (0)