Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/jupyter/kernel/test/input.test.ts
1450 views
1
/*
2
Test interactive input, which uses the stdin channel
3
4
https://jupyter-client.readthedocs.io/en/stable/messaging.html
5
6
7
pnpm test `pwd`/input.test.ts
8
*/
9
10
import { getPythonKernel, closeKernels } from "./util";
11
12
describe("test that interactive input throws an error if we do not provide a stdin function", () => {
13
let k;
14
it("get a python kernel", async () => {
15
k = await getPythonKernel("python-nostdin.ipynb");
16
});
17
18
it("test without stdin", async () => {
19
const code = "a = input('a:')";
20
const v: any[] = [];
21
// we do not pass a stdin function as an option:
22
const output = k.execute_code({ code });
23
for await (const out of output.iter()) {
24
v.push(out);
25
}
26
expect(JSON.stringify(v)).toContain("StdinNotImplementedError");
27
});
28
29
it("cleans up", () => {
30
k.close();
31
});
32
});
33
34
describe("test interactive input", () => {
35
let k;
36
it("get a python kernel", async () => {
37
k = await getPythonKernel("python-stdin.ipynb");
38
});
39
40
it("start some code running with stdin set and see it is called", async () => {
41
const code = "a = input('a:')";
42
const v: any[] = [];
43
const stdin = async (prompt: string, password: boolean) => {
44
v.push({ prompt, password });
45
return "bar";
46
};
47
const output = k.execute_code({ code, stdin });
48
for await (const _ of output.iter()) {
49
}
50
expect(v).toEqual([{ prompt: "a:", password: false }]);
51
});
52
53
it("cleans up", () => {
54
k.close();
55
});
56
});
57
58
afterAll(() => {
59
closeKernels();
60
});
61
62