|
10 | 10 | import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
11 | 11 | import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
12 | 12 | import { tmpdir } from "node:os"; |
13 | | -import { join } from "node:path"; |
| 13 | +import { dirname, join } from "node:path"; |
14 | 14 | import { parse as parseYaml } from "yaml"; |
15 | 15 | import { parseRepoSlug } from "../../src/discovery/targets.js"; |
16 | 16 | import { SectionSelection } from "../../src/engine/section-selection.js"; |
@@ -241,6 +241,73 @@ function disjointRefusal(snapshotDir: string, reposDir: string): string { |
241 | 241 | ); |
242 | 242 | } |
243 | 243 |
|
| 244 | +describe("runSnapshot writes through a staging file", () => { |
| 245 | + const unwritable = (path: string, input: "snapshot-file" | "snapshot-dir", os: string) => ({ |
| 246 | + level: "error" as const, |
| 247 | + line: `cannot write the snapshot to ${path}: Error: ${os}. Check that the "${input}" input names a writable path`, |
| 248 | + }); |
| 249 | + |
| 250 | + test("a staging write that fails leaves the previous snapshot intact and reports the write's error", async () => { |
| 251 | + const api = new MockApi(labelsRoute("o/r", [BUG])); |
| 252 | + const cfg = fileCfg(); |
| 253 | + const staging = `${cfg.snapshotFile}.tmp`; |
| 254 | + mkdirSync(dirname(cfg.snapshotFile), { recursive: true }); |
| 255 | + writeFileSync(cfg.snapshotFile, "labels: []\n"); |
| 256 | + // A directory at the staging path fails the write before the rename; the run must not remove it either. |
| 257 | + mkdirSync(staging); |
| 258 | + const collected = collectingIo(); |
| 259 | + expect(await run(api, cfg, collected.io)).toBe(1); |
| 260 | + expect(collected.outputs.result).toBe("failed"); |
| 261 | + expect(collected.lines[0]).toEqual( |
| 262 | + unwritable( |
| 263 | + cfg.snapshotFile, |
| 264 | + "snapshot-file", |
| 265 | + `EISDIR: illegal operation on a directory, open '${staging}'`, |
| 266 | + ), |
| 267 | + ); |
| 268 | + expect(readFileSync(cfg.snapshotFile, "utf8")).toBe("labels: []\n"); |
| 269 | + rmSync(staging, { recursive: true }); |
| 270 | + expect(await run(api, cfg, collectingIo().io)).toBe(0); |
| 271 | + expect(parseYaml(readFileSync(cfg.snapshotFile, "utf8"))).toEqual(doc(BUG)); |
| 272 | + expect(existsSync(staging)).toBe(false); |
| 273 | + }); |
| 274 | + |
| 275 | + test("a rename that fails removes the staging file, fails only that target, and leaves the destination as it was", async () => { |
| 276 | + const api = new MockApi({ |
| 277 | + "GET /repos/o/a": { data: { private: false } }, |
| 278 | + "GET /repos/o/b": { data: { private: false } }, |
| 279 | + ...labelsRoute("o/a", [BUG]), |
| 280 | + ...labelsRoute("o/b", [DOCS]), |
| 281 | + }); |
| 282 | + const cfg = dirCfg(); |
| 283 | + const fileA = join(cfg.snapshotDir, "o", "a.yml"); |
| 284 | + const fileB = join(cfg.snapshotDir, "o", "b.yml"); |
| 285 | + // A directory holding a file at o/a's destination lets the staging write succeed and the rename fail. |
| 286 | + mkdirSync(fileA, { recursive: true }); |
| 287 | + writeFileSync(join(fileA, "keep"), "authored\n"); |
| 288 | + const collected = collectingIo(); |
| 289 | + expect(await run(api, cfg, collected.io)).toBe(1); |
| 290 | + expect(collected.outputs).toEqual({ |
| 291 | + "skipped-sections": "", |
| 292 | + result: "failed", |
| 293 | + "repos-result": JSON.stringify({ |
| 294 | + "o/a": { result: "failed", source: "remote", skippedSections: [] }, |
| 295 | + "o/b": { result: "snapshot", source: "remote", skippedSections: [] }, |
| 296 | + }), |
| 297 | + }); |
| 298 | + const { level, line } = unwritable( |
| 299 | + fileA, |
| 300 | + "snapshot-dir", |
| 301 | + `EISDIR: illegal operation on a directory, rename '${fileA}.tmp' -> '${fileA}'`, |
| 302 | + ); |
| 303 | + expect(collected.lines[0]).toEqual({ level, line: `o/a: ${line}` }); |
| 304 | + expect(existsSync(`${fileA}.tmp`)).toBe(false); |
| 305 | + expect(readFileSync(join(fileA, "keep"), "utf8")).toBe("authored\n"); |
| 306 | + expect(parseYaml(readFileSync(fileB, "utf8"))).toEqual(doc(DOCS)); |
| 307 | + expect(existsSync(`${fileB}.tmp`)).toBe(false); |
| 308 | + }); |
| 309 | +}); |
| 310 | + |
244 | 311 | describe("runSnapshot refuses a destination that would overwrite an authored file", () => { |
245 | 312 | test.each([ |
246 | 313 | [ |
|
0 commit comments