Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/project/jupyter/test/stdin.ts
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45/*6Test backend part of interactive input.7*/89import {} from "mocha";10import * as expect from "expect";11import * as common from "./common";1213function stdin_func(expected_prompt, expected_password, value) {14async function stdin(prompt: string, password: boolean): Promise<string> {15if (prompt != expected_prompt) {16return `'bad prompt=${prompt}'`;17}18if (password != expected_password) {19return `'password=${password} should not be set'`;20}21return JSON.stringify(value);22}23return stdin;24}2526describe("get input using the python2 kernel -- ", function () {27this.timeout(10000);28let kernel: common.JupyterKernel;2930it("creates a python2 kernel", function () {31kernel = common.kernel("test-python2");32});3334it("reading input - no prompt", async function () {35const out = await kernel.execute_code_now({36code: "print(input())",37stdin: stdin_func("", false, "cocalc"),38});39expect(common.output(out)).toEqual("cocalc\n");40});4142it("reading input - different return", async function () {43const out = await kernel.execute_code_now({44code: "print(input())",45stdin: stdin_func("", false, "sage"),46});47expect(common.output(out)).toEqual("sage\n");48});4950it("reading raw_input - no prompt", async function () {51const out = await kernel.execute_code_now({52code: "print(raw_input())",53stdin: stdin_func("", false, "cocalc"),54});55expect(common.output(out)).toEqual('"cocalc"\n');56});5758it("reading input - prompt", async function () {59const out = await kernel.execute_code_now({60code: 'print(input("prompt"))',61stdin: stdin_func("prompt", false, "cocalc"),62});63expect(common.output(out)).toEqual("cocalc\n");64});6566it("reading raw_input - prompt", async function () {67const out = await kernel.execute_code_now({68code: 'print(raw_input("prompt"))',69stdin: stdin_func("prompt", false, "cocalc"),70});71expect(common.output(out)).toEqual('"cocalc"\n');72});7374it("reading a password", async function () {75const out = await kernel.execute_code_now({76code: 'import getpass; print(getpass.getpass("password?"))',77stdin: stdin_func("password?", true, "cocalc"),78});79expect(common.output(out)).toEqual('"cocalc"\n');80});8182return it("closes the kernel", function () {83kernel.close();84});85});8687/*88describe("get input using the python3 kernel -- ", function() {89this.timeout(20000);9091it("do it", async function() {92const kernel = common.kernel("test-python3");93const out = await kernel.execute_code_now({94code: 'print(input("prompt"))',95stdin: stdin_func("prompt", false, "cocalc")96});97expect(common.output(out)).toEqual("cocalc\n");98});99});100101describe("get input using the ir kernel -- ", function() {102this.timeout(20000);103104it("do it", async function() {105const kernel = common.kernel("test-ir");106const out = await kernel.execute_code_now({107code: 'print(readline("prompt"))',108stdin: stdin_func("prompt", false, "cocalc")109});110expect(common.output(out)).toEqual('[1] "cocalc"\n');111kernel.close();112});113});114115*/116117118