-
Notifications
You must be signed in to change notification settings - Fork 8
/
path-finder.js
81 lines (67 loc) · 2.04 KB
/
path-finder.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
var fs = require('fs');
var glob = require('glob');
var customLogger = require('./custom-logger');
const logger = customLogger.init();
function parseTestFiles(pattern, encoding) {
var paths = {};
glob.sync(pattern).find((path, index, array) => {
parseTestFile(paths, path, testFileData(path, encoding));
});
return paths;
}
function testFileData(path, encoding) {
return removeNewLines(removeComments(fs.readFileSync(path, encoding)));
}
function parseTestFile(paths, path, data) {
var result, regex = regexPattern();
while ((result = regex.exec(data)) != null) {
var type = result[2] || result[3];
var text = result[5];
if (paths[path] == undefined) {
paths[path] = { describe: [], it: [] };
}
if (type === 'describe') {
paths[path].describe.push(text);
}
if (type === 'it') {
paths[path].it.push(text);
}
}
}
function testFile(paths, describe, it) {
var testFile = Object.keys(paths).find(path =>
exist(paths, path, describe, it));
if (testFile === undefined) {
logger.error('Test file path not found! %s | %s | %s',
JSON.stringify(paths), describe, it);
}
return testFile;
}
function exist(paths, path, describe, it) {
return existDescribe(paths, path, describe) &&
existIt(paths, path, it);
}
function existDescribe(paths, path, describe) {
return paths[path].describe.find(element =>
describe.startsWith(removeEscapedQuotes(element)));
}
function existIt(paths, path, it) {
return paths[path].it.find(element =>
it.startsWith(removeEscapedQuotes(element)));
}
function regexPattern() {
return /((describe)|(it))\s*\(\s*((?<![\\])[\`\'\"])((?:.(?!(?<![\\])\4))*.?)\4/gi;
}
function removeEscapedQuotes(str) {
return str.replace(/(?:\\|\\\\)((")|(')|(`))/g,"$1");
}
function removeComments(data) {
return data.replace(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/gm, '');
}
function removeNewLines(data) {
return data.replace(/\r?\n|\r/g, '');
}
module.exports = {
parseTestFiles: parseTestFiles,
testFile: testFile
};