monocart-coverage-reports 2.8.2 → 2.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,385 +1,385 @@
1
- const CG = require('console-grid');
2
- const EC = require('eight-colors');
3
-
4
- const Util = require('./util.js');
5
-
6
- const getUncoveredLines = (file) => {
7
- const lines = [];
8
-
9
- const dataLines = file.data.lines;
10
-
11
- let startLine;
12
- let endLine;
13
-
14
- const addLines = () => {
15
- if (!startLine) {
16
- return;
17
- }
18
- if (endLine) {
19
- // range
20
- lines.push(`${startLine.line}-${endLine.line}`);
21
- startLine = null;
22
- endLine = null;
23
- } else {
24
- // only start
25
- lines.push(startLine.line);
26
- startLine = null;
27
- }
28
- };
29
-
30
- const setLines = (line) => {
31
- if (startLine) {
32
- endLine = {
33
- line
34
- };
35
- return;
36
- }
37
- startLine = {
38
- line
39
- };
40
- };
41
-
42
- Object.keys(dataLines).forEach((line) => {
43
- const count = dataLines[line];
44
- if (count === 0 || typeof count === 'string') {
45
- setLines(line);
46
- return;
47
- }
48
- // count >= 1
49
- addLines();
50
- });
51
- addLines();
52
-
53
- return lines.join(',');
54
- };
55
-
56
- const mergeSingleSubGroups = (item) => {
57
-
58
- if (!item.subs) {
59
- return;
60
- }
61
- if (item.subs.length === 1) {
62
- const sub = item.subs[0];
63
- if (!sub.subs) {
64
- return;
65
- }
66
- item.name = [item.name, sub.name].filter((it) => it).join('/');
67
- item.subs = sub.subs;
68
- mergeSingleSubGroups(item);
69
- return;
70
- }
71
-
72
- item.subs.forEach((sub) => {
73
- mergeSingleSubGroups(sub);
74
- });
75
-
76
- };
77
-
78
- const getGroupedRows = (flatRows) => {
79
- let groups = [];
80
- flatRows.forEach((file) => {
81
- const pathList = file.name.split('/');
82
- const lastName = pathList.pop();
83
- let subs = groups;
84
- pathList.forEach((key) => {
85
- const item = subs.find((it) => it.name === key && it.subs);
86
- if (item) {
87
- subs = item.subs;
88
- return;
89
- }
90
- const sub = {
91
- name: key,
92
- subs: []
93
- };
94
- subs.push(sub);
95
- subs = sub.subs;
96
- });
97
-
98
- let filename = lastName;
99
- if (file.nameStatus) {
100
- filename = Util.getColorStrByStatus(lastName, file.nameStatus);
101
- }
102
- file.name = filename;
103
- subs.push(file);
104
- });
105
- const group = {
106
- subs: groups
107
- };
108
- mergeSingleSubGroups(group);
109
- if (group.name) {
110
- groups = [group];
111
- }
112
- return groups;
113
- };
114
-
115
- // ============================================================
116
-
117
- const diffUncoveredLines = (list) => {
118
- const [oldStr, newStr] = list;
119
-
120
- const oldList = oldStr.split(',');
121
- const newList = newStr.split(',');
122
-
123
- const oldColor = oldList.map((it) => {
124
- if (newList.includes(it)) {
125
- return it;
126
- }
127
- return EC.green(it);
128
- }).join(',');
129
-
130
- const newColor = newList.map((it) => {
131
- if (oldList.includes(it)) {
132
- return it;
133
- }
134
- return EC.red(it);
135
- }).join(',');
136
-
137
- return [oldColor, newColor];
138
- };
139
-
140
- const getDiffMessage = (results, columns, diffOptions) => {
141
- const flatRows = [];
142
- results.forEach((it) => {
143
- if (!it.change && diffOptions.skipEqual) {
144
- return;
145
- }
146
- flatRows.push(it);
147
- });
148
-
149
- const rows = getGroupedRows(flatRows);
150
-
151
- const addedList = [];
152
- Util.forEach(rows, (it, parent) => {
153
- if (!it.change) {
154
- return;
155
- }
156
- // add color
157
- if (it.change === '-' || it.change === '+') {
158
- columns.forEach((c) => {
159
- const k = c.id;
160
- let v = it[k];
161
- if (k === 'name') {
162
- v = `${it.change} ${v}`;
163
- }
164
- it[k] = EC.red(v);
165
- });
166
- return;
167
- }
168
-
169
- // diff
170
- const cloneRow = {};
171
- columns.forEach((c) => {
172
- const k = c.id;
173
- let v = it[k];
174
- if (Array.isArray(v)) {
175
- if (k === 'uncoveredLines') {
176
- v = diffUncoveredLines(v);
177
- } else {
178
- v = [EC.green(v[0]), EC.red(v[1])];
179
- }
180
- it[k] = v[0];
181
- cloneRow[k] = v[1];
182
- return;
183
- }
184
- it[k] = v;
185
- cloneRow[k] = '';
186
- });
187
-
188
- addedList.push({
189
- subs: parent ? parent.subs : rows,
190
- row: it,
191
- cloneRow
192
- });
193
-
194
- });
195
-
196
- addedList.forEach((it) => {
197
- const {
198
- subs, row, cloneRow
199
- } = it;
200
- const i = subs.findIndex((r) => r === row);
201
- subs.splice(i + 1, 0, cloneRow);
202
- });
203
-
204
- // add innerBorder for summary
205
- const summaryIndex = rows.findIndex((it) => it.isSummary);
206
- if (summaryIndex !== -1) {
207
- rows.splice(summaryIndex, 0, {
208
- innerBorder: true
209
- });
210
- }
211
-
212
- const lines = CG({
213
- options: {
214
- silent: true,
215
- nullPlaceholder: '',
216
- defaultMaxWidth: diffOptions.maxCols
217
- },
218
- columns,
219
- rows
220
- });
221
-
222
- return lines.join('\n');
223
- };
224
-
225
-
226
- // ============================================================
227
-
228
- const getSnapshot = (reportData) => {
229
- const {
230
- summary, files, type
231
- } = reportData;
232
- const snapshot = {
233
- type,
234
- summary: {},
235
- files: {}
236
- };
237
-
238
- const addPercent = (target, fromSummary) => {
239
- Object.keys(fromSummary).forEach((k) => {
240
- let percent = fromSummary[k].pct;
241
- if (typeof percent === 'number') {
242
- percent = Util.PSF(percent, 100, 2);
243
- }
244
- target[k] = percent;
245
- });
246
- };
247
-
248
- addPercent(snapshot.summary, summary);
249
-
250
- files.forEach((file) => {
251
- // do NOT add debug file
252
- if (file.debug) {
253
- return;
254
- }
255
- const fileSummary = {};
256
- addPercent(fileSummary, file.summary);
257
- fileSummary.uncoveredLines = getUncoveredLines(file);
258
- snapshot.files[file.sourcePath] = fileSummary;
259
- });
260
-
261
- return snapshot;
262
- };
263
-
264
- const diffSnapshot = (oldData, newData, diffOptions) => {
265
-
266
- diffOptions = {
267
- skipEqual: true,
268
- showSummary: true,
269
- maxCols: 50,
270
- metrics: [],
271
- ... diffOptions
272
- };
273
-
274
- let change = false;
275
- const results = [];
276
-
277
- const metrics = Util.getMetrics(diffOptions.metrics, oldData.type);
278
- const columns = [{
279
- id: 'name',
280
- name: 'Name'
281
- }, ... metrics.map((m) => {
282
- return {
283
- id: m,
284
- name: Util.capitalizeFirstLetter(m),
285
- align: 'right'
286
- };
287
- }), {
288
- id: 'uncoveredLines',
289
- name: 'Uncovered Lines'
290
- }];
291
-
292
- const keys = columns.map((it) => it.id).filter((it) => it !== 'name');
293
- const oFiles = oldData.files;
294
- const nFiles = newData.files;
295
-
296
- Object.keys(oFiles).forEach((oPath) => {
297
-
298
- const oSummary = oFiles[oPath];
299
-
300
- const diffResult = {
301
- name: oPath
302
- };
303
- keys.forEach((k) => {
304
- diffResult[k] = oSummary[k];
305
- });
306
-
307
- const nSummary = nFiles[oPath];
308
- if (nSummary) {
309
-
310
- let fileChange = false;
311
- keys.forEach((k) => {
312
- const oValue = oSummary[k];
313
- const nValue = nSummary[k];
314
- if (oValue !== nValue) {
315
- change = true;
316
- fileChange = true;
317
- diffResult[k] = [oValue, nValue];
318
- }
319
- });
320
-
321
- diffResult.change = fileChange;
322
-
323
- } else {
324
- change = true;
325
- diffResult.change = '-';
326
- }
327
-
328
- results.push(diffResult);
329
-
330
- });
331
-
332
- // added
333
- Object.keys(nFiles).forEach((nPath) => {
334
- const oSummary = oFiles[nPath];
335
- if (oSummary) {
336
- return;
337
- }
338
- const nSummary = nFiles[nPath];
339
- const diffResult = {
340
- name: nPath,
341
- change: '+'
342
- };
343
- change = true;
344
- keys.forEach((k) => {
345
- diffResult[k] = nSummary[k];
346
- });
347
- results.push(diffResult);
348
- });
349
-
350
- if (diffOptions.showSummary) {
351
- const oSummary = oldData.summary;
352
- const nSummary = newData.summary;
353
- const diffResult = {
354
- name: 'Summary',
355
- isSummary: true,
356
- change: false
357
- };
358
- keys.forEach((k) => {
359
- const oValue = oSummary[k];
360
- const nValue = nSummary[k];
361
- if (oValue === nValue) {
362
- diffResult[k] = oValue;
363
- } else {
364
- change = true;
365
- diffResult.change = true;
366
- diffResult[k] = [oValue, nValue];
367
- }
368
- });
369
- results.push(diffResult);
370
- }
371
-
372
- const message = getDiffMessage(results, columns, diffOptions);
373
-
374
- return {
375
- change,
376
- results,
377
- message
378
- };
379
- };
380
-
381
- module.exports = {
382
- getGroupedRows,
383
- getSnapshot,
384
- diffSnapshot
385
- };
1
+ const CG = require('console-grid');
2
+ const EC = require('eight-colors');
3
+
4
+ const Util = require('./util.js');
5
+
6
+ const getUncoveredLines = (file) => {
7
+ const lines = [];
8
+
9
+ const dataLines = file.data.lines;
10
+
11
+ let startLine;
12
+ let endLine;
13
+
14
+ const addLines = () => {
15
+ if (!startLine) {
16
+ return;
17
+ }
18
+ if (endLine) {
19
+ // range
20
+ lines.push(`${startLine.line}-${endLine.line}`);
21
+ startLine = null;
22
+ endLine = null;
23
+ } else {
24
+ // only start
25
+ lines.push(startLine.line);
26
+ startLine = null;
27
+ }
28
+ };
29
+
30
+ const setLines = (line) => {
31
+ if (startLine) {
32
+ endLine = {
33
+ line
34
+ };
35
+ return;
36
+ }
37
+ startLine = {
38
+ line
39
+ };
40
+ };
41
+
42
+ Object.keys(dataLines).forEach((line) => {
43
+ const count = dataLines[line];
44
+ if (count === 0 || typeof count === 'string') {
45
+ setLines(line);
46
+ return;
47
+ }
48
+ // count >= 1
49
+ addLines();
50
+ });
51
+ addLines();
52
+
53
+ return lines.join(',');
54
+ };
55
+
56
+ const mergeSingleSubGroups = (item) => {
57
+
58
+ if (!item.subs) {
59
+ return;
60
+ }
61
+ if (item.subs.length === 1) {
62
+ const sub = item.subs[0];
63
+ if (!sub.subs) {
64
+ return;
65
+ }
66
+ item.name = [item.name, sub.name].filter((it) => it).join('/');
67
+ item.subs = sub.subs;
68
+ mergeSingleSubGroups(item);
69
+ return;
70
+ }
71
+
72
+ item.subs.forEach((sub) => {
73
+ mergeSingleSubGroups(sub);
74
+ });
75
+
76
+ };
77
+
78
+ const getGroupedRows = (flatRows) => {
79
+ let groups = [];
80
+ flatRows.forEach((file) => {
81
+ const pathList = file.name.split('/');
82
+ const lastName = pathList.pop();
83
+ let subs = groups;
84
+ pathList.forEach((key) => {
85
+ const item = subs.find((it) => it.name === key && it.subs);
86
+ if (item) {
87
+ subs = item.subs;
88
+ return;
89
+ }
90
+ const sub = {
91
+ name: key,
92
+ subs: []
93
+ };
94
+ subs.push(sub);
95
+ subs = sub.subs;
96
+ });
97
+
98
+ let filename = lastName;
99
+ if (file.nameStatus) {
100
+ filename = Util.getColorStrByStatus(lastName, file.nameStatus);
101
+ }
102
+ file.name = filename;
103
+ subs.push(file);
104
+ });
105
+ const group = {
106
+ subs: groups
107
+ };
108
+ mergeSingleSubGroups(group);
109
+ if (group.name) {
110
+ groups = [group];
111
+ }
112
+ return groups;
113
+ };
114
+
115
+ // ============================================================
116
+
117
+ const diffUncoveredLines = (list) => {
118
+ const [oldStr, newStr] = list;
119
+
120
+ const oldList = oldStr.split(',');
121
+ const newList = newStr.split(',');
122
+
123
+ const oldColor = oldList.map((it) => {
124
+ if (newList.includes(it)) {
125
+ return it;
126
+ }
127
+ return EC.green(it);
128
+ }).join(',');
129
+
130
+ const newColor = newList.map((it) => {
131
+ if (oldList.includes(it)) {
132
+ return it;
133
+ }
134
+ return EC.red(it);
135
+ }).join(',');
136
+
137
+ return [oldColor, newColor];
138
+ };
139
+
140
+ const getDiffMessage = (results, columns, diffOptions) => {
141
+ const flatRows = [];
142
+ results.forEach((it) => {
143
+ if (!it.change && diffOptions.skipEqual) {
144
+ return;
145
+ }
146
+ flatRows.push(it);
147
+ });
148
+
149
+ const rows = getGroupedRows(flatRows);
150
+
151
+ const addedList = [];
152
+ Util.forEach(rows, (it, parent) => {
153
+ if (!it.change) {
154
+ return;
155
+ }
156
+ // add color
157
+ if (it.change === '-' || it.change === '+') {
158
+ columns.forEach((c) => {
159
+ const k = c.id;
160
+ let v = it[k];
161
+ if (k === 'name') {
162
+ v = `${it.change} ${v}`;
163
+ }
164
+ it[k] = EC.red(v);
165
+ });
166
+ return;
167
+ }
168
+
169
+ // diff
170
+ const cloneRow = {};
171
+ columns.forEach((c) => {
172
+ const k = c.id;
173
+ let v = it[k];
174
+ if (Array.isArray(v)) {
175
+ if (k === 'uncoveredLines') {
176
+ v = diffUncoveredLines(v);
177
+ } else {
178
+ v = [EC.green(v[0]), EC.red(v[1])];
179
+ }
180
+ it[k] = v[0];
181
+ cloneRow[k] = v[1];
182
+ return;
183
+ }
184
+ it[k] = v;
185
+ cloneRow[k] = '';
186
+ });
187
+
188
+ addedList.push({
189
+ subs: parent ? parent.subs : rows,
190
+ row: it,
191
+ cloneRow
192
+ });
193
+
194
+ });
195
+
196
+ addedList.forEach((it) => {
197
+ const {
198
+ subs, row, cloneRow
199
+ } = it;
200
+ const i = subs.findIndex((r) => r === row);
201
+ subs.splice(i + 1, 0, cloneRow);
202
+ });
203
+
204
+ // add innerBorder for summary
205
+ const summaryIndex = rows.findIndex((it) => it.isSummary);
206
+ if (summaryIndex !== -1) {
207
+ rows.splice(summaryIndex, 0, {
208
+ innerBorder: true
209
+ });
210
+ }
211
+
212
+ const lines = CG({
213
+ options: {
214
+ silent: true,
215
+ nullPlaceholder: '',
216
+ defaultMaxWidth: diffOptions.maxCols
217
+ },
218
+ columns,
219
+ rows
220
+ });
221
+
222
+ return lines.join('\n');
223
+ };
224
+
225
+
226
+ // ============================================================
227
+
228
+ const getSnapshot = (reportData) => {
229
+ const {
230
+ summary, files, type
231
+ } = reportData;
232
+ const snapshot = {
233
+ type,
234
+ summary: {},
235
+ files: {}
236
+ };
237
+
238
+ const addPercent = (target, fromSummary) => {
239
+ Object.keys(fromSummary).forEach((k) => {
240
+ let percent = fromSummary[k].pct;
241
+ if (typeof percent === 'number') {
242
+ percent = Util.PSF(percent, 100, 2);
243
+ }
244
+ target[k] = percent;
245
+ });
246
+ };
247
+
248
+ addPercent(snapshot.summary, summary);
249
+
250
+ files.forEach((file) => {
251
+ // do NOT add debug file
252
+ if (file.debug) {
253
+ return;
254
+ }
255
+ const fileSummary = {};
256
+ addPercent(fileSummary, file.summary);
257
+ fileSummary.uncoveredLines = getUncoveredLines(file);
258
+ snapshot.files[file.sourcePath] = fileSummary;
259
+ });
260
+
261
+ return snapshot;
262
+ };
263
+
264
+ const diffSnapshot = (oldData, newData, diffOptions) => {
265
+
266
+ diffOptions = {
267
+ skipEqual: true,
268
+ showSummary: true,
269
+ maxCols: 50,
270
+ metrics: [],
271
+ ... diffOptions
272
+ };
273
+
274
+ let change = false;
275
+ const results = [];
276
+
277
+ const metrics = Util.getMetrics(diffOptions.metrics, oldData.type);
278
+ const columns = [{
279
+ id: 'name',
280
+ name: 'Name'
281
+ }, ... metrics.map((m) => {
282
+ return {
283
+ id: m,
284
+ name: Util.capitalizeFirstLetter(m),
285
+ align: 'right'
286
+ };
287
+ }), {
288
+ id: 'uncoveredLines',
289
+ name: 'Uncovered Lines'
290
+ }];
291
+
292
+ const keys = columns.map((it) => it.id).filter((it) => it !== 'name');
293
+ const oFiles = oldData.files;
294
+ const nFiles = newData.files;
295
+
296
+ Object.keys(oFiles).forEach((oPath) => {
297
+
298
+ const oSummary = oFiles[oPath];
299
+
300
+ const diffResult = {
301
+ name: oPath
302
+ };
303
+ keys.forEach((k) => {
304
+ diffResult[k] = oSummary[k];
305
+ });
306
+
307
+ const nSummary = nFiles[oPath];
308
+ if (nSummary) {
309
+
310
+ let fileChange = false;
311
+ keys.forEach((k) => {
312
+ const oValue = oSummary[k];
313
+ const nValue = nSummary[k];
314
+ if (oValue !== nValue) {
315
+ change = true;
316
+ fileChange = true;
317
+ diffResult[k] = [oValue, nValue];
318
+ }
319
+ });
320
+
321
+ diffResult.change = fileChange;
322
+
323
+ } else {
324
+ change = true;
325
+ diffResult.change = '-';
326
+ }
327
+
328
+ results.push(diffResult);
329
+
330
+ });
331
+
332
+ // added
333
+ Object.keys(nFiles).forEach((nPath) => {
334
+ const oSummary = oFiles[nPath];
335
+ if (oSummary) {
336
+ return;
337
+ }
338
+ const nSummary = nFiles[nPath];
339
+ const diffResult = {
340
+ name: nPath,
341
+ change: '+'
342
+ };
343
+ change = true;
344
+ keys.forEach((k) => {
345
+ diffResult[k] = nSummary[k];
346
+ });
347
+ results.push(diffResult);
348
+ });
349
+
350
+ if (diffOptions.showSummary) {
351
+ const oSummary = oldData.summary;
352
+ const nSummary = newData.summary;
353
+ const diffResult = {
354
+ name: 'Summary',
355
+ isSummary: true,
356
+ change: false
357
+ };
358
+ keys.forEach((k) => {
359
+ const oValue = oSummary[k];
360
+ const nValue = nSummary[k];
361
+ if (oValue === nValue) {
362
+ diffResult[k] = oValue;
363
+ } else {
364
+ change = true;
365
+ diffResult.change = true;
366
+ diffResult[k] = [oValue, nValue];
367
+ }
368
+ });
369
+ results.push(diffResult);
370
+ }
371
+
372
+ const message = getDiffMessage(results, columns, diffOptions);
373
+
374
+ return {
375
+ change,
376
+ results,
377
+ message
378
+ };
379
+ };
380
+
381
+ module.exports = {
382
+ getGroupedRows,
383
+ getSnapshot,
384
+ diffSnapshot
385
+ };