diff --git a/scripts/generate-artifact-schemas.ts b/scripts/generate-artifact-schemas.ts index 333f25e1..ed8d89f6 100644 --- a/scripts/generate-artifact-schemas.ts +++ b/scripts/generate-artifact-schemas.ts @@ -11,7 +11,10 @@ import { registrySnapshotArtifactSchema, supplyChainVerificationArtifactSchema, } from "./contracts/release-artifacts.ts"; -import { writeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts"; +import { + type ValidatedJsonArtifactInput, + writeValidatedJsonArtifact, +} from "./lib/validated-json-artifact.ts"; const ARTIFACT_SCHEMA_DEFINITIONS = Object.freeze([ { @@ -39,6 +42,13 @@ const ARTIFACT_SCHEMA_DEFINITIONS = Object.freeze([ type GenerateArtifactSchemasOptions = Readonly<{ root?: string; check?: boolean; + /** + * Publishes one schema document. The default writes durably (fsync of the + * file and its directory). A test that only checks the generated bytes in a + * temporary root passes a writer without fsync: four fsync pairs were most + * of its time and grew to seconds under the parallel suite's disk load. + */ + writeArtifact?: (input: ValidatedJsonArtifactInput) => Promise; }>; function canonicalize(value: unknown): unknown { @@ -90,7 +100,7 @@ export async function generateArtifactSchemas( continue; } await mkdir(path.dirname(destination), { recursive: true }); - await writeValidatedJsonArtifact({ + await (options.writeArtifact ?? writeValidatedJsonArtifact)({ path: destination, schema: jsonSchemaDocumentArtifactSchema, value: document, diff --git a/tests/unit/json-schema.test.ts b/tests/unit/json-schema.test.ts index fb5bfd0a..ebc27389 100644 --- a/tests/unit/json-schema.test.ts +++ b/tests/unit/json-schema.test.ts @@ -1,4 +1,4 @@ -import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { mkdtemp, open, readFile, rename, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; @@ -6,6 +6,30 @@ import { afterEach, describe, expect, it } from "vitest"; import { generateArtifactSchemas } from "../../scripts/generate-artifact-schemas.ts"; import { assertMatchesJsonSchema } from "../../scripts/lib/json-schema.ts"; +import { createValidatedJsonArtifactWriter } from "../../scripts/lib/validated-json-artifact.ts"; + +// The same validated, atomic-rename writer as the generator's default, without +// fsync. This suite checks generated bytes in a temporary root; durability is +// validated-json-artifact.test.ts's subject, and the fsync pairs were most of +// this test's time under the parallel suite's disk load. +const writeArtifactWithoutSync = createValidatedJsonArtifactWriter({ + fileSystem: { + open: async (target, flags, mode) => { + const handle = await open(target, flags, mode); + return { + writeFile: async (data, encoding) => handle.writeFile(data, encoding), + sync: async () => undefined, + close: async () => handle.close(), + }; + }, + openDirectory: async () => ({ + sync: async () => undefined, + close: async () => undefined, + }), + rename: async (source, destination) => rename(source, destination), + rm: async (target, options) => rm(target, options), + }, +}); const temporaryDirectories: string[] = []; @@ -77,7 +101,7 @@ describe("checked-in JSON Schema execution", () => { it("generates deterministic draft 2020-12 artifact schemas and detects drift", async () => { const root = await temporaryDirectory(); - await generateArtifactSchemas({ root }); + await generateArtifactSchemas({ root, writeArtifact: writeArtifactWithoutSync }); const generatedPath = path.join( root, "config/schemas/artifacts/dependency-inventory.schema.json",