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
140 lines (101 loc) · 2.34 KB
/
Copy pathtestCtrl.js
File metadata and controls
140 lines (101 loc) · 2.34 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
/**
* 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
*
*/
let test = {
type: `test`,
value: `Test Passed: Value == 'Some Value'`,
passed: true
};
let it = {
type: `it`,
value: `Fixed tests or random tests`,
time: 2, //Milliseconds
array: [test]
};
let describe = {
type: `describe`,
value: `Tests of something`,
time: 2, //Milliseconds
array: [it]
};
let main = {
array: [describe],
testCount: 1,
passedCount: 1
};
let exampleRes = {
array: [
describe,
it,
test,
{
type: `test`,
value: `Test Passed: Value == 'Some Value'`,
passed: false
},
],
testCount: 4,
passedcount: 3
};
const app = require('../server');
const db = app.get('db');
const Q = require('q');
const exec = require('child_process').exec;
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);
defer.resolve(stdErr);
} else {
console.log(stdOut)
}
});
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 kata = kataArray[0];
let promiseArr = [];
kata.test_script.forEach((ele, i) => {
let deffered = Q.defer();
exec(`docker run --rm codewars/node-runner run -l javascript -c "${body.script}" -t cw -f "${ele.test}"`,
(err, stdOut, stdErr) => {
if (err) {
console.log(err);
} else if (stdOut) {
if (stdOut.search(/passed/gi) > 0) {
ele.passed = true
} else {
ele.passed = false;
}
ele.result = stdOut.replace(/</g, '\n<');
deffered.resolve(stdOut);
} else if (stdErr) {
console.log(stdErr);
ele.result = stdErr;
deffered.resolve(stdErr);
}
});
promiseArr.push(deffered.promise)
});
Q.all(promiseArr).then((response) => {
res.json(kata.test_script);
})
});
},
testExamplesKata: (req, res, next) => {
let body = req.body;
console.log(body.script);
console.log(exampleRes);
res.json(exampleRes)
}
};