From d99ad55f5bc6e54709190e70cfdfe6a1f4039ed8 Mon Sep 17 00:00:00 2001 From: Ishan Madhusanka Date: Mon, 18 Jul 2022 15:47:06 +0530 Subject: [PATCH] fix issue with numbers in file name --- .github/workflows/test.yml | 1 + dist/index.js | 8 ++++---- src/sample/add2.spec.ts | 7 +++++++ src/sample/add2.ts | 3 +++ src/steps/__sample__/index.ts | 2 +- .../__snapshots__/merge-coverage.spec.ts.snap | 15 +++++++++++++++ src/steps/merge-coverage.spec.ts | 11 ++++++++++- src/steps/merge-coverage.ts | 19 +++++++++++++------ 8 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 src/sample/add2.spec.ts create mode 100644 src/sample/add2.ts create mode 100644 src/steps/__snapshots__/merge-coverage.spec.ts.snap diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 66d0661..ee6f3fb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -93,3 +93,4 @@ jobs: github-token: "${{ secrets.PERSONAL_ACCESS_TOKEN }}" command: "merge-coverage" shard-count: "2" + show-all-files-in-summary: true diff --git a/dist/index.js b/dist/index.js index 37a1ca1..86e9586 100644 --- a/dist/index.js +++ b/dist/index.js @@ -52,10 +52,10 @@ function print() { __p += __j.call(arguments, '') } `:`; `)+N+`return __p }`;var K=j0(function(){return te(h,G+"return "+N).apply(e,w)});if(K.source=N,id(K))throw K;return K}function _f(t){return ie(t).toLowerCase()}function Ef(t){return ie(t).toUpperCase()}function Tf(t,i,n){if(t=ie(t),t&&(n||i===e))return X2(t);if(!t||!(i=c1(i)))return t;var o=$1(t),u=$1(i),h=Q2(o,u),w=eo(o,u)+1;return kt(o,h,w).join("")}function yf(t,i,n){if(t=ie(t),t&&(n||i===e))return t.slice(0,ao(t)+1);if(!t||!(i=c1(i)))return t;var o=$1(t),u=eo(o,$1(i))+1;return kt(o,0,u).join("")}function bf(t,i,n){if(t=ie(t),t&&(n||i===e))return t.replace(rn,"");if(!t||!(i=c1(i)))return t;var o=$1(t),u=Q2(o,$1(i));return kt(o,u).join("")}function Af(t,i){var n=mt,o=U1;if(ve(i)){var u="separator"in i?i.separator:u;n="length"in i?z(i.length):n,o="omission"in i?c1(i.omission):o}t=ie(t);var h=t.length;if(Da(t)){var w=$1(t);h=w.length}if(n>=h)return t;var _=n-Ca(o);if(_<1)return o;var T=w?kt(w,0,_).join(""):t.slice(0,_);if(u===e)return T+o;if(w&&(_+=T.length-_),pd(u)){if(t.slice(_).search(u)){var C,O=T;for(u.global||(u=Tn(u.source,ie(E2.exec(u))+"g")),u.lastIndex=0;C=u.exec(O);)var N=C.index;T=T.slice(0,N===e?_:N)}}else if(t.indexOf(c1(u),_)!=_){var R=T.lastIndexOf(u);R>-1&&(T=T.slice(0,R))}return T+o}function Sf(t){return t=ie(t),t&&x3.test(t)?t.replace(g2,Q5):t}var Df=Pa(function(t,i,n){return t+(n?" ":"")+i.toUpperCase()}),sd=Ho("toUpperCase");function $0(t,i,n){return t=ie(t),i=n?e:i,i===e?K5(t)?a8(t):V5(t):t.match(i)||[]}var j0=J(function(t,i){try{return u1(t,e,i)}catch(n){return id(n)?n:new j(n)}}),Cf=wt(function(t,i){return b1(i,function(n){n=et(n),vt(t,n,ad(t[n],t))}),t});function Of(t){var i=t==null?0:t.length,n=L();return t=i?fe(t,function(o){if(typeof o[1]!="function")throw new A1(d);return[n(o[0]),o[1]]}):[],J(function(o){for(var u=-1;++uL1)return[];var n=Ze,o=qe(t,Ze);i=L(i),t-=Ze;for(var u=wn(o,i);++n0||i<0)?new X(n):(t<0?n=n.takeRight(-t):t&&(n=n.drop(t)),i!==e&&(i=z(i),n=i<0?n.dropRight(-i):n.take(i-t)),n)},X.prototype.takeRightWhile=function(t){return this.reverse().takeWhile(t).reverse()},X.prototype.toArray=function(){return this.take(Ze)},X1(X.prototype,function(t,i){var n=/^(?:filter|find|map|reject)|While$/.test(i),o=/^(?:head|last)$/.test(i),u=f[o?"take"+(i=="last"?"Right":""):i],h=o||/^find/.test(i);!u||(f.prototype[i]=function(){var w=this.__wrapped__,_=o?[1]:arguments,T=w instanceof X,C=_[0],O=T||H(w),N=function(Y){var Q=u.apply(f,Nt([Y],_));return o&&R?Q[0]:Q};O&&n&&typeof C=="function"&&C.length!=1&&(T=O=!1);var R=this.__chain__,G=!!this.__actions__.length,q=h&&!R,K=T&&!G;if(!h&&O){w=K?w:new X(this);var M=t.apply(w,_);return M.__actions__.push({func:Ki,args:[N],thisArg:e}),new S1(M,R)}return q&&K?t.apply(this,_):(M=this.thru(N),q?o?M.value()[0]:M.value():M)})}),b1(["pop","push","shift","sort","splice","unshift"],function(t){var i=Ei[t],n=/^(?:push|sort|unshift)$/.test(t)?"tap":"thru",o=/^(?:pop|shift)$/.test(t);f.prototype[t]=function(){var u=arguments;if(o&&!this.__chain__){var h=this.value();return i.apply(H(h)?h:[],u)}return this[n](function(w){return i.apply(H(w)?w:[],u)})}}),X1(X.prototype,function(t,i){var n=f[i];if(n){var o=n.name+"";de.call(Na,o)||(Na[o]=[]),Na[o].push({name:i,func:n})}}),Na[qi(e,I).name]=[{name:"wrapper",func:e}],X.prototype.clone=A8,X.prototype.reverse=S8,X.prototype.value=D8,f.prototype.at=t7,f.prototype.chain=a7,f.prototype.commit=r7,f.prototype.next=i7,f.prototype.plant=n7,f.prototype.reverse=d7,f.prototype.toJSON=f.prototype.valueOf=f.prototype.value=s7,f.prototype.first=f.prototype.head,yr&&(f.prototype[yr]=p7),f},Rt=r8();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(Fe._=Rt,define(function(){return Rt})):aa?((aa.exports=Rt)._=Rt,ln._=Rt):Fe._=Rt}).call(or)});var l1=z1(Ke()),f2=require("console");var lr=z1(ms()),g3=z1(s2()),w3=require("child_process"),di=require("console"),_3=require("fs");var qp=z1(Ke()),l3=z1(s3()),u3=z1(o3()),m2=(...e)=>e.filter(Boolean).join(" "),m3=async(e,a)=>{await(0,l3.mv)(e,a,{force:!0})},Mp=e=>{let a=e.stdout?.toString()||e.message;(0,qp.error)(a),(0,qp.setFailed)(a)},c3=e=>{let a=()=>["| | Covered/Total | Percentage |","|:--| :--: | --:|"],r=d=>{let{covered:s,total:l,pct:m}=e[d];return`| ${(0,u3.capitalize)(d)} | \`${s}/${l}\` | \`${m}%\` ${ay(m)} |`},p=[...a()];return p.push(r("statements")),p.push(r("branches")),p.push(r("functions")),p.push(r("lines")),p.join(` -`)},ay=e=>e>80?"\u{1F7E2}":e>60?"\u{1F7E0}":"\u{1F534}",f3=e=>`coverage/coverage-shard-${e}.json`,$p=e=>`coverage-shard-${e}`,h3=(e,a,r)=>{let p=0;for(let d=0;d(0,v3.getOctokit)(e);var T3=async({token:e,skipArtifactUpload:a,shardCount:r,showAllFilesInSummary:p})=>{(0,di.info)("Merging coverage...");try{if(!a){let v=(0,g3.create)();for(let g=1;g<=r;g++){let y=await v.downloadArtifact($p(g));(0,E3.debug)(`downloadOutput: ${JSON.stringify(y)}`)}}let s=(0,w3.execSync)("npx --yes nyc report --reporter=json-summary --reporter=text -t coverage --report-dir coverage-merged").toString();(0,di.info)("Text Summary:"),(0,di.info)(s);let l=(0,_3.readFileSync)("./coverage-merged/coverage-summary.json"),m=JSON.parse(l.toString()),c=ry(m,s,p);lr.context.payload.pull_request?await c2(e).rest.issues.createComment({...lr.context.repo,issue_number:lr.context.payload.pull_request.number,body:c}):await c2(e).rest.repos.createCommitComment({...lr.context.repo,commit_sha:lr.context.sha,body:c}),(0,di.info)("Merging coverage... DONE")}catch(d){Mp(d)}},ry=(e,a,r)=>{let p=[];if(p.push("### Code Coverage Summary"),p.push(""),p.push(c3(e.total)),r){p.push("
"),p.push(" Code Coverage on All Files"),p.push(""),p.push("### Code Coverage on All Files");let d=iy(a,r);p.push(d),p.push("
"),p.push("")}return p.join(` -`)},iy=(e,a)=>{let r=e.split(` -`).slice(1).filter(Boolean);return r.pop(),r=r.map(p=>{if(p.startsWith(" ")){let d=p.match(/^\s+/);d&&(p=p.replace(/^\s+/,"".padStart(d[0].length*2,"\u2500")+" "))}return p=p.replace(/(\d+([\.\-]\d+)*)/g,py),p}).filter(Boolean),r[1].startsWith("--")&&(r[1]="---|--:|---:|--:|--:|---"),r.join(` -`)},py=(e,...a)=>{if(!e)return"";let r=a[2],p=a[3];return h3(p,"|",5){(0,_a.info)(m2("Running tests",e?"with coverage":null,a&&`for shard ${a}`));try{let p=(0,y3.execSync)(m2("npm run test --",e?"--coverage":null,a&&`--shard ${a}`));(0,_a.info)(p.toString());let d=+a.split("/")[0];(0,jp.debug)(`shardIndex: ${d}`);let s=f3(d);if((0,jp.debug)(`coverageFileName: ${s}`),await m3("coverage/coverage-final.json",s),r){(0,_a.info)("Running tests... DONE");return}(0,_a.info)("Uploading artifacts...");let m=await(0,b3.create)().uploadArtifact($p(d),[s],".",{retentionDays:1});(0,jp.debug)(`outputUploadArtifact: ${JSON.stringify(m)}`),(0,_a.info)("Uploading artifacts... DONE"),(0,_a.info)("Running tests... DONE")}catch(p){Mp(p)}};var S3=async()=>{let e=(0,l1.getInput)("command");switch((0,f2.info)(`Running command: ${e}...`),e){case"run-tests":await A3({coverage:(0,l1.getBooleanInput)("coverage"),shard:(0,l1.getInput)("shard"),skipArtifactUpload:(0,l1.getBooleanInput)("skip-artifact-upload")});break;case"merge-coverage":await T3({token:(0,l1.getInput)("github-token"),skipArtifactUpload:(0,l1.getBooleanInput)("skip-artifact-upload"),shardCount:+(0,l1.getInput)("shard-count"),showAllFilesInSummary:(0,l1.getBooleanInput)("show-all-files-in-summary")});break;default:{let a=`Invalid command: ${e}`;(0,l1.error)(a),(0,l1.setFailed)(a)}}(0,f2.info)(`Running command: ${e}... DONE`)};S3(); +`)},ay=e=>e>80?"\u{1F7E2}":e>60?"\u{1F7E0}":"\u{1F534}",f3=e=>`coverage/coverage-shard-${e}.json`,$p=e=>`coverage-shard-${e}`,h3=(e,a,r)=>{let p=0;for(let d=0;d(0,v3.getOctokit)(e);var T3=async({token:e,skipArtifactUpload:a,shardCount:r,showAllFilesInSummary:p})=>{(0,di.info)("Merging coverage...");try{if(!a){let v=(0,g3.create)();for(let g=1;g<=r;g++){let y=await v.downloadArtifact($p(g));(0,E3.debug)(`downloadOutput: ${JSON.stringify(y)}`)}}let s=(0,w3.execSync)("npx --yes nyc report --reporter=json-summary --reporter=text -t coverage --report-dir coverage-merged").toString();(0,di.info)("Text Summary:"),(0,di.info)(s);let l=(0,_3.readFileSync)("./coverage-merged/coverage-summary.json"),m=JSON.parse(l.toString()),c=ry(m,s,p);lr.context.payload.pull_request?await c2(e).rest.issues.createComment({...lr.context.repo,issue_number:lr.context.payload.pull_request.number,body:c}):await c2(e).rest.repos.createCommitComment({...lr.context.repo,commit_sha:lr.context.sha,body:c}),(0,di.info)("Merging coverage... DONE")}catch(d){Mp(d)}},ry=(e,a,r)=>{let p=[];if(p.push("### Code Coverage Summary"),p.push(""),p.push(c3(e.total)),r){p.push("
"),p.push(" Code Coverage on All Files"),p.push(""),p.push("### Code Coverage on All Files");let d=iy(a);p.push(d),p.push("
"),p.push("")}return p.join(` +`)},iy=e=>{let a=e.split(` +`).slice(1).filter(Boolean);return a.pop(),a=a.map(r=>{if(r.startsWith(" ")){let p=r.match(/^\s+/);p&&(r=r.replace(/^\s+/,"".padStart(p[0].length*2,"\u2500")+" "))}return r=r.replace(/(\d+([\.\-]\d+)*)/g,py),r}).filter(Boolean),a[1].startsWith("--")&&(a[1]="---|--:|---:|--:|--:|---"),a.join(` +`)},py=(e,a,r,p,d)=>e?h3(d,"|",5){(0,_a.info)(m2("Running tests",e?"with coverage":null,a&&`for shard ${a}`));try{let p=(0,y3.execSync)(m2("npm run test --",e?"--coverage":null,a&&`--shard ${a}`));(0,_a.info)(p.toString());let d=+a.split("/")[0];(0,jp.debug)(`shardIndex: ${d}`);let s=f3(d);if((0,jp.debug)(`coverageFileName: ${s}`),await m3("coverage/coverage-final.json",s),r){(0,_a.info)("Running tests... DONE");return}(0,_a.info)("Uploading artifacts...");let m=await(0,b3.create)().uploadArtifact($p(d),[s],".",{retentionDays:1});(0,jp.debug)(`outputUploadArtifact: ${JSON.stringify(m)}`),(0,_a.info)("Uploading artifacts... DONE"),(0,_a.info)("Running tests... DONE")}catch(p){Mp(p)}};var S3=async()=>{let e=(0,l1.getInput)("command");switch((0,f2.info)(`Running command: ${e}...`),e){case"run-tests":await A3({coverage:(0,l1.getBooleanInput)("coverage"),shard:(0,l1.getInput)("shard"),skipArtifactUpload:(0,l1.getBooleanInput)("skip-artifact-upload")});break;case"merge-coverage":await T3({token:(0,l1.getInput)("github-token"),skipArtifactUpload:(0,l1.getBooleanInput)("skip-artifact-upload"),shardCount:+(0,l1.getInput)("shard-count"),showAllFilesInSummary:(0,l1.getBooleanInput)("show-all-files-in-summary")});break;default:{let a=`Invalid command: ${e}`;(0,l1.error)(a),(0,l1.setFailed)(a)}}(0,f2.info)(`Running command: ${e}... DONE`)};S3(); /*! * Tmp * diff --git a/src/sample/add2.spec.ts b/src/sample/add2.spec.ts new file mode 100644 index 0000000..3512029 --- /dev/null +++ b/src/sample/add2.spec.ts @@ -0,0 +1,7 @@ +import { add2 } from "./add2"; + +describe("add2", () => { + it("should add 2 numbers", () => { + expect(add2(1, 2)).toBe(3); + }); +}); diff --git a/src/sample/add2.ts b/src/sample/add2.ts new file mode 100644 index 0000000..073e653 --- /dev/null +++ b/src/sample/add2.ts @@ -0,0 +1,3 @@ +export const add2 = (a: number, b: number) => { + return a + b; +}; diff --git a/src/steps/__sample__/index.ts b/src/steps/__sample__/index.ts index 72c877d..6902b39 100644 --- a/src/steps/__sample__/index.ts +++ b/src/steps/__sample__/index.ts @@ -169,7 +169,7 @@ File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s All files | 25.51 | 18.51 | 16.66 | 21.25 | src | 0 | 0 | 0 | 0 | index.ts | 0 | 0 | 0 | 0 | 1-34 - run.ts | 0 | 0 | 0 | 0 | 1-33 + run3.ts | 0 | 0 | 0 | 0 | 1-33 src/steps | 23.94 | 0 | 40 | 21.53 | merge-coverage.ts | 36.95 | 0 | 66.66 | 33.33 | 21-24,33-60,67-85 run-tests.ts | 0 | 0 | 0 | 0 | 1-42 diff --git a/src/steps/__snapshots__/merge-coverage.spec.ts.snap b/src/steps/__snapshots__/merge-coverage.spec.ts.snap new file mode 100644 index 0000000..d73dcf8 --- /dev/null +++ b/src/steps/__snapshots__/merge-coverage.spec.ts.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`getCoverageDetailsTable should return formatted table 1`] = ` +"File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s +---|--:|---:|--:|--:|--- +All files | \`25.51\` | \`18.51\` | \`16.66\` | \`21.25\` | +── src | \`0.00\` | \`0.00\` | \`0.00\` | \`0.00\` | +──── index.ts | \`0.00\` | \`0.00\` | \`0.00\` | \`0.00\` | \`1-34\` +──── run3.ts | \`0.00\` | \`0.00\` | \`0.00\` | \`0.00\` | \`1-33\` +── src/steps | \`23.94\` | \`0.00\` | \`40.00\` | \`21.53\` | +──── merge-coverage.ts | \`36.95\` | \`0.00\` | \`66.66\` | \`33.33\` | \`21-24\`,\`33-60\`,\`67-85\` +──── run-tests.ts | \`0.00\` | \`0.00\` | \`0.00\` | \`0.00\` | \`1-42\` +── src/utils | \`48.78\` | \`62.50\` | \`10.00\` | \`41.93\` | +──── index.ts | \`48.78\` | \`62.50\` | \`10.00\` | \`41.93\` | \`7\`,\`11\`,\`21-36\`,\`40-43\`,\`47\`,\`51\` " +`; diff --git a/src/steps/merge-coverage.spec.ts b/src/steps/merge-coverage.spec.ts index b2a994c..017ea9a 100644 --- a/src/steps/merge-coverage.spec.ts +++ b/src/steps/merge-coverage.spec.ts @@ -1,5 +1,5 @@ import * as mockedFs from "fs"; -import { mergeCoverage } from "./merge-coverage"; +import { getCoverageDetailsTable, mergeCoverage } from "./merge-coverage"; import { coverageSummary, execSyncOutput } from "./__sample__"; import * as mockedOctokit from "../utils/octokit"; import * as mockedUtils from "../utils"; @@ -80,3 +80,12 @@ describe("merge-coverage", () => { }); }); }); + +describe("getCoverageDetailsTable", () => { + it("should return formatted table", () => { + const coverageSummary = execSyncOutput; + const table = getCoverageDetailsTable(coverageSummary); + + expect(table).toMatchSnapshot(); + }); +}); diff --git a/src/steps/merge-coverage.ts b/src/steps/merge-coverage.ts index 370b255..787c50e 100644 --- a/src/steps/merge-coverage.ts +++ b/src/steps/merge-coverage.ts @@ -87,7 +87,7 @@ export const getCommentBody = ( output.push(""); output.push("### Code Coverage on All Files"); - const _textSummaryBody = getCoverageDetailsTable(textSummary, showAllFilesInSummary); + const _textSummaryBody = getCoverageDetailsTable(textSummary); output.push(_textSummaryBody); output.push(``); @@ -97,7 +97,7 @@ export const getCommentBody = ( return output.join("\n"); }; -const getCoverageDetailsTable = (textSummary: string, showAllFilesInSummary: boolean) => { +export const getCoverageDetailsTable = (textSummary: string) => { let lines = textSummary.split("\n").slice(1).filter(Boolean); lines.pop(); lines = lines @@ -122,16 +122,23 @@ const getCoverageDetailsTable = (textSummary: string, showAllFilesInSummary: boo return lines.join("\n"); }; -export const getFormattedValue = (value: string, ...args) => { +export const getFormattedValue = ( + value: string, + match: string, + _nestedMatch: string, + matchedIndex: number, + line: string +) => { if (!value) return ""; - const matchedIndex = args[2]; - const line = args[3]; - const lastColumnOffset = getNthIndexOfCharacter(line, "|", 5); if (lastColumnOffset < matchedIndex) { return `\`${value}\``; } + if (line.charAt(matchedIndex - 1).match("[A-z]")) { + return value; + } + return `\`${Number(value).toFixed(2)}\``; };