From df439661d371b75fb25afa58282bcb9d86876048 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 2 Dec 2026 19:03:41 +0800 Subject: [PATCH] test(java): share Maven and Gradle parser failure assertions (#15888) --- src/java/MavenGradleMetadataParser.test.ts | 89 ++++++---------------- 1 file changed, 22 insertions(+), 67 deletions(-) diff --git a/src/java/MavenGradleMetadataParser.test.ts b/src/java/MavenGradleMetadataParser.test.ts index 601e664dfbf..950fd919265 100644 --- a/src/java/MavenGradleMetadataParser.test.ts +++ b/src/java/MavenGradleMetadataParser.test.ts @@ -52,20 +52,21 @@ it("preserves an opaque XML counting failure without allocating a DOM", () => { const expectFailure = ( source: string | Uint8Array, - kind: "malformed" | "unsupported", -) => { + kind: "malformed" | "unsupported" | "limit", + path = "/artifacts/selected", +): ParseFailureError => { const bytes = typeof source === "string" ? Buffer.from(source, "utf8") : source; try { parseMavenGradleMetadata(bytes, { - path: "/artifacts/selected", + path, sha256: createHash("sha256").update(bytes).digest("hex"), bytes: bytes.length, }); } catch (cause) { if (!(cause instanceof ParseFailureError)) throw cause; expect(cause.failure.kind).toBe(kind); - return; + return cause; } throw new Error(`Expected metadata to be ${kind}`); }; @@ -794,19 +795,11 @@ it("refuses malformed Gradle JSON", () => { it("keeps malformed Gradle JSON source out of the failure message", () => { const bytes = Buffer.from('{\n "url": //user:secret@repo.example/x}', "utf8"); - let failure: unknown; - try { - parseMavenGradleMetadata(bytes, { - path: "/artifacts/demo.module", - sha256: createHash("sha256").update(bytes).digest("hex"), - bytes: bytes.length, - }); - } catch (cause) { - failure = cause; - } - expect(failure).toBeInstanceOf(ParseFailureError); - expect(failure).toMatchObject({ failure: { kind: "malformed" } }); - const { message } = failure as ParseFailureError; + const { message } = expectFailure( + bytes, + "malformed", + "/artifacts/demo.module", + ); expect(message).not.toContain("user:secret"); expect(message).not.toContain("repo.example"); expect(message).not.toContain('"url"'); @@ -841,19 +834,11 @@ it("refuses Gradle metadata with a repeated field instead of keeping the last", it("keeps a repeated Gradle field name out of the failure message", () => { const source = `{"formatVersion":"1.1","https://user:secret@repo.example/x":1,"https://user:secret@repo.example/x":2,"component":{"group":"com.example","module":"demo","version":"1.0.0"},"variants":[]}`; const bytes = Buffer.from(source, "utf8"); - let failure: unknown; - try { - parseMavenGradleMetadata(bytes, { - path: "/artifacts/demo.module", - sha256: createHash("sha256").update(bytes).digest("hex"), - bytes: bytes.length, - }); - } catch (cause) { - failure = cause; - } - expect(failure).toBeInstanceOf(ParseFailureError); - expect(failure).toMatchObject({ failure: { kind: "malformed" } }); - const { message } = failure as ParseFailureError; + const { message } = expectFailure( + bytes, + "malformed", + "/artifacts/demo.module", + ); expect(message).not.toContain("user:secret"); expect(message).not.toContain("repo.example"); }); @@ -1023,23 +1008,8 @@ it.for([ ] as const)( "refuses %s rows past the report allowance before building them", ([, source]) => { - const bytes = Buffer.from(source, "utf8"); - let failure: unknown; - try { - parseMavenGradleMetadata(bytes, { - path: "/artifacts/selected", - sha256: createHash("sha256").update(bytes).digest("hex"), - bytes: bytes.length, - }); - } catch (cause) { - failure = cause; - } - expect(failure).toBeInstanceOf(ParseFailureError); - expect(failure).toMatchObject({ - failure: { - kind: "limit", - limit: { name: "dependency_rows", maximum: 65_536, required: 65_537 }, - }, + expect(expectFailure(source, "limit").failure).toMatchObject({ + limit: { name: "dependency_rows", maximum: 65_536, required: 65_537 }, }); }, ); @@ -1060,26 +1030,11 @@ it.for([ ] as const)( "refuses %s past the report allowance before building them", ([, source, expected]) => { - const bytes = Buffer.from(source, "utf8"); - let failure: unknown; - try { - parseMavenGradleMetadata(bytes, { - path: "/artifacts/selected", - sha256: createHash("sha256").update(bytes).digest("hex"), - bytes: bytes.length, - }); - } catch (cause) { - failure = cause; - } - expect(failure).toBeInstanceOf(ParseFailureError); - expect(failure).toMatchObject({ - failure: { - kind: "limit", - limit: { - name: expected.allowance, - maximum: expected.limit, - required: expected.required, - }, + expect(expectFailure(source, "limit").failure).toMatchObject({ + limit: { + name: expected.allowance, + maximum: expected.limit, + required: expected.required, }, }); },