-
Notifications
You must be signed in to change notification settings - Fork 90
/
Test.js
116 lines (98 loc) · 2.96 KB
/
Test.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* eslint-disable no-useless-escape */
/* eslint-disable no-undef */
const fs = require("fs");
const PROBLEMS_FOLDERS = [
"./LeetcodeProblems/Algorithms/easy/",
"./LeetcodeProblems/Algorithms/medium/",
"./LeetcodeProblems/Algorithms/hard/"
];
const TESTS_FOLDERS = [
"./LeetcodeProblemsTests/Algorithms/easy/",
"./LeetcodeProblemsTests/Algorithms/medium/",
"./LeetcodeProblemsTests/Algorithms/hard/"
];
const REGEX_PATTERN_HIDDEN_FILES = /(^|\/)\.[^\/\.]/g;
const getAllTests = async function (paths) {
let problems = [];
for(const i in paths) {
const folder = paths[i];
const newProblems = await loadProblemsFiles(folder); // await
problems = problems.concat(newProblems);
}
return problems;
};
const runAllTests = async function (problems) {
try {
console.log(problems);
var solvePromises = problems.map(solveProblem);
await Promise.all(solvePromises);
} catch (error) {
console.log(error);
throw new Error(error);
}
};
const solveProblem = (problem) => {
try {
console.log("Solving: " + problem);
const tests = require(problem);
console.log("*".repeat(100));
if (Object.keys(tests).length == 0) {
console.warn("🔴 The problem " + problem + " doesn't have a test method implemented.\n");
return;
}
for(testIdx in tests) {
tests[testIdx]();
}
console.log("✅ Tests for " + problem + " run successfully \n");
} catch (error) {
console.log(error);
throw new Error(error);
}
};
const loadProblemsFiles = (folder) => {
return new Promise(function (resolve, reject) {
fs.readdir(folder, (error, files) => {
if (error) {
reject(error);
} else {
console.log(folder);
newProblems = files.filter((item) => !REGEX_PATTERN_HIDDEN_FILES.test(item));
newProblems = newProblems.map((item) => folder + item);
resolve(newProblems);
}
});
});
};
const getMissingTests = async function (tests, problems) {
const hasTestStatus = problems.reduce((status, problemPath) => {
const baseIndex = PROBLEMS_FOLDERS.findIndex((basePath) =>
problemPath.startsWith(basePath)
);
let testPath = problemPath
.replace(PROBLEMS_FOLDERS[baseIndex], TESTS_FOLDERS[baseIndex])
.replace(/\.js$/, "_Test.js");
status.push({
problem: problemPath,
hasTest: tests.includes(testPath)
});
return status;
}, []);
const missingTests = hasTestStatus.filter((stat) => !stat.hasTest);
console.log("Total Problems:", problems.length);
console.log("Missing Tests:", missingTests.length);
if(missingTests.length) {
console.table(missingTests);
}
};
async function runScript() {
if (process.argv.length > 2) {
const path = process.argv.pop();
solveProblem(path);
} else {
const problems = await getAllTests(PROBLEMS_FOLDERS);
const tests = await getAllTests(TESTS_FOLDERS);
await runAllTests(tests);
await getMissingTests(tests, problems);
}
}
runScript();