Skip to content

Commit

Permalink
Testing console redirections.
Browse files Browse the repository at this point in the history
  • Loading branch information
casatir committed Jan 11, 2021
1 parent 35f3cc4 commit 5c4939e
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/tests/test_console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from pathlib import Path
import sys

sys.path.append(str(Path(__file__).parents[2] / "src" / "pyodide-py"))

from pyodide import console # noqa: E402


def test_stream_redirection():
my_buffer = ""

def stdout_callback(string):
nonlocal my_buffer
my_buffer += string

my_stream = console._StdStream(stdout_callback)

print("foo", file=my_stream)
assert my_buffer == "foo\n"
print("bar", file=my_stream)
assert my_buffer == "foo\nbar\n"


def test_interactive_console_streams():

my_stdout = ""
my_stderr = ""

def stdout_callback(string):
nonlocal my_stdout
my_stdout += string

def stderr_callback(string):
nonlocal my_stderr
my_stderr += string

shell = console.InteractiveConsole(
stdout_callback=stdout_callback, stderr_callback=stderr_callback
)

# std names
assert sys.stdout.name == "<stdout>"
assert sys.stderr.name == "<stderr>"

# std redirections
print("foo")
assert my_stdout == "foo\n"
print("bar", file=sys.stderr)
assert my_stderr == "bar\n"

# redirections disabled at destruction
shell.restore_stdstreams()

my_stdout = ""
my_stderr = ""

print("bar")
assert my_stdout == ""

print("foo", file=sys.stdout)
assert my_stderr == ""

0 comments on commit 5c4939e

Please sign in to comment.