forked from ezyang/ghstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_shell.py
139 lines (113 loc) · 3.19 KB
/
test_shell.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
#!/usr/bin/env python3
import logging
import sys
import unittest
from dataclasses import dataclass
from typing import Any, List
import expecttest
import ghstack.shell
@dataclass
class ConsoleMsg:
pass
@dataclass
class out(ConsoleMsg):
msg: str
@dataclass
class err(ConsoleMsg):
msg: str
@dataclass
class big_dump(ConsoleMsg):
pass
class TestShell(expecttest.TestCase):
def setUp(self) -> None:
self.sh = ghstack.shell.Shell()
def emit(self, *payload: ConsoleMsg, **kwargs: Any) -> ghstack.shell._SHELL_RET:
args: List[str] = [sys.executable, 'emitter.py']
for p in payload:
if isinstance(p, out):
args.extend(('o', p.msg))
elif isinstance(p, err):
args.extend(('e', p.msg))
elif isinstance(p, big_dump):
args.extend(('r', '-'))
return self.sh.sh(*args, **kwargs)
def flog(self, cm: 'unittest._AssertLogsContext') -> str: # type: ignore[name-defined]
def redact(s: str) -> str:
s = s.replace(sys.executable, 'python')
return s
return '\n'.join(redact(r.getMessage()) for r in cm.records)
def test_stdout(self) -> None:
with self.assertLogs(level=logging.DEBUG) as cm:
self.emit(
out("arf\n")
)
self.assertExpectedInline(self.flog(cm), '''\
$ python emitter.py o 'arf\n'
arf
''')
def test_stderr(self) -> None:
with self.assertLogs(level=logging.DEBUG) as cm:
self.emit(
err("arf\n")
)
self.assertExpectedInline(self.flog(cm), '''\
$ python emitter.py e 'arf\n'
# stderr:
arf
''')
def test_stdout_passthru(self) -> None:
with self.assertLogs(level=logging.DEBUG) as cm:
self.emit(
out("arf\n"),
stdout=None
)
self.assertExpectedInline(self.flog(cm), '''\
$ python emitter.py o 'arf\n'
arf
''')
def test_stdout_with_stderr_prefix(self) -> None:
# What most commands should look like
with self.assertLogs(level=logging.DEBUG) as cm:
self.emit(
err("Step 1...\n"),
err("Step 2...\n"),
err("Step 3...\n"),
out("out\n"),
stdout=None
)
self.assertExpectedInline(self.flog(cm), '''\
$ python emitter.py e 'Step 1...\n' e 'Step 2...\n' e 'Step 3...\n' o 'out\n'
# stderr:
Step 1...
Step 2...
Step 3...
# stdout:
out
''')
def test_interleaved_stdout_stderr_passthru(self) -> None:
# NB: stdout is flushed in each of these cases
with self.assertLogs(level=logging.DEBUG) as cm:
self.emit(
out("A\n"),
err("B\n"),
out("C\n"),
err("D\n"),
stdout=None
)
self.assertExpectedInline(self.flog(cm), '''\
$ python emitter.py o 'A\n' e 'B\n' o 'C\n' e 'D\n'
# stderr:
B
D
# stdout:
A
C
''')
def test_deadlock(self) -> None:
self.emit(
big_dump()
)
def test_uses_raw_fd(self) -> None:
self.emit(out("A\n"), stdout=sys.stdout)
if __name__ == '__main__':
unittest.main()