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
87 lines (66 loc) · 1.85 KB
/
Copy pathtestCtrl.js
File metadata and controls
87 lines (66 loc) · 1.85 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
/**
* 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;
module.exports = {
testKata: (req, res, next) => {
console.log('hit testCtrl');
let body = req.body;
console.log(body);
db.read.kata_by_id([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) {
deffered.resolve(stdOut);
ele.result = stdOut;
} else if (stdErr) {
deffered.resolve(stdErr);
ele.result = stdErr;
}
});
promiseArr.push(deffered.promise)
});
Q.all(promiseArr).then((response) => {
res.json(kata.test_script);
})
});
},
testExamplesKata: (req, res, next) => {
let body = req.body;
let promiseArr = [];
console.log(body.script);
body.examples.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) {
deffered.resolve(stdOut);
ele.result = stdOut;
} else if (stdErr) {
deffered.resolve(stdErr);
ele.result = stdErr;
}
});
promiseArr.push(deffered.promise)
});
Q.all(promiseArr).then(response => {
console.log(body.examples);
res.json(body.examples);
})
},
};