test(java): share Maven and Gradle parser failure assertions (#15888)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-02 11:03:41 +00:00
1 parent 2601a0f615
commit df439661d3
1 file changed
+22 -67
+22 -67
View File
@@ -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,
},
});
},