From 9db36ae19d4379842f838eddb219414747d23cf7 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 6 Dec 2026 09:49:34 +0800 Subject: [PATCH] test(pyc): share bytecode artifact fixtures (#17352) --- src/python/PythonPycParser.test.ts | 62 ++++++++---------------------- 1 file changed, 15 insertions(+), 47 deletions(-) diff --git a/src/python/PythonPycParser.test.ts b/src/python/PythonPycParser.test.ts index 0d8cccc184..440c818f42 100644 --- a/src/python/PythonPycParser.test.ts +++ b/src/python/PythonPycParser.test.ts @@ -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 = {}) => - 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;