| 1 |
#!/usr/bin/env python3 |
| 2 |
import contextlib |
| 3 |
import os |
| 4 |
from pathlib import Path |
| 5 |
import tempfile |
| 6 |
|
| 7 |
import drgn.commands._builtin # noqa: F401 |
| 8 |
from tests.commands import CommandTestCase |
| 9 |
|
| 10 |
|
| 11 |
@contextlib.contextmanager |
| 12 |
def temporary_working_directory(): |
| 13 |
old_working_directory = os.getcwd() |
| 14 |
with tempfile.TemporaryDirectory() as f: |
| 15 |
try: |
| 16 |
os.chdir(f) |
| 17 |
yield f |
| 18 |
finally: |
| 19 |
os.chdir(old_working_directory) |
| 20 |
|
| 21 |
|
| 22 |
class RedirectedFile: |
| 23 |
def __init__(self, f): |
| 24 |
self.tempfile = f |
| 25 |
self.value = None |
| 26 |
|
| 27 |
|
| 28 |
@contextlib.contextmanager |
| 29 |
def redirect(stdout=False, stderr=False): |
| 30 |
# To redirect stdout for commands, we need a real file descriptor, not just |
| 31 |
# a StringIO |
| 32 |
with contextlib.ExitStack() as stack: |
| 33 |
f = stack.enter_context(tempfile.TemporaryFile("w+t")) |
| 34 |
if stdout: |
| 35 |
stack.enter_context(contextlib.redirect_stdout(f)) |
| 36 |
if stderr: |
| 37 |
stack.enter_context(contextlib.redirect_stderr(f)) |
| 38 |
redir = RedirectedFile(f) |
| 39 |
try: |
| 40 |
yield redir |
| 41 |
finally: |
| 42 |
f.seek(0) |
| 43 |
redir.value = f.read() |
| 44 |
|
| 45 |
|
| 46 |
class TestPyCommand(CommandTestCase): |
| 47 |
|
| 48 |
def test_py_redirect(self): |
| 49 |
with temporary_working_directory() as temp_dir: |
| 50 |
path = Path(temp_dir) / "6" |
| 51 |
self.run_command("py var = 5; var > 6") |
| 52 |
self.assertEqual(path.read_text(), "5\n") |
| 53 |
|
| 54 |
def test_py_paren_avoid_redirect(self): |
| 55 |
self.run_command("py var = 5; (var > 6)") |
| 56 |
with redirect(stdout=True) as f: |
| 57 |
self.run_command("py var = 5; (var > 6)") |
| 58 |
self.assertEqual(f.value, "False\n") |
| 59 |
|
| 60 |
def test_py_pipe(self): |
| 61 |
with redirect(stdout=True) as f: |
| 62 |
self.run_command("py echo = 5; 2 | echo + 5") |
| 63 |
self.assertEqual(f.value, "+ 5\n") |
| 64 |
|
| 65 |
def test_py_avoid_pipe(self): |
| 66 |
with redirect(stdout=True) as f: |
| 67 |
self.run_command("py echo = 5; (2 | (echo + 5))") |
| 68 |
self.assertEqual(f.value, "10\n") |
| 69 |
|
| 70 |
def test_py_chooses_first_pipe(self): |
| 71 |
with redirect(stdout=True) as f: |
| 72 |
# If the first | is used to separate the Python from the pipeline |
| 73 |
# (the expected behavior), then we'll get the value 5 written into |
| 74 |
# the "echo" command, which will ignore that and write "+ 6" through |
| 75 |
# the cat process to stdout. If the second | is used to separate the |
| 76 |
# Python from the pipeline, then we'll get "15" written into the cat |
| 77 |
# process. If none of the | were interpreted as a pipeline operator, |
| 78 |
# then the statement would output 31. |
| 79 |
self.run_command("py echo = 5; cat = 16; 5 | echo + 6 | cat") |
| 80 |
self.assertEqual("+ 6\n", f.value) |
| 81 |
|
| 82 |
def test_py_traceback_on_syntax_error(self): |
| 83 |
with redirect(stderr=True) as f: |
| 84 |
self.run_command("py a +") |
| 85 |
# SyntaxError does not print the "Traceback" header. Rather than trying |
| 86 |
# to assert too much about the format of the traceback, just assert that |
| 87 |
# the incorrect code is shown, as it would be for a traceback. |
| 88 |
self.assertTrue("a +" in f.value) |
| 89 |
self.assertTrue("SyntaxError" in f.value) |
| 90 |
|
| 91 |
def test_py_traceback_on_exception(self): |
| 92 |
with redirect(stderr=True) as f: |
| 93 |
self.run_command("py raise Exception('text')") |
| 94 |
self.assertTrue(f.value.startswith("Traceback")) |
| 95 |
self.assertTrue("Exception" in f.value) |