Files
asciinema/tests/pty_test.py

55 lines
1.3 KiB
Python
Raw Normal View History

import os
import pty
2022-02-21 18:30:09 -05:00
from typing import Any, List, Union
2021-11-01 00:22:09 -04:00
import asciinema.pty_
from .test_helper import Test
class Writer:
2022-02-21 18:30:09 -05:00
def __init__(self) -> None:
self.data: List[Union[float, str]] = []
2022-02-21 18:30:09 -05:00
def write_stdout(self, _ts: float, data: Any) -> None:
self.data.append(data)
2022-02-21 18:30:09 -05:00
def write_stdin(self, ts: float, data: Any) -> None:
raise NotImplementedError
2016-07-05 17:57:21 +02:00
2018-11-25 18:30:44 +01:00
class TestRecord(Test):
2022-02-21 18:30:09 -05:00
def setUp(self) -> None:
self.real_os_write = os.write
2022-02-21 18:30:09 -05:00
os.write = self.os_write # type: ignore
2022-02-21 18:30:09 -05:00
def tearDown(self) -> None:
os.write = self.real_os_write
2022-02-21 18:30:09 -05:00
def os_write(self, fd: int, data: Any) -> None:
if fd != pty.STDOUT_FILENO:
self.real_os_write(fd, data)
2022-02-21 18:30:09 -05:00
@staticmethod
def test_record_command_writes_to_stdout() -> None:
writer = Writer()
command = [
"python3",
"-c",
(
"import sys"
"; import time"
"; sys.stdout.write('foo')"
"; sys.stdout.flush()"
"; time.sleep(0.01)"
"; sys.stdout.write('bar')"
),
]
asciinema.pty_.record(
command, {}, writer, lambda: (80, 24), lambda s: None, {}
)
assert writer.data == [b"foo", b"bar"]