diff --git a/.changeset/late-sheep-rush.md b/.changeset/late-sheep-rush.md new file mode 100644 index 0000000000..07c7162a8b --- /dev/null +++ b/.changeset/late-sheep-rush.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/perseus": patch +--- + +Internal: make the exhaustive test tool for PerseusItem parsing find the shortest input file that repros each failure diff --git a/packages/perseus/src/util/parse-perseus-json/exhaustive-test-tool/index.ts b/packages/perseus/src/util/parse-perseus-json/exhaustive-test-tool/index.ts index e3d5880d09..f50ea2bede 100755 --- a/packages/perseus/src/util/parse-perseus-json/exhaustive-test-tool/index.ts +++ b/packages/perseus/src/util/parse-perseus-json/exhaustive-test-tool/index.ts @@ -75,7 +75,11 @@ async function testFile(path: string, outputDir: string) { desc, "utf-8", ); - await fs.writeFile( + // We'd like to find the shortest assessment item that reproduces + // each mismatch, to keep our regression tests succinct and + // readable. To that end, we only update the item.json file if the + // current item is shorter than the one already on disk. + await writeFileIfShorterThanExisting( join(outputDir, hash, "item.json"), String(JSON.stringify(rawItem)), "utf-8", @@ -124,6 +128,21 @@ function typeName(value: unknown): string { return typeof value; } +async function writeFileIfShorterThanExisting( + path: string, + content: string, + encoding: "utf-8", // TODO(benchristel): add more encodings if needed +) { + const size = await fs + .stat(path) + .then((stat) => stat.size) + .catch(() => null); + const contentLength = Buffer.byteLength(content, encoding); + if (size == null || contentLength < size) { + await fs.writeFile(path, content, encoding); + } +} + async function* listFilesRecursively(dir: string) { for await (const entry of await fs.opendir(dir, {recursive: true})) { if (entry.isFile()) {