Skip to content

Commit 9a3f09a

Browse files
fix(lib-report): replace empty skipFull tables with a fully-covered summary (#16)
Co-authored-by: Ari Perkkiö <[email protected]>
1 parent 26fd33d commit 9a3f09a

2 files changed

Lines changed: 120 additions & 5 deletions

File tree

‎packages/istanbul-lib-report/src/reports/text/index.ts‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,46 @@ function isFull(metrics: CoverageSummary): boolean {
194194
);
195195
}
196196

197+
/** why a row is left out of the table, or `null` when it is printed */
198+
function skipReason(
199+
metrics: CoverageSummary,
200+
skipEmpty: boolean | undefined,
201+
skipFull: boolean | undefined,
202+
): "empty" | "full" | null {
203+
if (skipEmpty && metrics.isEmpty()) return "empty";
204+
if (skipFull && isFull(metrics)) return "full";
205+
return null;
206+
}
207+
208+
/** counts of skipped files when `skipFull` would leave the table without file rows, otherwise `null` */
209+
function hiddenTableCounts(
210+
root: ReportNode,
211+
context: Context,
212+
skipEmpty: boolean | undefined,
213+
skipFull: boolean | undefined,
214+
): { full: number; empty: number } | null {
215+
if (!skipFull) return null;
216+
217+
const counts = { full: 0, empty: 0, shown: 0 };
218+
root.visit(
219+
context.getVisitor({
220+
onDetail(node) {
221+
const metrics = node.getCoverageSummary()!;
222+
223+
if (!skipReason(metrics, skipEmpty, skipFull)) counts.shown += 1;
224+
// empty files are hidden by `skipFull` too, but they are not "fully covered"
225+
else if (metrics.isEmpty()) counts.empty += 1;
226+
else counts.full += 1;
227+
},
228+
}),
229+
);
230+
return counts.shown === 0 && counts.full + counts.empty > 0 ? counts : null;
231+
}
232+
233+
function fileNoun(count: number): string {
234+
return count === 1 ? "file" : "files";
235+
}
236+
197237
function tableRow(
198238
node: ReportNode,
199239
context: Context,
@@ -207,10 +247,7 @@ function tableRow(
207247
const name = nodeName(node);
208248
const metrics = node.getCoverageSummary()!;
209249
const isEmpty = metrics.isEmpty();
210-
if (skipEmpty && isEmpty) {
211-
return "";
212-
}
213-
if (skipFull && isFull(metrics)) {
250+
if (skipReason(metrics, skipEmpty, skipFull)) {
214251
return "";
215252
}
216253

@@ -249,6 +286,7 @@ class TextReport extends ReportBase {
249286
declare skipFull: boolean | undefined;
250287
declare nameWidth: number;
251288
declare missingWidth: number;
289+
declare hideTable: boolean;
252290

253291
constructor(opts?: TextOptions) {
254292
super(opts);
@@ -261,10 +299,23 @@ class TextReport extends ReportBase {
261299
this.cw = null;
262300
this.skipEmpty = opts.skipEmpty;
263301
this.skipFull = opts.skipFull;
302+
this.hideTable = false;
264303
}
265304

266305
onStart(root: ReportNode, context: Context): void {
267306
this.cw = context.writer.writeFile(this.file);
307+
const counts = hiddenTableCounts(root, context, this.skipEmpty, this.skipFull);
308+
this.hideTable = counts !== null;
309+
if (counts) {
310+
this.cw.println("No files with missing coverage.");
311+
if (counts.full > 0) {
312+
this.cw.println(`${counts.full} ${fileNoun(counts.full)} fully covered.`);
313+
}
314+
if (counts.empty > 0) {
315+
this.cw.println(`${counts.empty} empty ${fileNoun(counts.empty)} skipped.`);
316+
}
317+
return;
318+
}
268319
this.nameWidth = Math.max(NAME_COL, findWidth(root, context, nodeName, depthFor));
269320
this.missingWidth = Math.max(MISSING_COL, findWidth(root, context, nodeMissing));
270321

@@ -289,6 +340,9 @@ class TextReport extends ReportBase {
289340
}
290341

291342
onSummary(node: ReportNode, context: Context): void {
343+
if (this.hideTable) {
344+
return;
345+
}
292346
const nodeDepth = depthFor(node);
293347
const row = tableRow(
294348
node,
@@ -310,7 +364,9 @@ class TextReport extends ReportBase {
310364
}
311365

312366
onEnd(): void {
313-
this.cw!.println(makeLine(this.nameWidth, this.missingWidth));
367+
if (!this.hideTable) {
368+
this.cw!.println(makeLine(this.nameWidth, this.missingWidth));
369+
}
314370
this.cw!.close();
315371
}
316372
}

‎packages/istanbul-lib-report/test/reports/text/index.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,63 @@ describe("TextReport", () => {
5454
createTest(file);
5555
}
5656
});
57+
58+
it("replaces an empty skipFull table with a fully-covered summary", () => {
59+
const fixture = require(
60+
path.resolve(import.meta.dirname, "../fixtures/specs/100-line-100-branch.json"),
61+
);
62+
const context = istanbulLibReport.createContext({
63+
dir: "./",
64+
coverageMap: istanbulLibCoverage.createCoverageMap(fixture.map),
65+
});
66+
const tree = context.getTree("pkg");
67+
const report = new TextReport({ ...fixture.opts, skipFull: true });
68+
tree.visit(report, context);
69+
assert.equal(
70+
FileWriter.getOutput(),
71+
"No files with missing coverage.\n1 file fully covered.\n",
72+
);
73+
});
74+
75+
describe("skipFull with multiple files", () => {
76+
function fixtureFile(spec: string, filePath: string) {
77+
const fixture = require(path.resolve(import.meta.dirname, "../fixtures/specs/" + spec));
78+
const fileCoverage = Object.values(fixture.map)[0] as istanbulLibCoverage.FileCoverageData;
79+
return { [filePath]: { ...fileCoverage, path: filePath } };
80+
}
81+
82+
function render(files: Record<string, istanbulLibCoverage.FileCoverageData>[]) {
83+
const context = istanbulLibReport.createContext({
84+
dir: "./",
85+
coverageMap: istanbulLibCoverage.createCoverageMap(Object.assign({}, ...files)),
86+
});
87+
context.getTree("pkg").visit(new TextReport({ maxCols: 80, skipFull: true }), context);
88+
return FileWriter.getOutput();
89+
}
90+
91+
const full = (filePath: string) => fixtureFile("100-line-100-branch.json", filePath);
92+
const empty = (filePath: string) => fixtureFile("empty-file.json", filePath);
93+
const partial = (filePath: string) => fixtureFile("missing-line-missing-branch.json", filePath);
94+
95+
it("counts fully covered and empty files separately", () => {
96+
const output = render([full("/src/a.js"), full("/src/nested/b.js"), empty("/src/e.js")]);
97+
assert.equal(
98+
output,
99+
"No files with missing coverage.\n2 files fully covered.\n1 empty file skipped.\n",
100+
);
101+
});
102+
103+
it("only reports empty files when nothing is fully covered", () => {
104+
const output = render([empty("/src/e.js"), empty("/src/f.js")]);
105+
assert.equal(output, "No files with missing coverage.\n2 empty files skipped.\n");
106+
});
107+
108+
it("prints the table when a file is partially covered", () => {
109+
const output = render([full("/src/full.js"), empty("/src/e.js"), partial("/src/partial.js")]);
110+
assert.include(output, "partial.js");
111+
assert.notInclude(output, "full.js");
112+
assert.notInclude(output, "e.js");
113+
assert.notInclude(output, "No files with missing coverage.");
114+
});
115+
});
57116
});

0 commit comments

Comments
 (0)