-
Notifications
You must be signed in to change notification settings - Fork 0
/
coverageUnusedFiles.js
89 lines (81 loc) · 3.73 KB
/
coverageUnusedFiles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
let path = require('path'),
fs = require('fs'),
nyc = require('nyc'),
controlsPath = path.join(__dirname, 'application', 'Controls'),
componentsPath = path.join(__dirname, 'application', 'SBIS3.CONTROLS'),
coveragePath = require('./package.json')['saby-units']['jsonCoverageReport'],
coverageAllPath = path.join(__dirname, 'artifacts', 'coverageAll.json'),
coverageControlsPath = path.join(__dirname, 'artifacts', 'coverageControls.json'),
coverageComponentsPath = path.join(__dirname, 'artifacts', 'coverageComponents.json'),
allFiles = [];
// функция пробегает по папке и находит все js файлы
dirWalker = function (dir) {
let pattern = /\.js$/,
files = fs.readdirSync(dir),
newPath = '';
for (let i = 0; i < files.length; i++) {
newPath = path.join(dir, files[i]);
if (fs.statSync(newPath).isDirectory()) {
dirWalker(newPath);
} else {
if (pattern.test(files[i])) {
allFiles.push(newPath);
}
}
}
};
// пробегаем по папкам Controls, Components
dirWalker(controlsPath);
dirWalker(componentsPath);
let rawCover = fs.readFileSync(coveragePath, 'utf8'),
cover = JSON.parse(rawCover),
newCover = {},
instrumenter = new nyc().instrumenter(),
transformer = instrumenter.instrumentSync.bind(instrumenter),
controlsFiles = allFiles.filter(file => file.includes('/Controls/')),
componentsFiles = allFiles.filter(file => file.includes('/SBIS3.CONTROLS/'));
// функция дописывает 0 покрытие для файлов которые не использовались в тестах
// и меняет относительные пути на абсолютные
function coverFiles(files, replacer) {
files.forEach(file => {
let relPath = file.replace(replacer, '').slice(1),
rootPaths = replacer.split(path.sep),
rootDir = rootPaths[rootPaths.length - 1],
key = [rootDir, relPath].join(path.sep),
coverData = cover[key];
if (!coverData) {
try {
let rawFile = fs.readFileSync(file, 'utf-8');
transformer(rawFile, file);
let coverState = instrumenter.lastFileCoverage();
Object.keys(coverState.s).forEach(key => coverState.s[key] = 0);
newCover[file] = coverState;
console.log('File ' + file.replace(__dirname, '').slice(1) + ' not using in tests')
} catch (err) {
console.log('File ' + file.replace(__dirname, '').slice(1) + ' can\'t be instrumented, pls try later');
}
} else {
coverData['path'] = file;
newCover[file] = coverData;
}
});
}
// дописываем 0 покрытия для файлов которые не использовались в тестах
coverFiles(controlsFiles, controlsPath);
coverFiles(componentsFiles, componentsPath);
// функция возвращает покрытие для опредленного пути
function getCoverByPath(path) {
let coverageByPath = {};
Object.keys(newCover).forEach(function (name) {
if (name.includes(path)) {
coverageByPath[name] = newCover[name]
}
});
return coverageByPath
}
let controlsCoverage = getCoverByPath(controlsPath),
componentsCoverage = getCoverByPath(componentsPath);
// сохраняем покрытие Общее, Controls, components
fs.writeFileSync(coverageAllPath, JSON.stringify(newCover), 'utf8');
fs.writeFileSync(coverageControlsPath, JSON.stringify(controlsCoverage), 'utf8');
fs.writeFileSync(coverageComponentsPath, JSON.stringify(componentsCoverage), 'utf8');