CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/project/jupyter/test/stdin.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
/*
7
Test backend part of interactive input.
8
*/
9
10
import {} from "mocha";
11
import * as expect from "expect";
12
import * as common from "./common";
13
14
function stdin_func(expected_prompt, expected_password, value) {
15
async function stdin(prompt: string, password: boolean): Promise<string> {
16
if (prompt != expected_prompt) {
17
return `'bad prompt=${prompt}'`;
18
}
19
if (password != expected_password) {
20
return `'password=${password} should not be set'`;
21
}
22
return JSON.stringify(value);
23
}
24
return stdin;
25
}
26
27
describe("get input using the python2 kernel -- ", function () {
28
this.timeout(10000);
29
let kernel: common.JupyterKernel;
30
31
it("creates a python2 kernel", function () {
32
kernel = common.kernel("test-python2");
33
});
34
35
it("reading input - no prompt", async function () {
36
const out = await kernel.execute_code_now({
37
code: "print(input())",
38
stdin: stdin_func("", false, "cocalc"),
39
});
40
expect(common.output(out)).toEqual("cocalc\n");
41
});
42
43
it("reading input - different return", async function () {
44
const out = await kernel.execute_code_now({
45
code: "print(input())",
46
stdin: stdin_func("", false, "sage"),
47
});
48
expect(common.output(out)).toEqual("sage\n");
49
});
50
51
it("reading raw_input - no prompt", async function () {
52
const out = await kernel.execute_code_now({
53
code: "print(raw_input())",
54
stdin: stdin_func("", false, "cocalc"),
55
});
56
expect(common.output(out)).toEqual('"cocalc"\n');
57
});
58
59
it("reading input - prompt", async function () {
60
const out = await kernel.execute_code_now({
61
code: 'print(input("prompt"))',
62
stdin: stdin_func("prompt", false, "cocalc"),
63
});
64
expect(common.output(out)).toEqual("cocalc\n");
65
});
66
67
it("reading raw_input - prompt", async function () {
68
const out = await kernel.execute_code_now({
69
code: 'print(raw_input("prompt"))',
70
stdin: stdin_func("prompt", false, "cocalc"),
71
});
72
expect(common.output(out)).toEqual('"cocalc"\n');
73
});
74
75
it("reading a password", async function () {
76
const out = await kernel.execute_code_now({
77
code: 'import getpass; print(getpass.getpass("password?"))',
78
stdin: stdin_func("password?", true, "cocalc"),
79
});
80
expect(common.output(out)).toEqual('"cocalc"\n');
81
});
82
83
return it("closes the kernel", function () {
84
kernel.close();
85
});
86
});
87
88
/*
89
describe("get input using the python3 kernel -- ", function() {
90
this.timeout(20000);
91
92
it("do it", async function() {
93
const kernel = common.kernel("test-python3");
94
const out = await kernel.execute_code_now({
95
code: 'print(input("prompt"))',
96
stdin: stdin_func("prompt", false, "cocalc")
97
});
98
expect(common.output(out)).toEqual("cocalc\n");
99
});
100
});
101
102
describe("get input using the ir kernel -- ", function() {
103
this.timeout(20000);
104
105
it("do it", async function() {
106
const kernel = common.kernel("test-ir");
107
const out = await kernel.execute_code_now({
108
code: 'print(readline("prompt"))',
109
stdin: stdin_func("prompt", false, "cocalc")
110
});
111
expect(common.output(out)).toEqual('[1] "cocalc"\n');
112
kernel.close();
113
});
114
});
115
116
*/
117
118