test(pyinstaller): reuse archive reader fixtures (#14879)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-11-27 09:52:04 +00:00
1 parent f1ca91efd3
commit e70ca8d02b
6 files changed
+66 -84

No files matched your search

@@ -1,7 +1,7 @@
import { appendFile, lstat, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { appendFile, lstat } from "node:fs/promises";
import { expect, it, vi } from "vitest";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
import { collectEntries } from "../../../tests/fixtures/artifactEntries.js";
import { createTestTempFile } from "../../../tests/fixtures/temporaryDirectory.js";
import { withOwnedRegularFile } from "../../filesystem/OwnedRegularFile.js";
import type { OwnedFileHandle } from "../../filesystem/OwnedFileHandle.js";
import { ArtifactEmbeddedFormatProbes } from "../ArtifactEmbeddedFormatProbes.js";
@@ -42,13 +42,10 @@ it.each([
] as const)(
"retains $carrier carrier dialect at offset $offset through member projection",
async ({ carrier, offset, dialect, path: memberPath }) => {
const root = await createTestTempDirectory(
"rea-pyinstaller-carrier-admission-",
);
const path = join(root, "app");
const sourceName = String.raw`pkg\data.bin`;
await writeFile(
path,
const path = await createTestTempFile(
"rea-pyinstaller-carrier-admission-",
"app",
cArchiveExecutable({
carrier,
...(offset === undefined ? {} : { peHeaderOffset: offset }),
@@ -87,8 +84,7 @@ it.each([
if (classified.digest === null)
throw new Error("Missing carrier digest");
await reader.prepareContainer(classified.digest.sha256);
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
expect(entries).toEqual([
expect.objectContaining({
path: memberPath,
@@ -122,10 +118,6 @@ it.each([
);
it("preserves backslashes when a standalone archive has no recognized carrier", async () => {
const root = await createTestTempDirectory(
"rea-pyinstaller-unknown-carrier-",
);
const path = join(root, "app");
const sourceName = String.raw`pkg\data.bin`;
const bytes = cArchiveExecutable({
layout: "2.1",
@@ -140,11 +132,14 @@ it("preserves backslashes when a standalone archive has no recognized carrier",
],
});
bytes.fill(0, 0, 64);
await writeFile(path, bytes);
const path = await createTestTempFile(
"rea-pyinstaller-unknown-carrier-",
"app",
bytes,
);
const reader = new PyInstallerArtifactReader(path);
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
expect(entries.map((entry) => entry.path)).toEqual([sourceName]);
} finally {
await reader.close();
@@ -152,11 +147,11 @@ it("preserves backslashes when a standalone archive has no recognized carrier",
});
it("inventories members using the cookie discovered by root classification", async () => {
const root = await createTestTempDirectory(
const path = await createTestTempFile(
"rea-pyinstaller-cookie-inventory-",
"app",
fixture(),
);
const path = join(root, "app");
await writeFile(path, fixture());
const locate = vi.spyOn(layout, "observePyInstallerCookie");
const resourceScope = new ArtifactResourceScope();
try {
@@ -179,9 +174,11 @@ it("inventories members using the cookie discovered by root classification", asy
});
it("rejects changed source before consuming an admitted cookie and leaves the borrowed descriptor open", async () => {
const root = await createTestTempDirectory("rea-pyinstaller-cookie-change-");
const path = join(root, "app");
await writeFile(path, fixture());
const path = await createTestTempFile(
"rea-pyinstaller-cookie-change-",
"app",
fixture(),
);
await withOwnedRegularFile(path, {}, async (source) => {
const cookie = await new ArtifactEmbeddedFormatProbes(
path,
@@ -212,9 +209,11 @@ it("rejects changed source before consuming an admitted cookie and leaves the bo
});
it("retains completed cookie facts across unchanged admissions and honors cached cancellation", async () => {
const root = await createTestTempDirectory("rea-pyinstaller-cookie-cache-");
const path = join(root, "app");
await writeFile(path, fixture());
const path = await createTestTempFile(
"rea-pyinstaller-cookie-cache-",
"app",
fixture(),
);
const locate = vi.spyOn(layout, "observePyInstallerCookie");
try {
await withOwnedRegularFile(path, {}, async (source) => {
@@ -245,10 +244,9 @@ it("retains completed cookie facts across unchanged admissions and honors cached
});
it("retains rejected magic candidates without classifying their native carrier as PyInstaller", async () => {
const root = await createTestTempDirectory("rea-pyinstaller-candidate-");
const path = join(root, "app");
await writeFile(
path,
const path = await createTestTempFile(
"rea-pyinstaller-candidate-",
"app",
Buffer.concat([
fixture().subarray(0, 64),
CARCHIVE_COOKIE_MAGIC,
@@ -285,12 +283,9 @@ it("retains rejected magic candidates without classifying their native carrier a
});
it("continues past an invalid trailing magic copy to inventory the valid cookie", async () => {
const root = await createTestTempDirectory(
const path = await createTestTempFile(
"rea-pyinstaller-candidate-valid-",
);
const path = join(root, "app");
await writeFile(
path,
"app",
Buffer.concat([fixture(), CARCHIVE_COOKIE_MAGIC, Buffer.alloc(88)]),
);
const resourceScope = new ArtifactResourceScope();
@@ -312,9 +307,11 @@ it("continues past an invalid trailing magic copy to inventory the valid cookie"
});
it("does not cache an incomplete tail read as an absent cookie and can retry", async () => {
const root = await createTestTempDirectory("rea-pyinstaller-tail-short-");
const path = join(root, "app");
await writeFile(path, fixture());
const path = await createTestTempFile(
"rea-pyinstaller-tail-short-",
"app",
fixture(),
);
await withOwnedRegularFile(path, {}, async (source) => {
const probes = new ArtifactEmbeddedFormatProbes(path, source);
const read = source.handle.read.bind(source.handle);
@@ -1,5 +1,6 @@
import type { FileHandle } from "node:fs/promises";
import { expect, it, vi } from "vitest";
import { collectEntries } from "../../../tests/fixtures/artifactEntries.js";
import { withPyInstallerReader } from "../../../tests/fixtures/pyinstallerReader.js";
import {
controlledCleanup,
@@ -54,8 +55,7 @@ it("accepts the private-image capacity and retires all image owners", async () =
const spool = vi.spyOn(PyInstallerPyzStore.prototype, "spool");
try {
await withPyInstallerReader(compressedImages(128), async (reader, root) => {
const listed: ArtifactEntry[] = [];
for await (const entry of reader.entries()) listed.push(entry);
const listed = await collectEntries(reader);
expect(listed).toHaveLength(128);
expect(spool).toHaveBeenCalledTimes(128);
await reader.close();
@@ -1,5 +1,5 @@
import { sha256 } from "../../../tests/support/sha256.js";
import { open, readFile, stat, writeFile } from "node:fs/promises";
import { open, readFile, stat } from "node:fs/promises";
import { join } from "node:path";
import { buffer } from "node:stream/consumers";
@@ -7,6 +7,7 @@ import { expect, it, vi } from "vitest";
import { ArtifactReaderFailure } from "../../resources/ArtifactReader.js";
import { OwnedFileHandle } from "../../filesystem/OwnedFileHandle.js";
import { collectEntries } from "../../../tests/fixtures/artifactEntries.js";
import {
materializeArtifactInventory,
scanCanonicalArtifactInventory,
@@ -33,9 +34,9 @@ const PYTHON_313_MAGIC = Buffer.from("f30d0d0a", "hex");
it.each(["close", "cancel"] as const)(
"does not complete a PyInstaller inventory after %s while paused",
async (interruption) => {
const path = join(await createTestTempDirectory("rea-pyi-paused-"), "app");
await writeFile(
path,
const path = await createTestTempFile(
"rea-pyi-paused-",
"app",
cArchiveExecutable({
layout: "2.1",
pythonVersion: 311,
@@ -134,8 +135,7 @@ it("preserves legacy pyc bytes and derives only modern headerless CArchive bytec
const path = await writeArchive("bytecode-layouts", fixture.archive);
const reader = new PyInstallerArtifactReader(path);
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
for (const module of fixture.modules) {
const entry = entries.find((item) => item.path === module.name)!;
expect(entry.formatMetadata).toMatchObject({
@@ -1085,8 +1085,7 @@ it("reads a PE signature beyond the carrier prefix before projecting paths", asy
);
const reader = new PyInstallerArtifactReader(path);
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
expect(entries.map((entry) => entry.path)).toContain("pkg/data.bin");
} finally {
await reader.close();
@@ -1118,8 +1117,7 @@ it("requires DOS magic before a PE-like record can select Windows path semantics
await writeArchive("carrier", bytes),
);
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
expect(entries).toContainEqual(
expect.objectContaining({
path: expectedPath,
@@ -1285,8 +1283,7 @@ it("opens selected PYZ modules lazily and permits repeat reads without retaining
);
const reader = new PyInstallerArtifactReader(path);
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
const selected = entries.find(
(entry) => entry.path === "PYZ.pyz/selected.pyc",
);
@@ -1333,8 +1330,7 @@ it.each([false, true])(
);
const reader = new PyInstallerArtifactReader(path);
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
const selected = entries.find(
(entry) => entry.path === "PYZ.pyz/selected.pyc",
);
@@ -1629,8 +1625,7 @@ it.each([
);
const reader = new PyInstallerArtifactReader(path);
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
const framed = entries.find((entry) => entry.path === "framed");
if (framed === undefined) throw new Error("Missing framed module");
expect(framed.formatMetadata).toMatchObject({
@@ -1708,8 +1703,7 @@ it("keeps mixed CArchive roles distinct across declarations, bytecode, and store
const path = await writeArchive("mixed-roles", fixture.archive);
const reader = new PyInstallerArtifactReader(path);
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
expect(reader.formatMetadata()).toMatchObject({
entry_count: 9,
runtime_option_count: 1,
@@ -1,10 +1,10 @@
import { withPyInstallerReader } from "../../../tests/fixtures/pyinstallerReader.js";
import { awaitAcknowledgement } from "../../../tests/fixtures/lifecycleAcknowledgement.js";
import { dirname, join } from "node:path";
import { dirname } from "node:path";
import { getEventListeners, once } from "node:events";
import { Readable } from "node:stream";
import { finished } from "node:stream/promises";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
import { createTestTempFile } from "../../../tests/fixtures/temporaryDirectory.js";
import { randomBytes } from "node:crypto";
import { sha256 as hash } from "../../../tests/support/sha256.js";
import { rename, unlink, writeFile, type FileHandle } from "node:fs/promises";
@@ -414,9 +414,9 @@ it.each(["success", "io", "caller cancellation", "reader close"] as const)(
it.each([false, true])(
"settles independent private and carrier closes and retries only failed owners (carrier failure: %s)",
async (failCarrier) => {
const path = join(await createTestTempDirectory("rea-pyi-cleanup-"), "app");
await writeFile(
path,
const path = await createTestTempFile(
"rea-pyi-cleanup-",
"app",
cArchiveExecutable({
layout: "2.1",
pythonVersion: 309,
@@ -4,6 +4,7 @@ import { dirname, join } from "node:path";
import { deflateSync } from "node:zlib";
import { randomBytes } from "node:crypto";
import { describe, expect, it, vi } from "vitest";
import { collectEntries } from "../../../tests/fixtures/artifactEntries.js";
import { withPyInstallerReader } from "../../../tests/fixtures/pyinstallerReader.js";
import {
scanCanonicalArtifactInventory,
@@ -122,8 +123,7 @@ it.each([0, 1, 2])(
for (let row = 0; row < index; row++)
record += original.readUInt32BE(record);
await withPyInstallerReader(original, async (reader) => {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
expect(entries).toHaveLength(3);
});
const malformed = Buffer.from(original);
@@ -169,8 +169,7 @@ it.each([
],
}),
async (reader) => {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
const module = entries.find((entry) => entry.path === "module")!;
expect(module.formatMetadata).toMatchObject({
bytecode_header: "unknown",
@@ -197,8 +196,7 @@ it("fills short descriptor reads when distinguishing both CArchive module layout
return Reflect.apply(read, this, args);
});
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
for (const module of fixture.modules) {
const entry = entries.find((item) => item.path === module.name)!;
expect(await buffer(await reader.open(entry))).toEqual(module.bytes);
@@ -238,8 +236,7 @@ it.each(["unused compressed input", "wrong declared size"])(
],
}),
async (reader) => {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
const module = entries.find((entry) => entry.path === "module")!;
expect(module.formatMetadata).toMatchObject({
bytecode_header: "derived",
@@ -371,8 +368,7 @@ it.each(["magic", "marshal"])(
members: [{ name: "PYZ.pyz", typeCode: "z", payload, compress: true }],
}),
async (reader, carrier) => {
const entries: ArtifactEntry[] = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
const entry = entries[0]!;
expect(entry.kind).toBe("file");
const read = vi
@@ -959,8 +955,7 @@ it.each([false, true])(
],
}),
async (reader, _handle, path) => {
const entries: ArtifactEntry[] = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
const inventory = await scanCanonicalArtifactInventory(path);
for (const [logicalPath, code, bytecode] of [
["PYZ.pyz/app/mod.pyc", 0, true],
@@ -1305,8 +1300,7 @@ it("retains multipackage references as distinct unresolved declarations", async
})),
}),
async (reader) => {
const entries: ArtifactEntry[] = [];
for await (const entry of reader.entries()) entries.push(entry);
const entries = await collectEntries(reader);
expect(reader.formatMetadata()).toMatchObject({
external_dependency_count: names.length,
runtime_option_count: 0,
+3 -6
View File
@@ -1,7 +1,6 @@
import { open, writeFile, type FileHandle } from "node:fs/promises";
import { join } from "node:path";
import { open, type FileHandle } from "node:fs/promises";
import { PyInstallerArtifactReader } from "../../src/artifacts/PyInstallerArtifactReader.js";
import { createTestTempDirectory } from "./temporaryDirectory.js";
import { createTestTempFile } from "./temporaryDirectory.js";
export const withPyInstallerReader = async (
bytes: Buffer,
@@ -11,9 +10,7 @@ export const withPyInstallerReader = async (
path: string,
) => Promise<void>,
) => {
const root = await createTestTempDirectory("rea-pyi-boundary-");
const path = join(root, "app");
await writeFile(path, bytes);
const path = await createTestTempFile("rea-pyi-boundary-", "app", bytes);
const handle = await open(path, "r");
const reader = new PyInstallerArtifactReader(path, {
handle,