|
| 1 | +""" |
| 2 | +Test that HandleCommand captures stdout and stderr from script commands. |
| 3 | +""" |
| 4 | + |
| 5 | +import lldb |
| 6 | +from lldbsuite.test.decorators import * |
| 7 | +from lldbsuite.test.lldbtest import * |
| 8 | + |
| 9 | + |
| 10 | +class CommandScriptOutputTestCase(TestBase): |
| 11 | + NO_DEBUG_INFO_TESTCASE = True |
| 12 | + |
| 13 | + def test_script_command_stdout_stderr(self): |
| 14 | + """Test that HandleCommand captures stdout and stderr from script commands.""" |
| 15 | + ci = self.dbg.GetCommandInterpreter() |
| 16 | + self.assertTrue(ci, VALID_COMMAND_INTERPRETER) |
| 17 | + |
| 18 | + res = lldb.SBCommandReturnObject() |
| 19 | + |
| 20 | + # Execute a script command that writes to stdout. |
| 21 | + ci.HandleCommand("script print('Hello stdout')", res) |
| 22 | + self.assertTrue(res.Succeeded()) |
| 23 | + self.assertIn("Hello stdout", res.GetOutput()) |
| 24 | + |
| 25 | + # Execute a script command that writes to stderr. |
| 26 | + ci.HandleCommand("script import sys; sys.stderr.write('Hello stderr\\n')", res) |
| 27 | + self.assertTrue(res.Succeeded()) |
| 28 | + self.assertIn("Hello stderr", res.GetOutput()) |
| 29 | + |
| 30 | + # Execute a script command that writes to both stdout and stderr. |
| 31 | + ci.HandleCommand( |
| 32 | + "script import sys; print('Output line'); sys.stderr.write('Error line\\n')", |
| 33 | + res, |
| 34 | + ) |
| 35 | + self.assertTrue(res.Succeeded()) |
| 36 | + self.assertIn("Output line", res.GetOutput()) |
| 37 | + self.assertIn("Error line", res.GetOutput()) |
| 38 | + |
| 39 | + # Test that multiple print statements are captured. |
| 40 | + ci.HandleCommand( |
| 41 | + "script print('Line 1'); print('Line 2'); print('Line 3')", res |
| 42 | + ) |
| 43 | + self.assertTrue(res.Succeeded()) |
| 44 | + output = res.GetOutput() |
| 45 | + self.assertIn("Line 1", output) |
| 46 | + self.assertIn("Line 2", output) |
| 47 | + self.assertIn("Line 3", output) |
0 commit comments