Skip to content

Commit 59d59b1

Browse files
committed
Use fs directly since istanbul context writer doesn't support appending
1 parent 2e6738e commit 59d59b1

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

lib/vitest-reporter/dist.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"use strict";var{ReportBase:u}=require("istanbul-lib-report");function m(s){return s.statements.pct===100&&s.branches.pct===100&&s.functions.pct===100&&s.lines.pct===100}function h(s){if(s.isSummary())return[];let e=s.getCoverageSummary(!1),t=e.isEmpty()?0:e.lines.pct,r,c=s.getFileCoverage();if(t===100){let i=c.getBranchCoverageByLine();r=Object.entries(i).map(([l,{coverage:a}])=>[l,a===100])}else r=Object.entries(c.getLineCoverage());let o=!0;return r.reduce((i,[l,a])=>{if(a)o=!0;else{let p=parseInt(l);o?(i.push([p]),o=!1):i[i.length-1][1]=p}return i},[])}var f=["Statements","Branches","Functions","Lines"];module.exports=class extends u{skipEmpty;skipFull;results={};cw=null;watermarks;constructor(e){super(e),this.skipEmpty=!!e.skipEmpty,this.skipFull=!!e.skipFull,this.watermarks=null}onStart(e,n){if(!process.env.GITHUB_STEP_SUMMARY){console.log("Reporter not being executed in Github Actions environment");return}this.cw=n.writer.writeFile(process.env.GITHUB_STEP_SUMMARY),this.watermarks=n.watermarks,this.cw.println("<h3>Test Coverage</h3>"),this.cw.println("<table><thead><tr>");for(let t of["File",...f,"Uncovered Lines"])this.cw.println(`<th>${t}</th>`);this.cw.println("</tr></thead><tbody>")}onSummary(e){let n=e.getRelativeName()||"All Files",t=e.getCoverageSummary(!1),r=t.isEmpty();this.skipEmpty&&r||this.skipFull&&m(t)||(this.results[n]={statements:r?0:t.statements.pct,branches:r?0:t.branches.pct,functions:r?0:t.functions.pct,lines:r?0:t.lines.pct,uncoveredLines:h(e)})}onDetail(e){return this.onSummary(e)}formatter(e,n){if(!this.watermarks)return`<td>${e}%</td>`;let[t,r]=this.watermarks[n];return e<t?`<td><p style="color:red">${e}%</p></td>`:e>r?`<td><p style="color:green">${e}%</p></td>`:`<td><p style="color:yellow">${e}%</p></td>`}onEnd(){if(!this.cw)return;let e=Object.keys(this.results).sort();for(let n of e){let t=this.results[n];if(this.cw.println(`<tr><td><code>${n}</code></td>`),this.cw.println(this.formatter(t.statements,"statements")),this.cw.println(this.formatter(t.branches,"branches")),this.cw.println(this.formatter(t.functions,"functions")),this.cw.println(this.formatter(t.lines,"lines")),t.uncoveredLines.length>0){this.cw.println("<td><details><summary>Expand</summary><ul>");for(let r of t.uncoveredLines)r.length===1?this.cw.println(`<li>${r[0]}</li>`):this.cw.println(`<li>${r[0]}-${r[1]}</li>`);this.cw.println("</ul></details></td>")}else this.cw.println("<td></td>");this.cw.println("</tr>")}this.cw.println("</tbody></table>"),this.cw.close()}};
1+
"use strict";var m=require("fs"),{ReportBase:p}=require("istanbul-lib-report");function h(s){return s.statements.pct===100&&s.branches.pct===100&&s.functions.pct===100&&s.lines.pct===100}function f(s){if(s.isSummary())return[];let e=s.getCoverageSummary(!1),t=e.isEmpty()?0:e.lines.pct,r,c=s.getFileCoverage();if(t===100){let i=c.getBranchCoverageByLine();r=Object.entries(i).map(([a,{coverage:l}])=>[a,l===100])}else r=Object.entries(c.getLineCoverage());let o=!0;return r.reduce((i,[a,l])=>{if(l)o=!0;else{let u=parseInt(a);o?(i.push([u]),o=!1):i[i.length-1][1]=u}return i},[])}var w=["Statements","Branches","Functions","Lines"];module.exports=class extends p{skipEmpty;skipFull;results={};cw=null;watermarks=null;constructor(e){super(e),this.skipEmpty=!!e.skipEmpty,this.skipFull=!!e.skipFull}onStart(e,n){if(!process.env.GITHUB_STEP_SUMMARY){console.log("Reporter not being executed in Github Actions environment");return}this.cw=m.createWriteStream(process.env.GITHUB_STEP_SUMMARY,{encoding:"utf-8",flags:"a"}),this.watermarks=n.watermarks,this.cw.write("<h3>Test Coverage</h3>"),this.cw.write("<table><thead><tr>");for(let t of["File",...w,"Uncovered Lines"])this.cw.write(`<th>${t}</th>`);this.cw.write("</tr></thead><tbody>")}onSummary(e){let n=e.getRelativeName()||"All Files",t=e.getCoverageSummary(!1),r=t.isEmpty();this.skipEmpty&&r||this.skipFull&&h(t)||(this.results[n]={statements:r?0:t.statements.pct,branches:r?0:t.branches.pct,functions:r?0:t.functions.pct,lines:r?0:t.lines.pct,uncoveredLines:f(e)})}onDetail(e){return this.onSummary(e)}formatter(e,n){if(!this.watermarks)return`<td>${e}%</td>`;let[t,r]=this.watermarks[n];return e<t?`<td><p style="color:red">${e}%</p></td>`:e>r?`<td><p style="color:green">${e}%</p></td>`:`<td><p style="color:yellow">${e}%</p></td>`}onEnd(){if(!this.cw)return;let e=Object.keys(this.results).sort();for(let n of e){let t=this.results[n];if(this.cw.write(`<tr><td><code>${n}</code></td>`),this.cw.write(this.formatter(t.statements,"statements")),this.cw.write(this.formatter(t.branches,"branches")),this.cw.write(this.formatter(t.functions,"functions")),this.cw.write(this.formatter(t.lines,"lines")),t.uncoveredLines.length>0){this.cw.write("<td><details><summary>Expand</summary><ul>");for(let r of t.uncoveredLines)r.length===1?this.cw.write(`<li>${r[0]}</li>`):this.cw.write(`<li>${r[0]}-${r[1]}</li>`);this.cw.write("</ul></details></td>")}else this.cw.write("<td></td>");this.cw.write("</tr>")}this.cw.write("</tbody></table>"),this.cw.close()}};

