monocart-coverage-reports 2.10.7 → 2.10.8-beta.0

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.
@@ -4,8 +4,10 @@ const Util = require('../utils/util.js');
4
4
 
5
5
  const { pathToFileURL, fileURLToPath } = require('url');
6
6
 
7
+ const { normalizeSourcePath, getSourcePathReplacer } = require('../utils/source-path.js');
8
+
7
9
  // const EC = require('eight-colors');
8
- const { minimatch } = require('../packages/monocart-coverage-vendor.js');
10
+ const { minimatch, convertSourceMap } = require('../packages/monocart-coverage-vendor.js');
9
11
 
10
12
  // ========================================================================================================
11
13
 
@@ -109,16 +111,64 @@ const resolveFileType = (fileType, filePath) => {
109
111
  return 'js';
110
112
  };
111
113
 
112
- const getUntestedCoverageData = (emptyList, options, coverageType) => {
114
+ const saveUntestedFileSource = async (entryFile, options) => {
115
+ const {
116
+ id,
117
+ url,
118
+ source,
119
+ sourceMap
120
+ } = entryFile;
121
+ // console.log('-', entry.sourcePath);
122
+
123
+ const { cachePath } = Util.getCacheFileInfo('source', id, options.cacheDir);
124
+ if (fs.existsSync(cachePath)) {
125
+ return;
126
+ }
127
+
128
+ // save source and sourceMap to separated json file
129
+ const sourceData = {
130
+ id,
131
+ url,
132
+ source,
133
+ sourceMap
134
+ };
135
+
136
+ // remove comments if not debug
137
+ if (!Util.isDebug()) {
138
+ sourceData.source = convertSourceMap.removeComments(source);
139
+ }
140
+
141
+ // console.log('save untested file', id, url);
142
+
143
+ await Util.saveSourceCacheFile(sourceData, options);
144
+
145
+ };
146
+
147
+ const getUntestedCoverageData = async (entryList, options, coverageType) => {
148
+
149
+ // save all empty coverage
150
+ const dataId = Util.uid();
151
+ const results = {
152
+ id: dataId
153
+ };
113
154
 
114
- // NOTE: do not save untested file to raw report
155
+ if (coverageType === 'istanbul') {
156
+ results.type = 'istanbul';
157
+ results.data = {};
158
+ } else {
159
+ results.type = 'v8';
160
+ results.data = [];
161
+ }
115
162
 
116
163
  const emptyCoverageList = [];
117
164
 
118
165
  // save all empty source and sourcemap
119
- for (const entryFile of emptyList) {
166
+ for (const entry of entryList) {
167
+
168
+ // for raw report: source file
169
+ await saveUntestedFileSource(entry, options);
120
170
 
121
- const { type, url } = entryFile;
171
+ const { type, url } = entry;
122
172
 
123
173
  if (coverageType === 'istanbul') {
124
174
 
@@ -133,6 +183,7 @@ const getUntestedCoverageData = (emptyList, options, coverageType) => {
133
183
  b: {}
134
184
  };
135
185
  // object
186
+ results.data[item.path] = item;
136
187
  emptyCoverageList.push(item);
137
188
 
138
189
  } else {
@@ -140,59 +191,99 @@ const getUntestedCoverageData = (emptyList, options, coverageType) => {
140
191
  // ===============================================
141
192
  if (type === 'js') {
142
193
  // empty js
143
- entryFile.functions = entryFile.functions || [{
194
+ entry.functions = entry.functions || [{
144
195
  functionName: '',
145
196
  ranges: [{
146
197
  startOffset: 0,
147
- endOffset: entryFile.source.length,
198
+ endOffset: entry.source.length,
148
199
  count: 0
149
200
  }]
150
201
  }];
151
202
  } else {
152
203
  // empty css
153
- entryFile.ranges = entryFile.ranges || [];
204
+ entry.ranges = entry.ranges || [];
154
205
  }
155
206
 
207
+ const item = {
208
+ ... entry
209
+ };
210
+ delete item.source;
211
+ delete item.sourceMap;
212
+
213
+ // array
214
+ results.data.push(item);
156
215
  // will be parsed to AST and converted to V8 coverage
157
- emptyCoverageList.push(entryFile);
216
+ emptyCoverageList.push(entry);
158
217
 
159
218
  }
160
219
 
161
220
  }
162
221
 
222
+ // for raw report: coverage file
223
+ const { cachePath } = Util.getCacheFileInfo('coverage', dataId, options.cacheDir);
224
+ await Util.writeFile(cachePath, JSON.stringify(results));
225
+
163
226
  return emptyCoverageList;
164
227
  };
165
228
 
166
- const getEmptyCoverages = async (fileList, options, coverageType, fileTransformer) => {
229
+ const getEmptyCoverages = async (testedMap, options, coverageType, fileList, fileTransformer) => {
230
+ const sourceFilter = Util.getSourceFilter(options);
231
+ const sourcePathReplacer = getSourcePathReplacer(options);
232
+ const baseDir = options.baseDir;
167
233
 
168
- const emptyList = [];
234
+ // console.log(fileList);
169
235
 
236
+ const entryList = [];
170
237
  for (const item of fileList) {
171
- const {
172
- filePath, fileType, sourcePath
173
- } = item;
238
+
239
+ const { fileType, filePath } = item;
174
240
  const type = resolveFileType(fileType, filePath);
175
241
  const url = pathToFileURL(filePath).toString();
176
242
  const source = Util.readFileSync(filePath);
177
- const entryFile = {
243
+ const entry = {
178
244
  empty: true,
179
245
  type,
180
246
  url,
181
- sourcePath,
182
- // for empty, css supports both source and text
183
247
  source
184
248
  };
185
249
 
186
- await fileTransformer(entryFile, coverageType);
250
+ // normalize sourcePath here
251
+ let sourcePath = normalizeSourcePath(url, baseDir);
252
+ if (sourcePathReplacer) {
253
+ const newSourcePath = sourcePathReplacer(sourcePath, entry);
254
+ if (typeof newSourcePath === 'string' && newSourcePath) {
255
+ sourcePath = newSourcePath;
256
+ }
257
+ }
258
+
259
+ // console.log(sourcePath);
260
+
261
+ if (testedMap.has(sourcePath)) {
262
+ continue;
263
+ }
264
+
265
+ if (!sourceFilter(sourcePath)) {
266
+ continue;
267
+ }
268
+
269
+ entry.sourcePath = sourcePath;
187
270
 
271
+ await fileTransformer(entry, coverageType);
188
272
  // after transformer
189
- entryFile.id = Util.calculateSha1(entryFile.sourcePath + entryFile.source);
273
+ entry.id = Util.calculateSha1(entry.sourcePath + entry.source);
190
274
 
191
- emptyList.push(entryFile);
275
+ entryList.push(entry);
192
276
  }
193
277
 
278
+
279
+ // console.log('fileList', fileList);
280
+ if (!entryList.length) {
281
+ return;
282
+ }
283
+
284
+
194
285
  // save empty coverage for merging raw reports
195
- return getUntestedCoverageData(emptyList, options, coverageType);
286
+ return getUntestedCoverageData(entryList, options, coverageType);
196
287
  };
197
288
 
198
289
  const getUntestedList = (testedMap, options, coverageType = 'v8') => {
@@ -201,44 +292,34 @@ const getUntestedList = (testedMap, options, coverageType = 'v8') => {
201
292
  if (!allOptions) {
202
293
  return;
203
294
  }
295
+
204
296
  const {
205
297
  dirList, fileFilter, fileTransformer
206
298
  } = allOptions;
207
299
 
208
- const sourceFilter = Util.getSourceFilter(options);
209
-
210
300
  const fileList = [];
211
301
  dirList.forEach((dir) => {
212
302
  Util.forEachFile(dir, [], (fileName, fileDir) => {
213
303
  const filePath = path.resolve(fileDir, fileName);
214
304
  // return file extname for file type
215
305
  const fileType = fileFilter(filePath);
216
- if (!fileType) {
217
- return;
306
+ if (fileType) {
307
+ fileList.push({
308
+ filePath,
309
+ fileType,
310
+ fileDir,
311
+ fileName
312
+ });
218
313
  }
219
314
 
220
- const sourcePath = Util.relativePath(filePath);
221
- if (testedMap.has(sourcePath)) {
222
- return;
223
- }
224
-
225
- if (!sourceFilter(sourcePath)) {
226
- return;
227
- }
228
-
229
- fileList.push({
230
- filePath,
231
- fileType,
232
- sourcePath
233
- });
234
315
  });
235
316
  });
236
- // console.log('fileList', fileList);
317
+
237
318
  if (!fileList.length) {
238
319
  return;
239
320
  }
240
321
 
241
- return getEmptyCoverages(fileList, options, coverageType, fileTransformer);
322
+ return getEmptyCoverages(testedMap, options, coverageType, fileList, fileTransformer);
242
323
 
243
324
  };
244
325
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "monocart-coverage-reports",
3
- "version": "2.10.7",
3
+ "version": "2.10.8-beta.0",
4
4
  "description": "A code coverage tool to generate native V8 reports or Istanbul reports.",
5
5
  "main": "./lib/index.js",
6
6
  "bin": {
@@ -111,4 +111,4 @@
111
111
  "vine-ui": "^3.1.16",
112
112
  "ws": "^8.18.0"
113
113
  }
114
- }
114
+ }