|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import { CmdRunTask, CmdRunTaskResult } from "./_types"; |
| 3 | +import { applyRunExitCode } from "./exit-code"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Tests that the CLI exits with non-zero code when localization tasks fail. |
| 7 | + * This prevents CI/CD pipelines from silently passing on partial errors. |
| 8 | + */ |
| 9 | +describe("run command - exit code on errors", () => { |
| 10 | + let originalExitCode: number | undefined; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + originalExitCode = process.exitCode; |
| 14 | + process.exitCode = undefined; |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + process.exitCode = originalExitCode; |
| 19 | + }); |
| 20 | + |
| 21 | + function createTask(overrides?: Partial<CmdRunTask>): CmdRunTask { |
| 22 | + return { |
| 23 | + sourceLocale: "en", |
| 24 | + targetLocale: "es", |
| 25 | + bucketType: "json", |
| 26 | + bucketPathPattern: "[locale]/messages.json", |
| 27 | + injectLocale: [], |
| 28 | + lockedKeys: [], |
| 29 | + lockedPatterns: [], |
| 30 | + ignoredKeys: [], |
| 31 | + preservedKeys: [], |
| 32 | + localizableKeys: [], |
| 33 | + onlyKeys: [], |
| 34 | + ...overrides, |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + it("should set exitCode=1 when any task has error status", () => { |
| 39 | + const results = new Map<CmdRunTask, CmdRunTaskResult>(); |
| 40 | + results.set(createTask({ targetLocale: "es" }), { status: "success" }); |
| 41 | + results.set(createTask({ targetLocale: "fr" }), { |
| 42 | + status: "error", |
| 43 | + error: new Error("API timeout"), |
| 44 | + }); |
| 45 | + results.set(createTask({ targetLocale: "de" }), { status: "success" }); |
| 46 | + |
| 47 | + const hasErrors = applyRunExitCode(results); |
| 48 | + |
| 49 | + expect(hasErrors).toBe(true); |
| 50 | + expect(process.exitCode).toBe(1); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should NOT set exitCode when all tasks succeed", () => { |
| 54 | + const results = new Map<CmdRunTask, CmdRunTaskResult>(); |
| 55 | + results.set(createTask({ targetLocale: "es" }), { status: "success" }); |
| 56 | + results.set(createTask({ targetLocale: "fr" }), { status: "success" }); |
| 57 | + |
| 58 | + const hasErrors = applyRunExitCode(results); |
| 59 | + |
| 60 | + expect(hasErrors).toBe(false); |
| 61 | + expect(process.exitCode).toBeUndefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should NOT set exitCode when tasks are skipped (no errors)", () => { |
| 65 | + const results = new Map<CmdRunTask, CmdRunTaskResult>(); |
| 66 | + results.set(createTask({ targetLocale: "es" }), { status: "skipped" }); |
| 67 | + results.set(createTask({ targetLocale: "fr" }), { status: "success" }); |
| 68 | + |
| 69 | + const hasErrors = applyRunExitCode(results); |
| 70 | + |
| 71 | + expect(hasErrors).toBe(false); |
| 72 | + expect(process.exitCode).toBeUndefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should set exitCode=1 when all tasks fail", () => { |
| 76 | + const results = new Map<CmdRunTask, CmdRunTaskResult>(); |
| 77 | + results.set(createTask({ targetLocale: "es" }), { |
| 78 | + status: "error", |
| 79 | + error: new Error("fail 1"), |
| 80 | + }); |
| 81 | + results.set(createTask({ targetLocale: "fr" }), { |
| 82 | + status: "error", |
| 83 | + error: new Error("fail 2"), |
| 84 | + }); |
| 85 | + |
| 86 | + const hasErrors = applyRunExitCode(results); |
| 87 | + |
| 88 | + expect(hasErrors).toBe(true); |
| 89 | + expect(process.exitCode).toBe(1); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should set exitCode=1 even with mix of success, skipped, and error", () => { |
| 93 | + const results = new Map<CmdRunTask, CmdRunTaskResult>(); |
| 94 | + results.set(createTask({ targetLocale: "es" }), { status: "success" }); |
| 95 | + results.set(createTask({ targetLocale: "fr" }), { status: "skipped" }); |
| 96 | + results.set(createTask({ targetLocale: "de" }), { |
| 97 | + status: "error", |
| 98 | + error: new Error("one failure"), |
| 99 | + }); |
| 100 | + |
| 101 | + const hasErrors = applyRunExitCode(results); |
| 102 | + |
| 103 | + expect(hasErrors).toBe(true); |
| 104 | + expect(process.exitCode).toBe(1); |
| 105 | + }); |
| 106 | + |
| 107 | + it("should NOT set exitCode when results map is empty", () => { |
| 108 | + const results = new Map<CmdRunTask, CmdRunTaskResult>(); |
| 109 | + |
| 110 | + const hasErrors = applyRunExitCode(results); |
| 111 | + |
| 112 | + expect(hasErrors).toBe(false); |
| 113 | + expect(process.exitCode).toBeUndefined(); |
| 114 | + }); |
| 115 | +}); |
0 commit comments