-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpythonExecutor.test.ts
328 lines (275 loc) · 8.88 KB
/
pythonExecutor.test.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*global suite, test*/ //comment for eslint
// This test uses TDD Mocha. see https://mochajs.org/ for help
// http://ricostacruz.com/cheatsheets/mocha-tdd
// The module 'assert' provides assertion methods from node
import * as assert from 'assert'
import { PythonExecutor, PythonState } from './pythonExecutor'
import { EOL } from 'os';
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
suite("python_evaluator Tests", () => {
let pyEvaluator = new PythonExecutor()
let input = {
evalCode: "",
filePath: "",
usePreviousVariables: false,
show_global_vars: true,
default_filter_vars: [],
default_filter_types: ["<class 'module'>", "<class 'function'>"]
}
const pythonStartupTime = 3000
suiteSetup(function () {
this.timeout(pythonStartupTime + 500)
})
setup(function (done) {
pyEvaluator.onPrint = () => { }
pyEvaluator.onStderr = () => { }
pyEvaluator.onResult = () => { }
pyEvaluator.start(done)
})
teardown(function(){
pyEvaluator.stop(true)
})
test("sanity check: 1+1=2", () => {
assert.strictEqual(1 + 1, 2)
})
test("returns result", function (done) {
pyEvaluator.onResult = (result) => {
assert.notStrictEqual(result, null)
done()
}
pyEvaluator.onStderr = (err: string) => {
done(err)
}
pyEvaluator.onPrint = (msg: string) => {
done(msg)
}
input.evalCode = "x"
pyEvaluator.execCode(input)
})
test("returns user variables", function (done) {
pyEvaluator.onResult = (result) => {
assert.strictEqual(result.userVariables['x'], 1)
done()
}
input.evalCode = "x=1"
pyEvaluator.execCode(input)
})
test("can import importlib", function (done) {
pyEvaluator.onResult = (result) => {
assert.strictEqual(result.userErrorMsg, undefined)
done()
}
input.evalCode = "import importlib.resources as rsrc"
pyEvaluator.execCode(input)
})
test("returns user variables properly when there is a lot of content", function (done) {
pyEvaluator.onResult = (result) => {
assert.strictEqual(result.userVariables['x'], 1)
done()
}
input.evalCode = "x=1;y='a'*80000"
pyEvaluator.execCode(input)
})
suite("stdout/stderr tests", () => {
test("can print stdout", function (done) {
let hasPrinted = false
pyEvaluator.onPrint = (stdout) => {
assert.strictEqual(stdout, "hello world" + EOL)
hasPrinted = true
}
pyEvaluator.onResult = () => {
if (!hasPrinted) assert.fail("program has returned result", "program should still be printing")
else done()
}
input.evalCode = "print('hello world')"
pyEvaluator.execCode(input)
})
test("can print stdout if no newline", function (done) {
let hasPrinted = false
pyEvaluator.onPrint = (stdout) => {
assert.strictEqual(stdout, "hello world")
hasPrinted = true
}
pyEvaluator.onResult = () => {
if (!hasPrinted) assert.fail("program has returned result", "program should still be printing")
else done()
}
input.evalCode = "print('hello world', end='')"
pyEvaluator.execCode(input)
})
test("can print stderr", function (done) {
let hasLogged = false
pyEvaluator.onStderr = (stderr) => {
assert.strictEqual(stderr, "hello world")
hasLogged = true
done()
}
pyEvaluator.onResult = (result) => {
setTimeout(() => {
if (!hasLogged) assert.fail("program has returned result " + JSON.stringify(result), "program should still be logging")
}, 100); //to avoid race conditions wait a bit in case stderr arrives later
}
input.evalCode = "import sys;sys.stderr.write('hello world')"
pyEvaluator.execCode(input)
})
test("can print multiple lines", function (done) {
let firstPrint = false
pyEvaluator.onPrint = (stdout) => {
// not sure why it is doing this.. stdout should be line buffered
// so we should get 1 and 2 seperately
assert.strictEqual(stdout, '1' + EOL + '2' + EOL)
firstPrint = true
}
pyEvaluator.onResult = () => {
if (!firstPrint) assert.fail("program has returned result", "program should still be printing")
else done()
}
input.evalCode = "[print(x) for x in [1,2]]"
pyEvaluator.execCode(input)
})
test("prints in real-time", function (done) {
let printed = false
pyEvaluator.onPrint = (stdout) => { printed = true }
pyEvaluator.onResult = () => { done() }
setTimeout(() => { if (!printed) assert.fail("") }, 25)
input.evalCode = "from time import sleep\nprint('a')\nsleep(.05)\nprint(b)"
pyEvaluator.execCode(input)
})
test("returns result after print", function (done) {
pyEvaluator.onPrint = (stdout) => {
assert.strictEqual(stdout, "hello world" + EOL)
assert.strictEqual(pyEvaluator.state, PythonState.Executing)
}
pyEvaluator.onResult = () => {
assert.strictEqual(pyEvaluator.state, PythonState.DirtyFree)
done()
}
input.evalCode = "print('hello world')"
pyEvaluator.execCode(input)
})
})
test("no encoding errors with utf8 on windows", function (done) {
// other platforms may have the locale encoding
// so we just test windows
// see https://docs.python.org/3/library/sys.html#sys.stdout
if (process.platform != "win32") {
done()
return
}
pyEvaluator.onResult = (result) => {
assert.strictEqual(result.userErrorMsg, undefined)
assert.strictEqual(result.internalError, null)
done()
}
input.evalCode = "#㍦"
pyEvaluator.execCode(input)
})
test("dump returns result", function (done) {
let gotDump = false
pyEvaluator.onResult = (result) => {
if (gotDump) return
assert.notStrictEqual(result, null)
assert.strictEqual(isEmpty(result.userError), true)
assert.strictEqual(result.internalError, null)
assert.strictEqual(result.userVariables['dump output'], 5)
assert.strictEqual(result.caller, '<module>')
assert.strictEqual(result.lineno, 1)
gotDump = true
done()
}
input.evalCode = "from arepl_dump import dump;dump(5)"
pyEvaluator.execCode(input)
})
test("returns syntax error when incorrect syntax", function (done) {
pyEvaluator.onResult = (result) => {
assert.notStrictEqual(result.userError, null)
assert.strictEqual(result.userError.filename, '<string>')
assert.strictEqual(result.userError.lineno, '1')
assert.strictEqual(result.userError.msg, 'invalid syntax')
done()
}
input.evalCode = "x="
pyEvaluator.execCode(input)
})
test("uses previousRun variables asked", function (done) {
function onSecondResult(result) {
assert.strictEqual(result.userVariables['y'], 1)
done()
}
pyEvaluator.onResult = (result) => {
pyEvaluator.onResult = onSecondResult
input.usePreviousVariables = true
pyEvaluator.execCode(input)
input.usePreviousVariables = false
}
input.evalCode = "x=1"
pyEvaluator.execCode(input)
input.evalCode = "y=x"
})
test("can restart", function (done) {
this.timeout(this.timeout() + pythonStartupTime)
assert.strictEqual(pyEvaluator.state, PythonState.FreshFree)
pyEvaluator.restart(() => {
assert.strictEqual(pyEvaluator.state, PythonState.FreshFree)
pyEvaluator.onResult = () => done()
input.evalCode = "x"
pyEvaluator.execCode(input)
})
})
test("strips out unnecessary error info", function (done) {
pyEvaluator.onResult = (result) => {
assert.strictEqual(result.userErrorMsg, "Traceback (most recent call last):\n line 1, in <module>\nNameError: name 'x' is not defined\n")
done()
}
input.evalCode = "x"
pyEvaluator.execCode(input)
})
test("strips out unnecessary error info even with long tracebacks", function (done) {
pyEvaluator.onResult = (result) => {
// asserting the exact string would result in flaky tests
// because internal python code could change & the traceback would be different
// so we just do some generic checks
assert.strictEqual(result.userErrorMsg.includes("TypeError"), true)
assert.strictEqual(result.userErrorMsg.split('File ').length > 1, true)
assert.strictEqual(result.userErrorMsg.includes("python_evaluator.py"), false)
assert.strictEqual(result.userErrorMsg.includes("exec(data['evalCode'], evalLocals)"), false)
done()
}
input.evalCode = "import json;json.dumps(json)"
pyEvaluator.execCode(input)
})
test("strips out unnecessary error info even with multiple tracebacks", function (done) {
pyEvaluator.onResult = (result) => {
assert.strictEqual(result.userErrorMsg, `Traceback (most recent call last):
line 6, in <module>
line 3, in foo
Exception
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
line 8, in <module>
NameError: name 'fah' is not defined
`)
done()
}
input.evalCode = `
def foo():
raise Exception
try:
foo()
except Exception as e:
fah`
pyEvaluator.execCode(input)
})
test("checks syntax", function (done) {
pyEvaluator.checkSyntax("x=").then(() => {
assert.fail("promise should have been rejected")
}).catch(() => { })
pyEvaluator.checkSyntax("x=1").then(() => {
done()
}).catch((err) => {
assert.fail("syntax was correct there should not have been an error")
})
})
})