-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_readline.py
119 lines (83 loc) · 3.2 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
import os
import pty
import sys
import pytest
from pyrepl.readline import _ReadlineWrapper
@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))
with open(str(histfile), "r") as f:
assert f.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))
with open(str(histfile), "r") as f:
assert f.readlines() == ["foo\n", "bar\n"]
@pytest.mark.parametrize('auto_history,expected', [(True, 1), (False, 0)])
def test_set_auto_history(auto_history, expected):
master, slave = pty.openpty()
readline_wrapper = _ReadlineWrapper(slave, slave)
readline_wrapper.set_auto_history(auto_history)
os.write(master, b'input\n')
readline_wrapper.get_reader().readline()
assert readline_wrapper.get_current_history_length() == expected