Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork252
Expand file tree
/
Copy pathtest_args.py
More file actions
122 lines (107 loc) · 3.41 KB
/
test_args.py
File metadata and controls
122 lines (107 loc) · 3.41 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
importerrno
importos
importpty
importre
importselect
importsubprocess
importsys
importtempfile
importunittest
fromtextwrapimportdedent
frombpythonimportargs
frombpython.configimportgetpreferredencoding
frombpython.testimportFixLanguageTestCaseasTestCase
defrun_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,
)
forfdin (slave_stdout,slave_stderr,slave_stdin):
os.close(fd)
readable= [master_stdout,master_stderr]
result= {master_stdout:b"",master_stderr:b""}
try:
whilereadable:
ready,_,_=select.select(readable, [], [],1)
forfdinready:
try:
data=os.read(fd,512)
exceptOSErrorase:
ife.errno!=errno.EIO:
raise
# EIO means EOF on some systems
readable.remove(fd)
else:
ifnotdata:# EOF
readable.remove(fd)
result[fd]+=data
finally:
forfdin (master_stdout,master_stderr,master_stdin):
os.close(fd)
ifp.poll()isNone:
p.kill()
p.wait()
ifp.returncode:
raiseRuntimeError(f"Subprocess exited with{p.returncode}")
return (
result[master_stdout].decode(getpreferredencoding()),
result[master_stderr].decode(getpreferredencoding()),
)
classTestExecArgs(unittest.TestCase):
deftest_exec_dunder_file(self):
withtempfile.NamedTemporaryFile(mode="w")asf:
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)
deftest_exec_nonascii_file(self):
withtempfile.NamedTemporaryFile(mode="w")asf:
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)
deftest_exec_nonascii_file_linenums(self):
withtempfile.NamedTemporaryFile(mode="w")asf:
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))
defclean_colors(s):
returnre.sub(r"\x1b[^m]*m","",s)
classTestParse(TestCase):
deftest_version(self):
withself.assertRaises(SystemExit):
args.parse(["--version"])