diff --git a/src/drivers/ios/index-save-source-lifecycle.test.ts b/src/drivers/ios/index-save-source-lifecycle.test.ts index 1af924b2d..74be485a8 100644 --- a/src/drivers/ios/index-save-source-lifecycle.test.ts +++ b/src/drivers/ios/index-save-source-lifecycle.test.ts @@ -72,6 +72,38 @@ describe("IosSimctlDriver", () => { } as const; const cloneInvocation = simctl("clone", driverData.udid, baselineName, sourcesSet); + it("saves the real device plist when a CoreSimulator temporary plist vanishes during the scan", async () => { + const folder = `${sourcesSet}/${cloneUdid}`; + const temporary = "device.plist.sb-vanishing"; + const filesystem = new (class extends MemoryFilesystem { + override async lstat(path: string) { + if (path === `${folder}/${temporary}`) { + // why: CoreSimulator can finish an atomic write between readdir and lstat. + await this.rm(path); + } + return super.lstat(path); + } + })(); + await plantManagedDevice(filesystem); + await filesystem.mkdirp(folder); + const contents = `/Devices/${device.address}/data`; + await filesystem.writeFileAtomic(`${folder}/device.plist`, contents); + await filesystem.writeFileAtomic(`${folder}/${temporary}`, contents); + await filesystem.writeFileAtomic(`${folder}/device.plist.sb-in-progress`, contents); + const database = await memorySourceDatabase(); + const runner = new ScriptedProcessRunner([ + { match: listInvocation, result: { code: 0, stderr: "", stdout: listFixture } }, + { match: simctl("shutdown", driverData.udid) }, + { match: cloneInvocation, result: { code: 0, stderr: "", stdout: `${cloneUdid}\n` } }, + ]); + const driver = await createDriver(runner, new FakeClock(), filesystem, database); + + await expect(driver.saveSource(device)).resolves.toMatchObject({ sourceId: "device-1" }); + expect(database.prepare("SELECT relative_path FROM ios_source_files").all()).toEqual([ + { relative_path: "device.plist" }, + ]); + }); + it("keeps a shut-down copy of the device in the sources set as a ready baseline source with its disabled.plist, and a failing clone keeps no row", async () => { const database = await memorySourceDatabase(); const rowsAtShutdown: unknown[] = []; diff --git a/src/drivers/ios/lineage-files.ts b/src/drivers/ios/lineage-files.ts index c7cf6b485..e871e1d20 100644 --- a/src/drivers/ios/lineage-files.ts +++ b/src/drivers/ios/lineage-files.ts @@ -90,8 +90,7 @@ async function visitFolder( const path = `${directory}/${name}`; if ((await options.filesystem.lstat(path)).kind === "file") await flushWal(options, path); } - const names = (await options.filesystem.readdir(directory)).sort(); - for (const name of names) { + for (const name of await lineageEntryNames(options.filesystem, directory, relative === "")) { const path = `${directory}/${name}`; const relativePath = relative === "" ? name : `${relative}/${name}`; const details = await options.filesystem.lstat(path); @@ -104,6 +103,16 @@ async function visitFolder( } } +/** why: CoreSimulator's atomic device.plist writes can vanish before lstat or before a later clone. */ +async function lineageEntryNames( + filesystem: Filesystem, + directory: string, + sourceRoot: boolean, +): Promise { + const names = (await filesystem.readdir(directory)).sort(); + return sourceRoot ? names.filter((name) => !name.startsWith("device.plist.sb-")) : names; +} + /** why: a link is recorded when its target names the lineage; a WAL is never recorded; any other file is recorded when its bytes name it or cannot be read. */ async function recordedKind( options: RecordOptions,