Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/python-wasm/src/test/no-stdio.test.ts
1067 views
1
/*
2
3
Test disabling using stdio in node.js.
4
5
This is so a nodejs application can run full python
6
terminals in memory without touch process.stdin,
7
process.stdout, and process.stderr. This is critical
8
for building an electron app with CoWasm running
9
under node.js. It's also useful for automated
10
testing.
11
*/
12
13
import { asyncPython } from "../node";
14
import { delay } from "awaiting";
15
16
test("use noStdio", async () => {
17
jest.setTimeout(15000);
18
const python = await asyncPython({ noStdio: true });
19
// capture stdout and stderr to a string. Actual stdout/stderr is a Buffer.
20
let stdout = "";
21
python.kernel.on("stdout", (data) => {
22
stdout += data.toString();
23
});
24
let stderr = "";
25
python.kernel.on("stderr", (data) => {
26
stderr += data.toString();
27
});
28
// start the full interactive REPL running. We don't await this at
29
// the top level or we would be stuck until the REPL gets exited.
30
(async () => {
31
try {
32
await python.terminal();
33
} catch (_) {}
34
})();
35
const t = new Date().valueOf();
36
while (new Date().valueOf() - t < 5000 && !stdout.includes(">>>")) {
37
await delay(50);
38
}
39
40
// send 389 + 5077
41
await python.kernel.writeToStdin("389 + 5077\n");
42
43
// output is buffered, so it can take a little while before it appears.
44
const t0 = new Date().valueOf();
45
while (
46
new Date().valueOf() - t0 < 5000 &&
47
!stdout.includes(`${389 + 5077}`)
48
) {
49
await delay(50);
50
}
51
expect(stdout).toContain(`${389 + 5077}`);
52
53
// send something to stderr
54
await python.kernel.writeToStdin(
55
"import sys; sys.stderr.write('cowasm'); sys.stderr.flush()\n"
56
);
57
const t1 = new Date().valueOf();
58
while (new Date().valueOf() - t1 < 5000 && !stderr.includes("cowasm")) {
59
await delay(50);
60
}
61
expect(stderr).toContain("cowasm");
62
63
await python.kernel.terminate();
64
});
65
66