Path: blob/main/python/python-wasm/src/test/no-stdio.test.ts
1067 views
/*12Test disabling using stdio in node.js.34This is so a nodejs application can run full python5terminals in memory without touch process.stdin,6process.stdout, and process.stderr. This is critical7for building an electron app with CoWasm running8under node.js. It's also useful for automated9testing.10*/1112import { asyncPython } from "../node";13import { delay } from "awaiting";1415test("use noStdio", async () => {16jest.setTimeout(15000);17const python = await asyncPython({ noStdio: true });18// capture stdout and stderr to a string. Actual stdout/stderr is a Buffer.19let stdout = "";20python.kernel.on("stdout", (data) => {21stdout += data.toString();22});23let stderr = "";24python.kernel.on("stderr", (data) => {25stderr += data.toString();26});27// start the full interactive REPL running. We don't await this at28// the top level or we would be stuck until the REPL gets exited.29(async () => {30try {31await python.terminal();32} catch (_) {}33})();34const t = new Date().valueOf();35while (new Date().valueOf() - t < 5000 && !stdout.includes(">>>")) {36await delay(50);37}3839// send 389 + 507740await python.kernel.writeToStdin("389 + 5077\n");4142// output is buffered, so it can take a little while before it appears.43const t0 = new Date().valueOf();44while (45new Date().valueOf() - t0 < 5000 &&46!stdout.includes(`${389 + 5077}`)47) {48await delay(50);49}50expect(stdout).toContain(`${389 + 5077}`);5152// send something to stderr53await python.kernel.writeToStdin(54"import sys; sys.stderr.write('cowasm'); sys.stderr.flush()\n"55);56const t1 = new Date().valueOf();57while (new Date().valueOf() - t1 < 5000 && !stderr.includes("cowasm")) {58await delay(50);59}60expect(stderr).toContain("cowasm");6162await python.kernel.terminate();63});646566