monocart-coverage-reports 2.5.6 → 2.5.8

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.
@@ -0,0 +1,62 @@
1
+ const http = require('http');
2
+ const https = require('https');
3
+
4
+ // minimal http request for get sourcemap json
5
+ const request = (options) => {
6
+
7
+ const url = `${options.url}`;
8
+ // console.log('request', url);
9
+
10
+ return new Promise((resolve) => {
11
+
12
+ const HTTP = url.startsWith('https') ? https : http;
13
+
14
+ HTTP.get(url, (res) => {
15
+ const { statusCode } = res;
16
+
17
+ // Any 2xx status code signals a successful response but here we're only checking for 200.
18
+ if (statusCode !== 200) {
19
+ // Consume response data to free up memory
20
+ res.resume();
21
+ const resErr = new Error(`Request Failed. Status Code: ${statusCode}`);
22
+ resolve([resErr, null]);
23
+ return;
24
+ }
25
+
26
+ res.setEncoding('utf8');
27
+
28
+ let rawData = '';
29
+ res.on('data', (chunk) => {
30
+ rawData += chunk;
31
+ });
32
+
33
+ res.on('end', () => {
34
+
35
+ let dataErr;
36
+ let data = rawData;
37
+
38
+ // parse json
39
+ const contentType = res.headers['content-type'];
40
+ if ((/^application\/json/).test(contentType)) {
41
+ try {
42
+ data = JSON.parse(rawData);
43
+ } catch (e) {
44
+ dataErr = e;
45
+ }
46
+ }
47
+
48
+ res.data = data;
49
+ resolve([dataErr, res]);
50
+
51
+ });
52
+
53
+ }).on('error', (httpErr) => {
54
+
55
+ resolve([httpErr, null]);
56
+
57
+ });
58
+
59
+ });
60
+ };
61
+
62
+ module.exports = request;
@@ -72,10 +72,32 @@ const getSourceType = (sourcePath) => {
72
72
  return type;
73
73
  };
74
74
 
75
+
76
+ // ========================================================================================================
77
+
78
+ const getSourcePathHandler = (options) => {
79
+ const input = options.sourcePath;
80
+ if (typeof input === 'function') {
81
+ return input;
82
+ }
83
+ // object replace key with value
84
+ if (input && typeof input === 'object') {
85
+ const keys = Object.keys(input).filter((k) => typeof input[k] === 'string');
86
+ if (keys.length) {
87
+ return (filePath) => {
88
+ keys.forEach((k) => {
89
+ filePath = filePath.replace(k, input[k]);
90
+ });
91
+ return filePath;
92
+ };
93
+ }
94
+ }
95
+ };
96
+
75
97
  // ========================================================================================================
76
98
 
