Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ function renderCoverageDiff(report: CoverageReport): string | null {

function fmtDiff(head: number, base: number): string {
const d = head - base;
if (d === 0) return "";
if (d === 0) return "0";
return d > 0 ? `+${d}` : String(d);
}

function fmtPctDiff(d: number | null): string {
if (d === null || d === 0) return "";
if (d === null) return "";
if (d === 0) return "0.00%";
return `${d > 0 ? "+" : ""}${d.toFixed(2)}%`;
}

Expand Down Expand Up @@ -124,14 +125,28 @@ function renderCoverageDiff(report: CoverageReport): string | null {

const actual = rows.filter((r): r is RowData => r !== "sep");
const lw = Math.max(...actual.map((r) => r.label.length));
const bw = Math.max(4, ...actual.map((r) => r.base.length));
const hw = Math.max(4, ...actual.map((r) => r.head.length));
const dw = Math.max(3, ...actual.map((r) => r.diff.length));
const bw = Math.max("base".length, ...actual.map((r) => r.base.length));
const hw = Math.max("head".length, ...actual.map((r) => r.head.length));
const dw = Math.max("+/-".length, ...actual.map((r) => r.diff.length));

function fmtColumns(
prefix: string,
label: string,
base: string,
head: string,
diff: string,
): string {
return `${prefix} ${label.padEnd(lw)} ${base.padStart(bw)} ${head.padStart(hw)} ${
diff.padStart(dw)
}`;
}

function fmtRow(r: RowData): string {
return `${r.prefix} ${r.label.padEnd(lw)} ${r.base.padStart(bw)} ${
r.head.padStart(hw)
} ${r.diff.padStart(dw)}`;
return fmtColumns(r.prefix, r.label, r.base, r.head, r.diff);
}

function fmtHeaderColumns(base: string, head: string, diff: string): string {
return ` ${"".padEnd(lw)} ${base.padEnd(bw)} ${head.padEnd(hw)} ${diff.padEnd(dw)}`;
}

const rowLen = fmtRow(actual[0]).length;
Expand All @@ -145,9 +160,7 @@ function renderCoverageDiff(report: CoverageReport): string | null {
const tl = Math.floor(tPad / 2);
const tr = tPad - tl;

const colInner = ` ${" ".repeat(lw)} ${"base".padStart(bw)} ${"head".padStart(hw)} ${
" +/-".padStart(dw)
}`;
const colInner = fmtHeaderColumns("base", "head", "+/-");

const lines: string[] = [
`@@${" ".repeat(tl)}${title}${" ".repeat(tr)}@@`,
Expand Down
60 changes: 60 additions & 0 deletions src/render_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,64 @@ describe("renderReport", () => {
const hitsLine = diffBlock.split("\n").find((l) => l.includes("Hits"))!;
expect(hitsLine.startsWith("-")).toBe(true);
});

test("coverage diff table renders unchanged values explicitly", () => {
const head: FileCoverage[] = [
{ file: "a.ts", coveredLines: 7, totalLines: 10, percent: 70 },
{ file: "b.ts", coveredLines: 9, totalLines: 10, percent: 90 },
];

const base: CoverageArtifact = {
tool: "bun",
files: [
{ file: "a.ts", coveredLines: 7, totalLines: 10, percent: 70 },
{ file: "b.ts", coveredLines: 9, totalLines: 10, percent: 90 },
],
commitSha: "abc",
branch: "main",
timestamp: "2025-01-01T00:00:00Z",
};

const toolReport = buildToolReport("bun", head, base, []);
const fullReport = buildFullReport([toolReport]);
const md = renderReport(fullReport, "<!-- m -->", true);

const diffBlock = md.split("```diff")[1].split("```")[0];
expect(diffBlock).toContain("0.00%");
expect(diffBlock).toContain("Files");
expect(diffBlock).toContain("Lines");
expect(diffBlock).toContain("Hits");
expect(diffBlock).toMatch(/Files\s+2\s+2\s+0/);
expect(diffBlock).toMatch(/Lines\s+20\s+20\s+0/);
expect(diffBlock).toMatch(/Hits\s+16\s+16\s+0/);
});

test("coverage diff table header columns align with data columns", () => {
const head: FileCoverage[] = [
{ file: "src/index.ts", coveredLines: 8, totalLines: 10, percent: 80 },
{ file: "src/utils.ts", coveredLines: 10, totalLines: 10, percent: 100 },
];
const base: CoverageArtifact = {
tool: "bun",
files: [
{ file: "src/index.ts", coveredLines: 7, totalLines: 10, percent: 70 },
{ file: "src/utils.ts", coveredLines: 9, totalLines: 10, percent: 90 },
],
commitSha: "abc",
branch: "main",
timestamp: "2025-01-01T00:00:00Z",
};

const toolReport = buildToolReport("bun", head, base, []);
const fullReport = buildFullReport([toolReport]);
const md = renderReport(fullReport, "<!-- m -->", true);
const diffBlock = md.split("```diff")[1].split("```")[0];
const lines = diffBlock.trim().split("\n");
const headerLine = lines[1].slice(2, -2);
const coverageLine = lines[2];

expect(headerLine.indexOf("base")).toBe(coverageLine.indexOf("80.00%"));
expect(headerLine.indexOf("head")).toBe(coverageLine.indexOf("90.00%"));
expect(headerLine.indexOf("+/-")).toBe(coverageLine.indexOf("+10.00%"));
});
});