2
2
/* eslint-disable no-undef */
3
3
const fs = require ( "fs" ) ;
4
4
5
+ const PROBLEMS_FOLDERS = [
6
+ "./LeetcodeProblems/Algorithms/easy/" ,
7
+ "./LeetcodeProblems/Algorithms/medium/" ,
8
+ "./LeetcodeProblems/Algorithms/hard/"
9
+ ] ;
10
+
5
11
const TESTS_FOLDERS = [
6
12
"./LeetcodeProblemsTests/Algorithms/easy/" ,
7
13
"./LeetcodeProblemsTests/Algorithms/medium/" ,
8
14
"./LeetcodeProblemsTests/Algorithms/hard/"
9
- ]
15
+ ] ;
10
16
11
17
const REGEX_PATTERN_HIDDEN_FILES = / ( ^ | \/ ) \. [ ^ \/ \. ] / g;
12
18
13
- var test_all = async function ( ) {
19
+ const getAllTests = async function ( paths ) {
20
+ let problems = [ ] ;
21
+ for ( const i in paths ) {
22
+ const folder = paths [ i ] ;
23
+ const newProblems = await loadProblemsFiles ( folder ) ; // await
24
+ problems = problems . concat ( newProblems ) ;
25
+ }
26
+ return problems ;
27
+ } ;
28
+
29
+ const runAllTests = async function ( problems ) {
14
30
try {
15
- var problems = [ ] ;
16
- for ( const i in TESTS_FOLDERS ) {
17
- var folder = TESTS_FOLDERS [ i ] ;
18
- var new_problems = await loadProblemsFiles ( folder ) ; // await
19
- problems = problems . concat ( new_problems ) ;
20
- } ;
21
31
console . log ( problems ) ;
22
-
23
- var solvePromises = problems . map ( solve ) ;
32
+ var solvePromises = problems . map ( solveProblem ) ;
24
33
25
- await Promise . all ( solvePromises )
34
+ await Promise . all ( solvePromises ) ;
26
35
} catch ( error ) {
27
36
console . log ( error ) ;
28
37
throw new Error ( error ) ;
29
38
}
30
39
} ;
31
40
32
- var solve = ( problem ) => {
41
+ const solveProblem = ( problem ) => {
33
42
try {
34
43
console . log ( "Solving: " + problem ) ;
35
44
@@ -47,28 +56,61 @@ var solve = (problem) => {
47
56
console . log ( error ) ;
48
57
throw new Error ( error ) ;
49
58
}
50
- }
59
+ } ;
51
60
52
- var loadProblemsFiles = ( folder ) => {
61
+ const loadProblemsFiles = ( folder ) => {
53
62
return new Promise ( function ( resolve , reject ) {
54
63
fs . readdir ( folder , ( error , files ) => {
55
64
if ( error ) {
56
65
reject ( error ) ;
57
66
} else {
58
67
console . log ( folder ) ;
59
- new_problems = files . filter ( ( item ) => ! REGEX_PATTERN_HIDDEN_FILES . test ( item ) ) ;
60
- new_problems = new_problems . map ( ( item ) => folder + item ) ;
68
+ newProblems = files . filter ( ( item ) => ! REGEX_PATTERN_HIDDEN_FILES . test ( item ) ) ;
69
+ newProblems = newProblems . map ( ( item ) => folder + item ) ;
61
70
62
- resolve ( new_problems ) ;
71
+ resolve ( newProblems ) ;
63
72
}
64
73
} ) ;
65
74
} ) ;
66
75
} ;
67
76
68
- if ( process . argv . length > 2 ) {
69
- const path = process . argv . pop ( ) ;
70
- solve ( path ) ;
71
- } else {
72
- test_all ( ) ;
77
+ const getMissingTests = async function ( tests , problems ) {
78
+ const hasTestStatus = problems . reduce ( ( status , problemPath ) => {
79
+ const baseIndex = PROBLEMS_FOLDERS . findIndex ( ( basePath ) =>
80
+ problemPath . startsWith ( basePath )
81
+ ) ;
82
+
83
+ let testPath = problemPath
84
+ . replace ( PROBLEMS_FOLDERS [ baseIndex ] , TESTS_FOLDERS [ baseIndex ] )
85
+ . replace ( / \. j s $ / , "_Test.js" ) ;
86
+
87
+ status . push ( {
88
+ problem : problemPath ,
89
+ hasTest : tests . includes ( testPath )
90
+ } ) ;
91
+
92
+ return status ;
93
+ } , [ ] ) ;
94
+ const missingTests = hasTestStatus . filter ( ( stat ) => ! stat . hasTest ) ;
95
+ console . log ( "Total Problems:" , problems . length ) ;
96
+ console . log ( "Missing Tests:" , missingTests . length ) ;
97
+
98
+ if ( missingTests . length ) {
99
+ console . table ( missingTests ) ;
100
+ }
101
+ } ;
102
+
103
+ async function runScript ( ) {
104
+ if ( process . argv . length > 2 ) {
105
+ const path = process . argv . pop ( ) ;
106
+ solveProblem ( path ) ;
107
+ } else {
108
+ const problems = await getAllTests ( PROBLEMS_FOLDERS ) ;
109
+ const tests = await getAllTests ( TESTS_FOLDERS ) ;
110
+
111
+ await runAllTests ( tests ) ;
112
+ await getMissingTests ( tests , problems ) ;
113
+ }
73
114
}
74
-
115
+
116
+ runScript ( ) ;
0 commit comments