77
99
  const initIstanbulSourcePath = (coverageData, fileSources, sourcePathHandler) => {
78
- if (typeof sourcePathHandler !== 'function') {
100
+ if (!sourcePathHandler) {
79
101
  return coverageData;
80
102
  }
81
103
 
@@ -121,7 +143,7 @@ const initSourceMapSourcePath = (sourceMap, fileUrls, sourcePathHandler) => {
121
143
  const sourceUrl = mergeSourceRoot(sourceRoot, sourceName);
122
144
 
123
145
  let sourcePath = getSourcePath(sourceUrl, i + 1);
124
- if (typeof sourcePathHandler === 'function') {
146
+ if (sourcePathHandler) {
125
147
  const newSourcePath = sourcePathHandler(sourcePath, sourceUrl);
126
148
  if (typeof newSourcePath === 'string' && newSourcePath) {
127
149
  sourcePath = newSourcePath;
@@ -137,6 +159,7 @@ const initSourceMapSourcePath = (sourceMap, fileUrls, sourcePathHandler) => {
137
159
  module.exports = {
138
160
  getSourcePath,
139
161
  getSourceType,
162
+ getSourcePathHandler,
140
163
  initIstanbulSourcePath,
141
164
  initSourceMapSourcePath
142
165
  };
package/lib/utils/util.js CHANGED
@@ -7,6 +7,7 @@ const crypto = require('crypto');
7
7
  const EC = require('eight-colors');
8
8
  const CG = require('console-grid');
9
9
  const Share = require('../platform/share.js');
10
+ const request = require('./request.js');
10
11
  const { deflateSync } = require('lz-utils');
11
12
 
12
13
  const { acornWalk, supportsColor } = require('../packages/monocart-coverage-vendor.js');
@@ -25,6 +26,8 @@ const Util = {
25
26
 
26
27
  root: process.cwd(),
27
28
 
29
+ request,
30
+
28
31
  relativePath: function(p, root) {
29
32
  p = `${p}`;
30
33
  root = `${root || Util.root}`;
@@ -499,20 +502,12 @@ const Util = {
499
502
  console.log(`[MCR] ${message}`);
500
503
  },
501
504
 
502
- // time is debug level
505
+ // time is debug level, fix the log info, for checking snapshot
503
506
  logTime: (message, time_start) => {
504
- if (Util.loggingLevel < Util.loggingLevels.info) {
507
+ if (Util.loggingLevel < Util.loggingLevels.debug) {
505
508
  return;
506
509
  }
507
-
508
- const slowDuration = 1000;
509
510
  const duration = Date.now() - time_start;
510
- if (Util.loggingLevel < Util.loggingLevels.debug) {
511
- if (duration < slowDuration) {
512
- return;
513
- }
514
- }
515
-
516
511
  const durationH = Util.TSF(duration);
517
512
  const ls = [`[MCR] ${message}`, ' ('];
518
513
  if (duration <= 100) {
@@ -5,6 +5,14 @@ const getV8SummaryBytes = (item) => {
5
5
  const source = item.source;
6
6
  const total = source.length;
7
7
 
8
+ // for empty coverage
9
+ if (item.empty) {
10
+ return {
11
+ total,
12
+ covered: 0
13
+ };
14
+ }
15
+
8
16
  const uncoveredBytes = item.data.bytes.filter((range) => range.count === 0);
9
17
 
10
18
  let uncovered = 0;
package/lib/v8/v8.js CHANGED
@@ -4,7 +4,7 @@ const EC = require('eight-colors');
4
4
  const Util = require('../utils/util.js');
5
5
  const { getV8Summary } = require('./v8-summary.js');
6
6
  const { dedupeFlatRanges } = require('../utils/dedupe.js');
7
- const { getSourcePath } = require('../utils/source-path.js');
7
+ const { getSourcePath, getSourcePathHandler } = require('../utils/source-path.js');
8
8
  const { mergeScriptCovs, minimatch } = require('../packages/monocart-coverage-vendor.js');
9
9
  const { collectSourceMaps } = require('../converter/collect-source-maps.js');
10
10
  const version = require('../../package.json').version;
@@ -24,17 +24,35 @@ const getWrapperSource = (offset, source) => {
24
24
  };
25
25
 
26
26
  const getEntryFilter = (options) => {
27
- const entryFilter = options.entryFilter;
28
- if (typeof entryFilter === 'function') {
29
- return entryFilter;
27
+ const input = options.entryFilter;
28
+
29
+ // for function handler
30
+ if (typeof input === 'function') {
31
+ return input;
30
32
  }
31
33
 
32
- if (entryFilter && typeof entryFilter === 'string') {
34
+ // for single minimatch pattern
35
+ if (input && typeof input === 'string') {
33
36
  return (entry) => {
34
- return minimatch(entry.url, entryFilter);
37
+ return minimatch(entry.url, input);
38
+ };
39
+ }
40
+
41
+ // for patterns
42
+ if (input && typeof input === 'object') {
43
+ const patterns = Object.keys(input);
44
+ return (entry) => {
45
+ const url = entry.url;
46
+ for (const pattern of patterns) {
47
+ if (minimatch(url, pattern)) {
48
+ return input[pattern];
49
+ }
50
+ }
51
+ // false if not matched
35
52
  };
36
53
  }
37
54
 
55
+ // default
38
56
  return () => true;
39
57
  };
40
58
 
@@ -78,7 +96,7 @@ const initV8ListAndSourcemap = async (v8list, options) => {
78
96
 
79
97
  // do not change original v8list, to work for multiple APIs
80
98
 
81
- const sourcePathHandler = options.sourcePath;
99
+ const sourcePathHandler = getSourcePathHandler(options);
82
100
 
83
101
  // init id, sourcePath
84
102
  v8list = v8list.map((item, i) => {
@@ -119,7 +137,7 @@ const initV8ListAndSourcemap = async (v8list, options) => {
119
137
 
120
138
  // resolve source path
121
139
  let sourcePath = getSourcePath(data.url, i + 1, data.type);
122
- if (typeof sourcePathHandler === 'function') {
140
+ if (sourcePathHandler) {
123
141
  const newSourcePath = sourcePathHandler(sourcePath, data.url);
124
142
  if (typeof newSourcePath === 'string' && newSourcePath) {
125
143
  sourcePath = newSourcePath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monocart-coverage-reports",
3
- "version": "2.5.6",
3
+ "version": "2.5.8",
4
4
  "description": "Monocart coverage reports",
5
5
  "main": "./lib/index.js",
6
6
  "bin": {
@@ -31,7 +31,7 @@
31
31
  "test-istanbul": "node ./test/test-istanbul.js",
32
32
  "test-v8": "node ./test/test-v8.js",
33
33
  "test-puppeteer": "node ./test/test-puppeteer.js",
34
- "test-browser": "node ./test/test.js && npm run test-istanbul && npm run test-v8 && npm run test-puppeteer && npm run test-anonymous && npm run test-css",
34
+ "test-browser": "node ./test/test-browser.js && npm run test-istanbul && npm run test-v8 && npm run test-puppeteer && npm run test-anonymous && npm run test-css",
35
35
  "test-anonymous": "node ./test/test-anonymous.js",
36
36
  "test-css": "node ./test/test-css.js",
37
37
  "test-cli": "npx mcr \"node ./test/test-node-env.js\" -o docs/cli -c test/cli-options.js",