forked from CodewarsClone/Codewars
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestCtrl.js
More file actions
162 lines (129 loc) · 3.14 KB
/
Copy pathtestCtrl.js
File metadata and controls
162 lines (129 loc) · 3.14 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Created by Joshua Baert on 12/15/2016.
*
* This Controller is where we take take a script as a string then run our tests against it
*
*/
const app = require('../server');
const db = app.get('db');
const Q = require('q');
const exec = require('child_process').exec;
const util = require('util');
function timeParser(str) {
str = str.replace(/<COMPLETEDIN::>/, '');
if (!parseInt(str)) {
return 1
} else {
return parseInt(str)
}
}
function objectifer(arr) {
return arr.map((ele, i) => {
if (ele.search(/<DESCRIBE::>/) > -1) {
return {
type: `describe`,
value: ele.replace(/<DESCRIBE::>/, ''),
time: null,
nest: []
}
} else if (ele.search(/<IT::>/) > -1) {
return {
type: `it`,
value: ele.replace(/<IT::>/, ''),
time: null,
nest: []
}
} else if (ele.search(/<PASSED::>/) > -1) {
return {
type: 'test',
passed: true,
value: ele.replace(/<PASSED::>/, ''),
}
} else if (ele.search(/<FAILED::>/) > -1) {
return {
type: 'test',
passed: false,
value: ele.replace(/<FAILED::>/, ''),
}
} else if (ele.search(/<ERROR::>/) > -1) {
return {
type: 'ERROR',
value: ele.replace(/<ERROR::>/, '')
}
} else if (ele.search(/<COMPLETEDIN::>/) > -1) {
return {type: 'completed', time: timeParser(ele)}
}
});
}
function nester(array) {
let testCount = 0;
let passCount = 0;
for (let i = array.length - 1; i >= 0; i--) {
if (array[i]) {
if (array[i].type == 'test') {
testCount += 1;
if (array[i].passed == true) passCount += 1
}
if (array[i].type === 'describe' || array[i].type === 'it') {
let j = i + 1;
let recurs = true;
while (recurs) {
if (array[j].type !== 'completed') {
array[i].nest.push(array.splice(j, 1)[0])
} else {
array[i].time = array[j].time;
array.splice(j, 1);
recurs = false
}
}
}
}
}
return {
nest: array,
testCount: testCount,
passCount: passCount
}
}
function testRunner(script, test) {
let defer = Q.defer();
exec(`docker run --rm codewars/node-runner run -l javascript -c "${script}" -t cw -f "${test}"`,
(err, stdOut, stdErr) => {
if (err) console.log(err);
if (stdErr) {
console.log('stdErr');
console.log(stdErr);
}
let output = stdOut.split(/\n/g);
for (let i = output.length - 1; i >= 0; i--) if (output[i] === '') output.splice(i, 1);
let newArr = objectifer(output);
// console.log(output);
newArr = nester(newArr);
defer.resolve(newArr);
return
}
);
return defer.promise
}
module.exports = {
testKata: (req, res, next) => {
let body = req.body;
db.read.kata_for_test([req.params.kataid], (err, kataArray) => {
if (err) console.log(err);
let test = kataArray[0].test_script[0].test;
testRunner(body.script, test).then((response) => {
res.json(response);
// console.log(util.inspect(response, false, null));
return
});
});
},
testExamplesKata: (req, res, next) => {
let body = req.body;
testRunner(body.script, body.examples).then((response) => {
res.json(response);
// console.log(util.inspect(response, false, null));
return
});
}
};