|
1 | | -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 1 | +import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
2 | 2 | import { tmpdir } from "node:os"; |
3 | 3 | import { join } from "node:path"; |
4 | 4 | import { main, type Io } from "./prCommentCli.js"; |
@@ -197,6 +197,43 @@ describe("prCommentCli", () => { |
197 | 197 | expect(r.err).not.toContain(" at "); |
198 | 198 | }); |
199 | 199 |
|
| 200 | + describe("--out", () => { |
| 201 | + it("writes the comment to the file and leaves stdout empty", () => { |
| 202 | + const outPath = join(dir, "out-delta.md"); |
| 203 | + const r = run(headPath, basePath, `--out=${outPath}`); |
| 204 | + expect(r.code).toBe(0); |
| 205 | + expect(r.out).toBe(""); |
| 206 | + |
| 207 | + const written = readFileSync(outPath, "utf8"); |
| 208 | + expect(written.split("\n")[0]).toBe("<!-- observability-map-report -->"); |
| 209 | + }); |
| 210 | + |
| 211 | + it("writes an empty file when there is nothing to post, rather than no file", () => { |
| 212 | + const outPath = join(dir, "out-nothing.md"); |
| 213 | + const r = run(unchangedPath, unchangedPath, `--out=${outPath}`); |
| 214 | + expect(r.code).toBe(0); |
| 215 | + expect(existsSync(outPath)).toBe(true); |
| 216 | + expect(readFileSync(outPath, "utf8")).toBe(""); |
| 217 | + }); |
| 218 | + |
| 219 | + it("writes the resolved and scan-failed comments too", () => { |
| 220 | + for (const mode of ["--resolved", "--scan-failed"]) { |
| 221 | + const outPath = join(dir, `out${mode}.md`); |
| 222 | + expect(run(mode, `--out=${outPath}`).code).toBe(0); |
| 223 | + expect(readFileSync(outPath, "utf8").split("\n")[0]).toBe( |
| 224 | + "<!-- observability-map-report -->" |
| 225 | + ); |
| 226 | + } |
| 227 | + }); |
| 228 | + |
| 229 | + it("truncates a file left by an earlier run", () => { |
| 230 | + const outPath = join(dir, "out-stale.md"); |
| 231 | + writeFileSync(outPath, "stale content from a previous push\n"); |
| 232 | + expect(run(unchangedPath, unchangedPath, `--out=${outPath}`).code).toBe(0); |
| 233 | + expect(readFileSync(outPath, "utf8")).toBe(""); |
| 234 | + }); |
| 235 | + }); |
| 236 | + |
200 | 237 | it("exits 1 with a one-line message when base.json is malformed", () => { |
201 | 238 | const malformedBasePath = join(dir, "malformed-base.json"); |
202 | 239 | writeFileSync(malformedBasePath, "not json at all"); |
|
0 commit comments