Skip to content

Commit a8e1570

Browse files
authored
File storage: migrate stub data to empty files (#164)
* Remove testable import * File storage: migrate stub data to empty files Old clients may have stub files still around, so let's migrate them instead of emit errors. We can consider removing this migration logic at a later time.
1 parent 75e846e commit a8e1570

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

Sources/Sharing/Internal/Deprecations.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
#if canImport(Foundation)
2+
import Foundation
3+
#endif
4+
5+
// NB: Deprecated after 2.5.2
6+
7+
#if canImport(Foundation)
8+
@available(iOS, deprecated: 9999, message: "This will be removed in Sharing 3.")
9+
@available(macOS, deprecated: 9999, message: "This will be removed in Sharing 3.")
10+
@available(tvOS, deprecated: 9999, message: "This will be removed in Sharing 3.")
11+
@available(watchOS, deprecated: 9999, message: "This will be removed in Sharing 3.")
12+
extension Data {
13+
package static let stub = Self("co.pointfree.Sharing.FileStorage.stub".utf8)
14+
}
15+
#endif
16+
117
// NB: Deprecated after 2.2.0
218

319
extension SharedReader {

Sources/Sharing/SharedKeys/FileStorageKey.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,12 @@
375375
}
376376
},
377377
load: { url in
378-
try Data(contentsOf: url)
378+
var data = try Data(contentsOf: url)
379+
if data == .stub {
380+
data = Data()
381+
try data.write(to: url, options: .atomic)
382+
}
383+
return data
379384
},
380385
save: { data, url in
381386
try data.write(to: url, options: .atomic)

Tests/SharingTests/FileStorageTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,16 @@
408408
try Data().write(to: .fileURL)
409409
@Shared(.fileStorage(.fileURL)) var count = 0
410410
#expect(count == 0)
411+
#expect($count.loadError == nil)
412+
}
413+
414+
@Test func stubData() throws {
415+
try? FileManager.default.removeItem(at: .fileURL)
416+
try Data.stub.write(to: .fileURL)
417+
@Shared(.fileStorage(.fileURL)) var count = 0
418+
#expect(count == 0)
419+
#expect($count.loadError == nil)
420+
#expect(try Data(contentsOf: .fileURL).isEmpty)
411421
}
412422

413423
@Test func corruptData() async throws {

Tests/SharingTests/PublisherTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Combine
33
import Dependencies
44
import Foundation
5-
@testable import Sharing
5+
import Sharing
66
import Testing
77

88
@MainActor

0 commit comments

Comments
 (0)