Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/drivers/ios/index-save-source-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down
13 changes: 11 additions & 2 deletions src/drivers/ios/lineage-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<string[]> {
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,
Expand Down