2016-06-07 21:02:11 +02:00
|
|
|
import os
|
|
|
|
|
import pty
|
|
|
|
|
|
2021-11-01 00:22:09 -04:00
|
|
|
import asciinema.pty_
|
2016-06-07 21:02:11 +02:00
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
from .test_helper import Test
|
|
|
|
|
|
2016-06-07 21:02:11 +02:00
|
|
|
|
2016-06-30 16:36:48 +02:00
|
|
|
class FakeStdout:
|
2016-06-07 21:02:11 +02:00
|
|
|
def __init__(self):
|
|
|
|
|
self.data = []
|
|
|
|
|
|
2019-03-23 12:26:20 +01:00
|
|
|
def write_stdout(self, ts, data):
|
2016-06-07 21:02:11 +02:00
|
|
|
self.data.append(data)
|
|
|
|
|
|
2019-03-23 12:26:20 +01:00
|
|
|
def write_stdin(self, ts, data):
|
2017-09-22 22:21:35 +02:00
|
|
|
pass
|
2016-07-05 17:57:21 +02:00
|
|
|
|
2016-06-07 21:02:11 +02:00
|
|
|
|
2018-11-25 18:30:44 +01:00
|
|
|
class TestRecord(Test):
|
2016-06-07 21:02:11 +02:00
|
|
|
def setUp(self):
|
|
|
|
|
self.real_os_write = os.write
|
|
|
|
|
os.write = self.os_write
|
|
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
|
os.write = self.real_os_write
|
|
|
|
|
|
|
|
|
|
def os_write(self, fd, data):
|
|
|
|
|
if fd != pty.STDOUT_FILENO:
|
|
|
|
|
self.real_os_write(fd, data)
|
|
|
|
|
|
|
|
|
|
def test_record_command_writes_to_stdout(self):
|
|
|
|
|
output = FakeStdout()
|
|
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
command = [
|
|
|
|
|
"python3",
|
|
|
|
|
"-c",
|
|
|
|
|
(
|
|
|
|
|
"import sys"
|
|
|
|
|
"; import time"
|
|
|
|
|
"; sys.stdout.write('foo')"
|
|
|
|
|
"; sys.stdout.flush()"
|
|
|
|
|
"; time.sleep(0.01)"
|
|
|
|
|
"; sys.stdout.write('bar')"
|
|
|
|
|
),
|
|
|
|
|
]
|
2022-02-16 23:01:15 +01:00
|
|
|
asciinema.pty_.record(command, output, lambda: (80, 24))
|
2016-06-07 21:02:11 +02:00
|
|
|
|
2021-10-30 13:21:45 -04:00
|
|
|
assert output.data == [b"foo", b"bar"]
|