|
1 | 1 | // Format Generated Module tests cover format generated module script behavior. |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import os from "node:os"; |
2 | 4 | import path from "node:path"; |
3 | | -import { describe, expect, it } from "vitest"; |
4 | | -import { resolveGeneratedModuleFormatter } from "../../scripts/lib/format-generated-module.mjs"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import { |
| 7 | + formatGeneratedModule, |
| 8 | + GENERATED_MODULE_FORMAT_MAX_BUFFER_BYTES, |
| 9 | + GENERATED_MODULE_FORMAT_TIMEOUT_MS, |
| 10 | + resolveGeneratedModuleFormatter, |
| 11 | +} from "../../scripts/lib/format-generated-module.mjs"; |
| 12 | + |
| 13 | +const tempDirs: string[] = []; |
| 14 | + |
| 15 | +function makeRepoRoot() { |
| 16 | + const repoRoot = mkdtempSync(path.join(os.tmpdir(), "openclaw-format-generated-module-")); |
| 17 | + tempDirs.push(repoRoot); |
| 18 | + const formatterDir = path.join(repoRoot, "node_modules", ".bin"); |
| 19 | + mkdirSync(formatterDir, { recursive: true }); |
| 20 | + writeFileSync(path.join(formatterDir, "oxfmt"), "#!/bin/sh\n", "utf8"); |
| 21 | + return repoRoot; |
| 22 | +} |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + for (const dir of tempDirs.splice(0)) { |
| 26 | + rmSync(dir, { force: true, recursive: true }); |
| 27 | + } |
| 28 | +}); |
5 | 29 |
|
6 | 30 | describe("resolveGeneratedModuleFormatter", () => { |
7 | 31 | it("uses the direct formatter binary on non-Windows when available", () => { |
@@ -43,4 +67,103 @@ describe("resolveGeneratedModuleFormatter", () => { |
43 | 67 | windowsVerbatimArguments: true, |
44 | 68 | }); |
45 | 69 | }); |
| 70 | + |
| 71 | + it("runs generated module formatting with bounded child execution", () => { |
| 72 | + const repoRoot = makeRepoRoot(); |
| 73 | + const calls: unknown[] = []; |
| 74 | + |
| 75 | + const formatted = formatGeneratedModule( |
| 76 | + "export const value=1;", |
| 77 | + { |
| 78 | + errorLabel: "test module", |
| 79 | + outputPath: "generated.ts", |
| 80 | + repoRoot, |
| 81 | + }, |
| 82 | + { |
| 83 | + resolveFormatter: ({ outputPath }: { outputPath: string }) => ({ |
| 84 | + args: ["--write", outputPath], |
| 85 | + command: "oxfmt", |
| 86 | + shell: false, |
| 87 | + }), |
| 88 | + spawnSync: (_command: string, args: string[], options: unknown) => { |
| 89 | + calls.push(options); |
| 90 | + writeFileSync(args[1] ?? "", "export const value = 1;\n", "utf8"); |
| 91 | + return { status: 0, stderr: "", stdout: "" }; |
| 92 | + }, |
| 93 | + }, |
| 94 | + ); |
| 95 | + |
| 96 | + expect(formatted).toBe("export const value = 1;\n"); |
| 97 | + expect(calls).toHaveLength(1); |
| 98 | + expect(calls[0]).toMatchObject({ |
| 99 | + cwd: repoRoot, |
| 100 | + encoding: "utf8", |
| 101 | + maxBuffer: GENERATED_MODULE_FORMAT_MAX_BUFFER_BYTES, |
| 102 | + shell: false, |
| 103 | + timeout: GENERATED_MODULE_FORMAT_TIMEOUT_MS, |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("reports formatter timeouts with bounded output tails", () => { |
| 108 | + const repoRoot = makeRepoRoot(); |
| 109 | + const timeoutError = Object.assign(new Error("spawnSync oxfmt ETIMEDOUT"), { |
| 110 | + code: "ETIMEDOUT", |
| 111 | + }); |
| 112 | + |
| 113 | + expect(() => |
| 114 | + formatGeneratedModule( |
| 115 | + "export const value=1;", |
| 116 | + { |
| 117 | + errorLabel: "test module", |
| 118 | + outputPath: "generated.ts", |
| 119 | + repoRoot, |
| 120 | + }, |
| 121 | + { |
| 122 | + resolveFormatter: ({ outputPath }: { outputPath: string }) => ({ |
| 123 | + args: ["--write", outputPath], |
| 124 | + command: "oxfmt", |
| 125 | + shell: false, |
| 126 | + }), |
| 127 | + spawnSync: () => ({ |
| 128 | + error: timeoutError, |
| 129 | + signal: "SIGTERM", |
| 130 | + status: null, |
| 131 | + stderr: `DO_NOT_DUMP_OLD_STDERR${"x".repeat(20 * 1024)}\nrecent stderr tail`, |
| 132 | + stdout: `DO_NOT_DUMP_OLD_STDOUT${"x".repeat(20 * 1024)}\nrecent stdout tail`, |
| 133 | + }), |
| 134 | + }, |
| 135 | + ), |
| 136 | + ).toThrow( |
| 137 | + /formatter timed out after 30000ms[\s\S]*recent stderr tail[\s\S]*recent stdout tail/u, |
| 138 | + ); |
| 139 | + |
| 140 | + try { |
| 141 | + formatGeneratedModule( |
| 142 | + "export const value=1;", |
| 143 | + { |
| 144 | + errorLabel: "test module", |
| 145 | + outputPath: "generated.ts", |
| 146 | + repoRoot, |
| 147 | + }, |
| 148 | + { |
| 149 | + resolveFormatter: ({ outputPath }: { outputPath: string }) => ({ |
| 150 | + args: ["--write", outputPath], |
| 151 | + command: "oxfmt", |
| 152 | + shell: false, |
| 153 | + }), |
| 154 | + spawnSync: () => ({ |
| 155 | + error: timeoutError, |
| 156 | + signal: "SIGTERM", |
| 157 | + status: null, |
| 158 | + stderr: `DO_NOT_DUMP_OLD_STDERR${"x".repeat(20 * 1024)}\nrecent stderr tail`, |
| 159 | + stdout: `DO_NOT_DUMP_OLD_STDOUT${"x".repeat(20 * 1024)}\nrecent stdout tail`, |
| 160 | + }), |
| 161 | + }, |
| 162 | + ); |
| 163 | + } catch (error) { |
| 164 | + const message = error instanceof Error ? error.message : String(error); |
| 165 | + expect(message).not.toContain("DO_NOT_DUMP_OLD_STDERR"); |
| 166 | + expect(message).not.toContain("DO_NOT_DUMP_OLD_STDOUT"); |
| 167 | + } |
| 168 | + }); |
46 | 169 | }); |
0 commit comments