-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support unconditionally logging stderr/stdout, no matter the spec.
Signed-off-by: Edward Z. Yang <ezyang@fb.com>
- Loading branch information
Showing
6 changed files
with
260 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Helper binary used by test_shell.py to print interleaved sequences | ||
# of strings to stderr/stdout. | ||
|
||
import sys | ||
from typing import Sequence, TypeVar, Tuple, Iterator | ||
import itertools | ||
|
||
T = TypeVar('T') | ||
|
||
|
||
def grouper(n: int, iterable: Sequence[T]) -> Iterator[Tuple[T, ...]]: | ||
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" | ||
args = [iter(iterable)] * n | ||
return itertools.zip_longest(*args) | ||
|
||
|
||
for mode, payload in grouper(2, sys.argv[1:]): | ||
if mode == 'e': | ||
print(payload, end='', file=sys.stderr) | ||
sys.stderr.flush() | ||
elif mode == 'o': | ||
print(payload, end='', file=sys.stdout) | ||
sys.stdout.flush() | ||
elif mode == 'r': | ||
# Big enough payload to exceed default chunk limit | ||
print("." * (4096 * 128), file=sys.stdout) | ||
sys.stdout.flush() | ||
else: | ||
raise RuntimeError('Unrecognized mode') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import unittest | ||
import logging | ||
from typing import List, Any | ||
from dataclasses import dataclass | ||
import sys | ||
|
||
import ghstack.expecttest as 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: | ||
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.assertExpected(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.assertExpected(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.assertExpected(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.assertExpected(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.assertExpected(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() | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |