-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecuteQuestionFile.js
More file actions
69 lines (54 loc) Β· 2.04 KB
/
executeQuestionFile.js
File metadata and controls
69 lines (54 loc) Β· 2.04 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
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const executeQuestionFile = async (filePath, questionId) => {
return new Promise(async (resolve, reject) => {
console.log("Question Id in Exec", questionId);
await axios.get(`http://localhost:9200/api/coding/get/question?id=${questionId}`)
.then(async (response) => {
console.log("Question -- ", response.data.questionData[0]);
let { testCases, functionName } = response.data.questionData[0];
let codeToAppend = `
import json
testCasesResults = []
def runTestCase(testCase):
output = ${functionName}(*testCase["parameters"])
if json.dumps(output) == json.dumps(testCase["output"]):
respDict = dict()
respDict["success"] = True
respDict["programOutput"] = output
testCasesResults.append(respDict)
else:
respDict = dict()
respDict["success"] = False
respDict["programOutput"] = output
respDict["expectedOutput"] = testCase["output"]
testCasesResults.append(respDict)
if __name__ == "__main__":
testCases = ${JSON.stringify(testCases)}
for i in testCases:
runTestCase(i)
print(testCasesResults)
`;
try{
await fs.appendFileSync(filePath, codeToAppend);
} catch(err){
console.log("Error while appending", err);
return reject({success: false, error: "Error appending prebuilt content!"})
}
exec(`python ${filePath}`,
(error, stdout, stderr) => {
error && reject({error, stderr})
stderr && reject({stderr})
resolve(stdout)
})
}).catch((err) => {
console.log("Error Exe Question", err);
return reject({err});
})
})
}
module.exports = {
executeQuestionFile
}