Skip to content

Commit 31f2938

Browse files
samchonclaude
andcommitted
test(typia-generate): assert regeneration is byte-identical
Extends the generate harness to re-run `typia generate` into a second output directory and fail if any file differs from the first run, pinning the non-deterministic emit order (unsorted Go map iteration) fixed in the native Random/Protobuf/Metadata programmers on this branch. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 3e25b23 commit 31f2938

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
src/output
1+
src/output*

tests/test-typia-generate/index.js

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,66 @@
11
/**
2-
* Verifies that `typia generate` actually transforms each input file.
2+
* Verifies that `typia generate` transforms its input deterministically.
33
*
4-
* Pins the regression where `typia generate` discarded the transform
5-
* result and copied the original input verbatim into the output. Every
6-
* `src/input/generate_*.ts` fixture contains `typia.*<T>()` call sites,
7-
* so a correctly transformed output can never be byte-identical to its
8-
* input file.
4+
* Pins two regressions: (1) `typia generate` discarding the transform
5+
* result and copying the input file verbatim into the output, and (2)
6+
* non-deterministic emit order caused by unsorted Go map iteration in the
7+
* native programmers. Every `src/input/generate_*.ts` fixture contains
8+
* `typia.*<T>()` call sites, so a correct output is never byte-identical
9+
* to its input and never changes between two generator runs.
910
*
1011
* 1. Run `pnpm run generate` then `pnpm run build` (the `start` script).
11-
* 2. For each input file, read the matching generated output file.
12-
* 3. Fail if any output is missing or identical to its input file.
12+
* 2. Assert each output differs from its input file (it was transformed).
13+
* 3. Re-run the generator and assert every output is byte-identical.
1314
*/
15+
const cp = require("child_process");
1416
const fs = require("fs");
1517

1618
const inputDir = `${__dirname}/src/input`;
1719
const outputDir = `${__dirname}/src/output`;
20+
const repeatDir = `${__dirname}/src/output-repeat`;
1821

1922
const main = () => {
2023
const files = fs
2124
.readdirSync(inputDir)
2225
.filter((file) => file.endsWith(".ts"));
2326
let failed = false;
27+
28+
// (1) every output must be transformed, not a verbatim copy of its input
2429
for (const file of files) {
25-
const input = fs.readFileSync(`${inputDir}/${file}`, "utf8");
2630
const outputPath = `${outputDir}/${file}`;
2731
if (fs.existsSync(outputPath) === false) {
2832
console.error(`Bug on ${file}: output file was not generated.`);
2933
failed = true;
3034
continue;
3135
}
32-
const output = fs.readFileSync(outputPath, "utf8");
33-
if (input === output) {
36+
const input = fs.readFileSync(`${inputDir}/${file}`, "utf8");
37+
if (fs.readFileSync(outputPath, "utf8") === input) {
3438
console.error(
3539
`Bug on ${file}: output is identical to input (not transformed).`,
3640
);
3741
failed = true;
3842
}
3943
}
44+
45+
// (2) re-running the generator must produce byte-identical output
46+
fs.rmSync(repeatDir, { recursive: true, force: true });
47+
cp.execSync("pnpm run generate:repeat", {
48+
cwd: __dirname,
49+
stdio: "ignore",
50+
});
51+
for (const file of files) {
52+
const first = fs.readFileSync(`${outputDir}/${file}`, "utf8");
53+
const second = fs.readFileSync(`${repeatDir}/${file}`, "utf8");
54+
if (first !== second) {
55+
console.error(`Bug on ${file}: regeneration is not deterministic.`);
56+
failed = true;
57+
}
58+
}
59+
fs.rmSync(repeatDir, { recursive: true, force: true });
60+
4061
if (failed) process.exit(1);
41-
console.log(`All ${files.length} generate outputs were transformed.`);
62+
console.log(
63+
`All ${files.length} generate outputs are transformed and deterministic.`,
64+
);
4265
};
4366
main();

tests/test-typia-generate/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"build": "ttsc",
88
"generate": "ttsx node_modules/typia/src/executable/typia.ts generate --input src/input --output src/output",
9+
"generate:repeat": "ttsx node_modules/typia/src/executable/typia.ts generate --input src/input --output src/output-repeat",
910
"start": "pnpm run generate && pnpm run build && node index.js"
1011
},
1112
"repository": {

0 commit comments

Comments
 (0)