-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_readline.py
143 lines (101 loc) · 3.72 KB
/
test_readline.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import os
import pty
import sys
import pytest
from pyrepl.readline import _ReadlineWrapper
pytest_plugins = ["pytester"]
@pytest.fixture
def readline_wrapper():
master, slave = pty.openpty()
return _ReadlineWrapper(slave, slave)
if sys.version_info < (3, ):
bytes_type = str
unicode_type = unicode # noqa: F821
else:
bytes_type = bytes
unicode_type = str
def test_readline():
master, slave = pty.openpty()
readline_wrapper = _ReadlineWrapper(slave, slave)
os.write(master, b'input\n')
result = readline_wrapper.get_reader().readline()
assert result == b'input'
assert isinstance(result, bytes_type)
def test_readline_returns_unicode():
master, slave = pty.openpty()
readline_wrapper = _ReadlineWrapper(slave, slave)
os.write(master, b'input\n')
result = readline_wrapper.get_reader().readline(returns_unicode=True)
assert result == 'input'
assert isinstance(result, unicode_type)
def test_raw_input():
master, slave = pty.openpty()
readline_wrapper = _ReadlineWrapper(slave, slave)
os.write(master, b'input\n')
result = readline_wrapper.raw_input('prompt:')
if sys.version_info < (3, ):
assert result == b'input'
assert isinstance(result, bytes_type)
else:
assert result == 'input'
assert isinstance(result, unicode_type)
def test_read_history_file(readline_wrapper, tmp_path):
histfile = tmp_path / "history"
histfile.touch()
assert readline_wrapper.reader is None
readline_wrapper.read_history_file(str(histfile))
assert readline_wrapper.reader.history == []
histfile.write_bytes(b"foo\nbar\n")
readline_wrapper.read_history_file(str(histfile))
assert readline_wrapper.reader.history == ["foo", "bar"]
def test_write_history_file(readline_wrapper, tmp_path):
histfile = tmp_path / "history"
reader = readline_wrapper.get_reader()
history = reader.history
assert history == []
history.extend(["foo", "bar"])
readline_wrapper.write_history_file(str(histfile))
assert open(str(histfile), "r").readlines() == ["foo\n", "bar\n"]
def test_write_history_file_with_exception(readline_wrapper, tmp_path):
"""The history file should not get nuked on inner exceptions.
This was the case with unicode decoding previously."""
histfile = tmp_path / "history"
histfile.write_bytes(b"foo\nbar\n")
class BadEntryException(Exception):
pass
class BadEntry(object):
@classmethod
def replace(cls, *args):
raise BadEntryException
history = readline_wrapper.get_reader().history
history.extend([BadEntry])
with pytest.raises(BadEntryException):
readline_wrapper.write_history_file(str(histfile))
assert open(str(histfile), "r").readlines() == ["foo\n", "bar\n"]
@pytest.mark.parametrize("use_pyrepl", (True, False))
def test_uses_current_stdin(use_pyrepl, testdir):
p1 = testdir.makepyfile("""
import io, os, sys
assert os.isatty(0)
dup_stdin = io.FileIO(os.dup(0))
assert os.isatty(dup_stdin.fileno())
old_stdin = sys.stdin
sys.stdin = dup_stdin
if {use_pyrepl!r}:
import pyrepl.readline
print("input1:", input())
dup_stdin.close()
sys.stdin = old_stdin
print("input2:", input())
""".format(use_pyrepl=use_pyrepl))
child = testdir.spawn(sys.executable + " " + str(p1), expect_timeout=1)
child.sendline("line1")
if use_pyrepl:
child.expect_exact("input1: line1\r\n")
else:
child.expect_exact("input1: b'line1'\r\n")
child.sendline("line2")
if use_pyrepl:
child.expect_exact("input2: line2\r\n")
else:
child.expect_exact(b'line2\r\ninput2: line2\r\n')