lib/vitest-reporter/index.cts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
*
55
* Heavily based on the default text reporter
66
*/
7+
import type fslib from 'fs';
78
import type { CoverageSummary } from 'istanbul-lib-coverage';
89
import type * as report from 'istanbul-lib-report';
910

11+
const fs: typeof fslib = require('fs');
1012
const { ReportBase }: typeof report = require('istanbul-lib-report');
1113

1214
/**
@@ -79,15 +81,14 @@ module.exports = class GithubActionsCoverageReporter extends ReportBase {
7981
private readonly skipEmpty: boolean;
8082
private readonly skipFull: boolean;
8183
private readonly results: Record<string, ResultObject> = {};
82-
private cw: report.ContentWriter | null = null;
83-
private watermarks: report.Watermarks | null;
84+
private cw: fslib.WriteStream | null = null;
85+
private watermarks: report.Watermarks | null = null;
8486

8587
constructor(opts: any) {
8688
super(opts);
8789

8890
this.skipEmpty = Boolean(opts.skipEmpty);
8991
this.skipFull = Boolean(opts.skipFull);
90-
this.watermarks = null;
9192
}
9293

9394
onStart(_node: any, context: report.Context) {
@@ -96,15 +97,15 @@ module.exports = class GithubActionsCoverageReporter extends ReportBase {
9697
return;
9798
}
9899

99-
this.cw = context.writer.writeFile(process.env.GITHUB_STEP_SUMMARY);
100+
this.cw = fs.createWriteStream(process.env.GITHUB_STEP_SUMMARY, { encoding: 'utf-8', flags: 'a' })
100101

101102
this.watermarks = context.watermarks;
102-
this.cw.println('<h3>Test Coverage</h3>')
103-
this.cw.println('<table><thead><tr>');
103+
this.cw.write('<h3>Test Coverage</h3>')
104+
this.cw.write('<table><thead><tr>');
104105
for (const heading of ['File', ...headers, 'Uncovered Lines']) {
105-
this.cw.println(`<th>${heading}</th>`);
106+
this.cw.write(`<th>${heading}</th>`);
106107
}
107-
this.cw.println('</tr></thead><tbody>');
108+
this.cw.write('</tr></thead><tbody>');
108109
}
109110

110111
onSummary(node: report.ReportNode) {
@@ -154,30 +155,30 @@ module.exports = class GithubActionsCoverageReporter extends ReportBase {
154155

155156
for (const fileName of fileNames) {
156157
const metrics = this.results[fileName];
157-
this.cw.println(`<tr><td><code>${fileName}</code></td>`);
158-
this.cw.println(this.formatter(metrics.statements, 'statements'));
159-
this.cw.println(this.formatter(metrics.branches, 'branches'));
160-
this.cw.println(this.formatter(metrics.functions, 'functions'));
161-
this.cw.println(this.formatter(metrics.lines, 'lines'));
158+
this.cw.write(`<tr><td><code>${fileName}</code></td>`);
159+
this.cw.write(this.formatter(metrics.statements, 'statements'));
160+
this.cw.write(this.formatter(metrics.branches, 'branches'));
161+
this.cw.write(this.formatter(metrics.functions, 'functions'));
162+
this.cw.write(this.formatter(metrics.lines, 'lines'));
162163

163164
if (metrics.uncoveredLines.length > 0) {
164-
this.cw.println('<td><details><summary>Expand</summary><ul>');
165+
this.cw.write('<td><details><summary>Expand</summary><ul>');
165166
for (const range of metrics.uncoveredLines) {
166167
if (range.length === 1) {
167-
this.cw.println(`<li>${range[0]}</li>`)
168+
this.cw.write(`<li>${range[0]}</li>`)
168169
} else {
169-
this.cw.println(`<li>${range[0]}-${range[1]}</li>`)
170+
this.cw.write(`<li>${range[0]}-${range[1]}</li>`)
170171
}
171172
}
172-
this.cw.println('</ul></details></td>');
173+
this.cw.write('</ul></details></td>');
173174
} else {
174-
this.cw.println('<td></td>');
175+
this.cw.write('<td></td>');
175176
}
176177

177-
this.cw.println('</tr>');
178+
this.cw.write('</tr>');
178179
}
179180

180-
this.cw.println('</tbody></table>');
181+
this.cw.write('</tbody></table>');
181182
this.cw.close();
182183
}
183184
}

0 commit comments

Comments
 (0)