File tree Expand file tree Collapse file tree 4 files changed +33
-3
lines changed Expand file tree Collapse file tree 4 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -22,7 +22,6 @@ public final class HeartbeatController {
22
22
private let heartbeatsStorageCapacity : Int = 30
23
23
/// Current date provider. It is used for testability.
24
24
private let dateProvider : ( ) -> Date
25
- // TODO: Maybe share config with HeartbeatsPayload's DateFormatter?
26
25
/// Used for standardizing dates for calendar-day comparision.
27
26
static let dateStandardizer : ( Date ) -> ( Date ) = {
28
27
var calendar = Calendar ( identifier: . iso8601)
Original file line number Diff line number Diff line change @@ -90,7 +90,9 @@ final class HeartbeatStorage: HeartbeatStorageProtocol {
90
90
91
91
// MARK: - HeartbeatStorageProtocol
92
92
93
- // TODO: Document.
93
+ /// Synchronously reads from and writes to storage using the given transform block.
94
+ /// - Parameter transform: A block to transform the currently stored heartbeats bundle to a new
95
+ /// heartbeats bundle value.
94
96
func readAndWriteSync( using transform: ( HeartbeatsBundle ? ) -> HeartbeatsBundle ? ) {
95
97
queue. sync {
96
98
let oldHeartbeatsBundle = try ? load ( from: storage)
Original file line number Diff line number Diff line change @@ -32,7 +32,10 @@ extension FileStorage: StorageFactory {
32
32
static func makeStorage( id: String ) -> Storage {
33
33
let rootDirectory = FileManager . default. applicationSupportDirectory
34
34
let heartbeatDirectoryPath = Constants . heartbeatFileStorageDirectoryPath
35
- let heartbeatFilePath = " heartbeats- \( id) "
35
+
36
+ // Sanitize the `id` so the heartbeat file name does not include a ":".
37
+ let sanitizedID = id. replacingOccurrences ( of: " : " , with: " _ " )
38
+ let heartbeatFilePath = " heartbeats- \( sanitizedID) "
36
39
37
40
let storageURL = rootDirectory
38
41
. appendingPathComponent ( heartbeatDirectoryPath, isDirectory: true )
Original file line number Diff line number Diff line change @@ -364,4 +364,30 @@ class HeartbeatLoggingIntegrationTests: XCTestCase {
364
364
XCTAssertNotNil ( try Data ( contentsOf: heartbeatsFileURL) , " Data should not be nil. " )
365
365
#endif
366
366
}
367
+
368
+ #if !os(tvOS)
369
+ // Do not run on tvOS because tvOS uses UserDefaults to store heartbeats.
370
+ func testControllerCreatesHeartbeatStorageWithSanitizedFileName( ) throws {
371
+ // Given
372
+ let appID = " 1:123456789000:ios:abcdefghijklmnop "
373
+ let sanitizedAppID = appID. replacingOccurrences ( of: " : " , with: " _ " )
374
+ let controller = HeartbeatController ( id: appID)
375
+ // When
376
+ // - Trigger the controller to write to the file system.
377
+ controller. log ( " dummy_agent " )
378
+ _ = XCTWaiter . wait ( for: [ expectation ( description: " Wait for async log. " ) ] , timeout: 0.1 )
379
+ // Then
380
+ let heartbeatsDirectoryURL = FileManager . default
381
+ . applicationSupportDirectory
382
+ . appendingPathComponent (
383
+ HeartbeatLoggingTestUtils . Constants. heartbeatFileStorageDirectoryPath,
384
+ isDirectory: true
385
+ )
386
+
387
+ let directoryContents = try FileManager . default
388
+ . contentsOfDirectory ( atPath: heartbeatsDirectoryURL. path)
389
+
390
+ XCTAssertEqual ( directoryContents, [ " heartbeats- \( sanitizedAppID) " ] )
391
+ }
392
+ #endif // !os(tvOS)
367
393
}
You can’t perform that action at this time.
0 commit comments