diff --git a/tests/boundary/filesystem/asarReaderCleanup.test.ts b/tests/boundary/filesystem/asarReaderCleanup.test.ts index 0c7ff30368..d75accbe61 100644 --- a/tests/boundary/filesystem/asarReaderCleanup.test.ts +++ b/tests/boundary/filesystem/asarReaderCleanup.test.ts @@ -1,4 +1,3 @@ -import { fstatSync } from "node:fs"; import { lstat, mkdir, @@ -29,7 +28,7 @@ const createArchive = async (content = CONTENT) => { return { archive }; }; -type OpenedHandle = { readonly path: string; readonly fd: number }; +type OpenedHandle = { readonly path: string; readonly handle: FileHandle }; type ReaderHooks = { readonly beforeAdoption?: (path: string, handle: FileHandle) => Promise; readonly close?: (handle: FileHandle) => Promise; @@ -40,7 +39,7 @@ const createTrackedReader = (archive: string, hooks: ReaderHooks = {}) => { const reader = new AsarArtifactReader(archive, { openFile: async (path, flags) => { const handle = await openFile(path, flags); - opened.push({ path, fd: handle.fd }); + opened.push({ path, handle }); await hooks.beforeAdoption?.(path, handle); return { get fd() { @@ -77,19 +76,22 @@ const openPackedMember = async ( return { output, handle, root: dirname(handle.path) }; }; -const expectDescriptorOpen = (fd: number): void => { - expect(fstatSync(fd).isFile()).toBe(true); +const expectDescriptorOpen = async ({ + handle, +}: OpenedHandle): Promise => { + expect((await handle.stat()).isFile()).toBe(true); }; -const expectDescriptorClosed = (fd: number): void => { - expect(() => fstatSync(fd)).toThrow(); +const expectDescriptorClosed = ({ handle }: OpenedHandle): void => { + // The OS can reassign the old descriptor number after close. + expect(handle.fd).toBe(-1); }; const expectSnapshotRemoved = async ( - fd: number, + descriptor: OpenedHandle, root: string, ): Promise => { - expectDescriptorClosed(fd); + expectDescriptorClosed(descriptor); await expect(lstat(root)).rejects.toMatchObject({ code: "ENOENT" }); }; @@ -105,12 +107,12 @@ describe("ASAR reader cleanup", () => { root, } = await openPackedMember(reader, opened); expect(memberHandle.path).toContain("rea-asar-snapshot-"); - expectDescriptorOpen(memberHandle.fd); + await expectDescriptorOpen(memberHandle); expect(output.destroyed).toBe(false); await reader.close(); - await expectSnapshotRemoved(memberHandle.fd, root); + await expectSnapshotRemoved(memberHandle, root); } finally { await reader.close(); } @@ -144,13 +146,13 @@ describe("ASAR reader cleanup", () => { await expect(reader.close()).rejects.toMatchObject({ cleanup: { resources: [memberHandle.path] }, }); - expectDescriptorOpen(memberHandle.fd); + await expectDescriptorOpen(memberHandle); expect((await lstat(root)).isDirectory()).toBe(true); expect(await packedMember(reader)).toMatchObject({ path: "main.js" }); await reader.close(); - await expectSnapshotRemoved(memberHandle.fd, root); + await expectSnapshotRemoved(memberHandle, root); } finally { await reader.close(); } @@ -185,7 +187,7 @@ describe("ASAR reader cleanup", () => { await closing; expect(output.destroyed).toBe(true); - await expectSnapshotRemoved(memberHandle.fd, root); + await expectSnapshotRemoved(memberHandle, root); }, [ memberOpen.release, async () => { @@ -234,10 +236,10 @@ describe("ASAR preparation cleanup", () => { resources: [handle.path], }, }); - expectDescriptorOpen(handle.fd); + await expectDescriptorOpen(handle); expect((await lstat(dirname(handle.path))).isDirectory()).toBe(true); await reader.close(); - await expectSnapshotRemoved(handle.fd, dirname(handle.path)); + await expectSnapshotRemoved(handle, dirname(handle.path)); } finally { await reader.close(); } @@ -270,10 +272,10 @@ describe("ASAR preparation cleanup", () => { }, }); const handle = latestHandle(opened); - expectDescriptorOpen(handle.fd); + await expectDescriptorOpen(handle); expect((await lstat(dirname(handle.path))).isDirectory()).toBe(true); await reader.close(); - await expectSnapshotRemoved(handle.fd, dirname(handle.path)); + await expectSnapshotRemoved(handle, dirname(handle.path)); } finally { await reader.close(); } @@ -297,7 +299,7 @@ it("forwards cancellation of an unconsumed packed member and closes its exact de cancellation: { reason: null }, }); await reader.close(); - expectDescriptorClosed(descriptor.fd); + expectDescriptorClosed(descriptor); } finally { await reader.close(); } @@ -345,7 +347,7 @@ it.each(["snapshot", "member"])( cause: failure, }); await reader.close(); - for (const handle of opened) expectDescriptorClosed(handle.fd); + for (const handle of opened) expectDescriptorClosed(handle); } finally { await reader.close(); }