-
-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathtest_args.py
More file actions
157 lines (140 loc) · 4.66 KB
/
test_args.py
File metadata and controls
157 lines (140 loc) · 4.66 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import errno
import os
import pty
import re
import select
import subprocess
import sys
import tempfile
import unittest
from textwrap import dedent
from bpython import args
from bpython.config import getpreferredencoding
from bpython.test import FixLanguageTestCase as TestCase
def run_with_tty(command):
# based on https://stackoverflow.com/questions/52954248/capture-output-as-a-tty-in-python
master_stdout, slave_stdout = pty.openpty()
master_stderr, slave_stderr = pty.openpty()
master_stdin, slave_stdin = pty.openpty()
p = subprocess.Popen(
command,
stdout=slave_stdout,
stderr=slave_stderr,
stdin=slave_stdin,
close_fds=True,
)
for fd in (slave_stdout, slave_stderr, slave_stdin):
os.close(fd)
readable = [master_stdout, master_stderr]
result = {master_stdout: b"", master_stderr: b""}
try:
while readable:
ready, _, _ = select.select(readable, [], [], 1)
for fd in ready:
try:
data = os.read(fd, 512)
except OSError as e:
if e.errno != errno.EIO:
raise
# EIO means EOF on some systems
readable.remove(fd)
else:
if not data: # EOF
readable.remove(fd)
result[fd] += data
finally:
for fd in (master_stdout, master_stderr, master_stdin):
os.close(fd)
if p.poll() is None:
p.kill()
p.wait()
if p.returncode:
raise RuntimeError(f"Subprocess exited with {p.returncode}")
return (
result[master_stdout].decode(getpreferredencoding()),
result[master_stderr].decode(getpreferredencoding()),
)
class TestExecArgs(unittest.TestCase):
def test_exec_dunder_file(self):
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(
dedent(
"""\
import sys
sys.stderr.write(__file__)
sys.stderr.flush()"""
)
)
f.flush()
_, stderr = run_with_tty(
[sys.executable] + ["-m", "bpython.curtsies", f.name]
)
self.assertEqual(stderr.strip(), f.name)
def test_exec_dunder_name(self):
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(
dedent(
"""\
import sys
sys.stderr.write(__name__)
sys.stderr.flush()"""
)
)
f.flush()
_, stderr = run_with_tty(
[sys.executable] + ["-m", "bpython.curtsies", f.name]
)
self.assertEqual(stderr.strip(), '__main__')
def test_exec_dunder_name_in_imported_module(self):
with tempfile.NamedTemporaryFile(mode="w", suffix=".py") as module_file, tempfile.NamedTemporaryFile(mode="w") as main_file:
module_file.write(
dedent(
"""\
import sys
sys.stderr.write(__name__)
sys.stderr.flush()"""
)
)
module_file.flush()
module_name = os.path.basename(module_file.name)[:-3]
main_file.write(f"import {module_name}")
main_file.flush()
_, stderr = run_with_tty(
[sys.executable] + ["-m", "bpython.curtsies", main_file.name]
)
self.assertEqual(stderr.strip(), module_name)
def test_exec_nonascii_file(self):
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(
dedent(
"""\
# coding: utf-8
"你好 # nonascii"
"""
)
)
f.flush()
_, stderr = run_with_tty(
[sys.executable, "-m", "bpython.curtsies", f.name],
)
self.assertEqual(len(stderr), 0)
def test_exec_nonascii_file_linenums(self):
with tempfile.NamedTemporaryFile(mode="w") as f:
f.write(
dedent(
"""\
1/0
"""
)
)
f.flush()
_, stderr = run_with_tty(
[sys.executable, "-m", "bpython.curtsies", f.name],
)
self.assertIn("line 1", clean_colors(stderr))
def clean_colors(s):
return re.sub(r"\x1b[^m]*m", "", s)
class TestParse(TestCase):
def test_version(self):
with self.assertRaises(SystemExit):
args.parse(["--version"])