@@ -137,7 +137,10 @@ class StorageTests: XCTestCase {
137
137
}
138
138
139
139
func testFilePrepAndFinish( ) {
140
- let analytics = Analytics ( configuration: Configuration ( writeKey: " test " ) )
140
+ let config = Configuration ( writeKey: " test " )
141
+ . storageMode ( . diskAtURL( URL ( fileURLWithPath: NSTemporaryDirectory ( ) ) ) )
142
+ let analytics = Analytics ( configuration: config)
143
+
141
144
analytics. storage. hardReset ( doYouKnowHowToUseThis: true )
142
145
143
146
analytics. waitUntilStarted ( )
@@ -302,4 +305,45 @@ class StorageTests: XCTestCase {
302
305
let remaining = analytics. storage. read ( . events)
303
306
XCTAssertNil ( remaining)
304
307
}
308
+
309
+ func testMigrationFromOldLocation( ) {
310
+ let writeKey = " test-migration "
311
+ let fm = FileManager . default
312
+
313
+ // Clean slate
314
+ let appSupportURL = fm. urls ( for: . applicationSupportDirectory, in: . userDomainMask) [ 0 ]
315
+ let newSegmentDir = appSupportURL. appendingPathComponent ( " segment " )
316
+ try ? fm. removeItem ( at: newSegmentDir)
317
+
318
+ // Create fake old data in the platform-specific old location
319
+ #if (os(iOS) || os(watchOS)) && !targetEnvironment(macCatalyst)
320
+ let oldSearchPath = FileManager . SearchPathDirectory. documentDirectory
321
+ #else
322
+ let oldSearchPath = FileManager . SearchPathDirectory. cachesDirectory
323
+ #endif
324
+
325
+ let oldBaseURL = fm. urls ( for: oldSearchPath, in: . userDomainMask) [ 0 ]
326
+ let oldSegmentDir = oldBaseURL. appendingPathComponent ( " segment/ \( writeKey) " )
327
+ try ! fm. createDirectory ( at: oldSegmentDir, withIntermediateDirectories: true , attributes: nil )
328
+
329
+ // Write some fake event files
330
+ let testFile1 = oldSegmentDir. appendingPathComponent ( " 0-segment-events.temp " )
331
+ let testFile2 = oldSegmentDir. appendingPathComponent ( " 1-segment-events.temp " )
332
+ try ! " fake event data 1 " . write ( to: testFile1, atomically: true , encoding: . utf8)
333
+ try ! " fake event data 2 " . write ( to: testFile2, atomically: true , encoding: . utf8)
334
+
335
+ // Trigger migration
336
+ let resultURL = eventStorageDirectory ( writeKey: writeKey)
337
+
338
+ // Verify migration worked
339
+ XCTAssertTrue ( fm. fileExists ( atPath: resultURL. path) )
340
+ XCTAssertTrue ( fm. fileExists ( atPath: resultURL. appendingPathComponent ( " 0-segment-events.temp " ) . path) )
341
+ XCTAssertTrue ( fm. fileExists ( atPath: resultURL. appendingPathComponent ( " 1-segment-events.temp " ) . path) )
342
+
343
+ // Verify old directory is gone
344
+ XCTAssertFalse ( fm. fileExists ( atPath: oldSegmentDir. path) )
345
+
346
+ // Clean up
347
+ try ? fm. removeItem ( at: newSegmentDir)
348
+ }
305
349
}
0 commit comments