test(pyc): share bytecode artifact fixtures (#17352)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-06 09:49:34 +08:00
1 parent a016d88d50
commit 9db36ae19d
1 file changed
+15 -47
+15 -47
View File
@@ -36,6 +36,12 @@ const defaults = inspectPythonPycInputSchema.parse({
path: "/artifacts/sample.pyc",
});
const pycArtifact = (bytes: Buffer) => ({
path: defaults.path,
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.length,
});
const root = (report: PythonPyc) => {
const code = report.code_objects[0];
if (code === undefined) throw new Error("Report lists no code object");
@@ -43,16 +49,7 @@ const root = (report: PythonPyc) => {
};
const parse = (bytes: Buffer, work?: PythonPycWork) =>
parsePythonPyc(
bytes,
{
path: defaults.path,
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.length,
},
defaults,
work,
);
parsePythonPyc(bytes, pycArtifact(bytes), defaults, work);
/** The report and the work the parser counted while building it. */
const measure = (bytes: Buffer) => {
@@ -550,15 +547,10 @@ it("bounds names repeated through TYPE_REF by what the report carries", () => {
]);
const bytes = pythonPycWithCode(3531, "py311", { namesObject });
expect(() =>
parsePythonPyc(
bytes,
{
path: defaults.path,
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.length,
},
{ ...defaults, max_file_bytes: bytes.length },
),
parsePythonPyc(bytes, pycArtifact(bytes), {
...defaults,
max_file_bytes: bytes.length,
}),
).toThrow(parserLimit("reported_string_bytes"));
});
@@ -766,11 +758,7 @@ it("rejects constant tables whose copies exceed max_objects", () => {
),
],
});
const artifact = {
path: defaults.path,
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.length,
};
const artifact = pycArtifact(bytes);
const allowed = parsePythonPyc(bytes, artifact, defaults);
const entries = allowed.code_objects.reduce(
(sum, code) => sum + code.consts.length,
@@ -837,15 +825,7 @@ const textComplex = (real: string, imag: string): Buffer =>
]);
const parseLimited = (bytes: Buffer, max_file_bytes: number) =>
parsePythonPyc(
bytes,
{
path: defaults.path,
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.length,
},
{ ...defaults, max_file_bytes },
);
parsePythonPyc(bytes, pycArtifact(bytes), { ...defaults, max_file_bytes });
it("charges every reported code-constant name against the file budget", () => {
const name = Buffer.alloc(2000, 0x6e);
@@ -1211,11 +1191,7 @@ it("lists a shared container's code objects once per owner and charges each copy
);
expect(relationships).toBe(owners + owners);
expect(constants).toBeGreaterThan(report.coverage.objects);
const artifact = {
path: defaults.path,
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.length,
};
const artifact = pycArtifact(bytes);
expect(() =>
parsePythonPyc(bytes, artifact, { ...defaults, max_objects: constants }),
).toThrow(parserLimit("reported_rows"));
@@ -1396,15 +1372,7 @@ it("walks a wide dictionary once per child instead of copying it each step", ()
});
const parseWith = (bytes: Buffer, limits: Partial<typeof defaults> = {}) =>
parsePythonPyc(
bytes,
{
path: defaults.path,
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.length,
},
{ ...defaults, ...limits },
);
parsePythonPyc(bytes, pycArtifact(bytes), { ...defaults, ...limits });
it("preflights expanded shared code-name keys before building their hex table", () => {
const width = 1000;