|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { announceReplacement } from "./replacement-notice"; |
| 3 | + |
| 4 | +function runAnnouncement(githubActions: string) { |
| 5 | + vi.stubEnv("GITHUB_ACTIONS", githubActions); |
| 6 | + const stdoutWrite = vi |
| 7 | + .spyOn(process.stdout, "write") |
| 8 | + .mockImplementation(() => true); |
| 9 | + const log = vi.spyOn(console, "log").mockImplementation(() => {}); |
| 10 | + const info = vi.spyOn(console, "info").mockImplementation(() => {}); |
| 11 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 12 | + |
| 13 | + announceReplacement(); |
| 14 | + |
| 15 | + return { |
| 16 | + stdoutCalls: |
| 17 | + stdoutWrite.mock.calls.length + |
| 18 | + log.mock.calls.length + |
| 19 | + info.mock.calls.length, |
| 20 | + stderrCalls: warn.mock.calls.length, |
| 21 | + annotations: warn.mock.calls |
| 22 | + .flat() |
| 23 | + .map(String) |
| 24 | + .filter((line) => line.startsWith("::warning::")), |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +describe("announceReplacement", () => { |
| 29 | + afterEach(() => { |
| 30 | + vi.restoreAllMocks(); |
| 31 | + vi.unstubAllEnvs(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("keeps stdout clean and adds one single-line annotation under GitHub Actions", () => { |
| 35 | + const { stdoutCalls, annotations } = runAnnouncement("true"); |
| 36 | + |
| 37 | + expect(stdoutCalls).toBe(0); |
| 38 | + expect(annotations).toHaveLength(1); |
| 39 | + expect(annotations[0]).not.toContain("\n"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("writes only to stderr and adds no annotation outside GitHub Actions", () => { |
| 43 | + const { stdoutCalls, stderrCalls, annotations } = runAnnouncement(""); |
| 44 | + |
| 45 | + expect(stdoutCalls).toBe(0); |
| 46 | + expect(stderrCalls).toBeGreaterThan(0); |
| 47 | + expect(annotations).toHaveLength(0); |
| 48 | + }); |
| 49 | +}); |
0 commit comments