summaryrefslogtreecommitdiffstats
path: root/lldb/packages/Python/lldbsuite/test
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/packages/Python/lldbsuite/test')
-rw-r--r--lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py126
1 files changed, 125 insertions, 1 deletions
diff --git a/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
index fe75521e1b1..3231fdfe688 100644
--- a/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
+++ b/lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py
@@ -12,9 +12,19 @@ from contextlib import contextmanager
import lldb
from lldbsuite.test import lldbtest
-from lldbsuite.test.decorators import add_test_categories, no_debug_info_test
+from lldbsuite.test.decorators import (
+ add_test_categories, no_debug_info_test, skipIf)
+@contextmanager
+def replace_stdout(new):
+ old = sys.stdout
+ sys.stdout = new
+ try:
+ yield
+ finally:
+ sys.stdout = old
+
def readStrippedLines(f):
def i():
for line in f:
@@ -66,6 +76,8 @@ class FileHandleTestCase(lldbtest.TestBase):
interpreter.HandleCommand(cmd, ret)
else:
self.debugger.HandleCommand(cmd)
+ self.debugger.GetOutputFile().Flush()
+ self.debugger.GetErrorFile().Flush()
if collect_result and check:
self.assertTrue(ret.Succeeded())
return ret.GetOutput()
@@ -97,6 +109,19 @@ class FileHandleTestCase(lldbtest.TestBase):
with open(self.out_filename, 'r') as f:
self.assertIn('deadbeef', f.read())
+ @add_test_categories(['pyapi'])
+ @no_debug_info_test
+ def test_legacy_file_err_with_get(self):
+ with open(self.out_filename, 'w') as f:
+ self.debugger.SetErrorFileHandle(f, False)
+ self.handleCmd('lolwut', check=False, collect_result=False)
+ self.debugger.GetErrorFileHandle().write('FOOBAR\n')
+ lldb.SBDebugger.Destroy(self.debugger)
+ with open(self.out_filename, 'r') as f:
+ errors = f.read()
+ self.assertTrue(re.search(r'error:.*lolwut', errors))
+ self.assertTrue(re.search(r'FOOBAR', errors))
+
@add_test_categories(['pyapi'])
@no_debug_info_test
@@ -148,3 +173,102 @@ class FileHandleTestCase(lldbtest.TestBase):
self.assertTrue(e.Success())
self.assertEqual(buffer[:n], b'FOO')
+
+ @add_test_categories(['pyapi'])
+ @no_debug_info_test
+ def test_fileno_out(self):
+ with open(self.out_filename, 'w') as f:
+ sbf = lldb.SBFile(f.fileno(), "w", False)
+ status = self.debugger.SetOutputFile(sbf)
+ self.assertTrue(status.Success())
+ self.handleCmd('script 1+2')
+ self.debugger.GetOutputFile().Write(b'quux')
+
+ with open(self.out_filename, 'r') as f:
+ self.assertEqual(readStrippedLines(f), ['3', 'quux'])
+
+
+ @add_test_categories(['pyapi'])
+ @no_debug_info_test
+ def test_fileno_help(self):
+ with open(self.out_filename, 'w') as f:
+ sbf = lldb.SBFile(f.fileno(), "w", False)
+ status = self.debugger.SetOutputFile(sbf)
+ self.assertTrue(status.Success())
+ self.handleCmd("help help", collect_result=False, check=False)
+ with open(self.out_filename, 'r') as f:
+ self.assertTrue(re.search(r'Show a list of all debugger commands', f.read()))
+
+
+ @add_test_categories(['pyapi'])
+ @no_debug_info_test
+ def test_immediate(self):
+ with open(self.out_filename, 'w') as f:
+ ret = lldb.SBCommandReturnObject()
+ ret.SetImmediateOutputFile(f)
+ interpreter = self.debugger.GetCommandInterpreter()
+ interpreter.HandleCommand("help help", ret)
+ # make sure the file wasn't closed early.
+ f.write("\nQUUX\n")
+
+ ret = None # call destructor and flush streams
+
+ with open(self.out_filename, 'r') as f:
+ output = f.read()
+ self.assertTrue(re.search(r'Show a list of all debugger commands', output))
+ self.assertTrue(re.search(r'QUUX', output))
+
+
+ @add_test_categories(['pyapi'])
+ @no_debug_info_test
+ def test_fileno_inout(self):
+ with open(self.in_filename, 'w') as f:
+ f.write("help help\n")
+
+ with open(self.out_filename, 'w') as outf, open(self.in_filename, 'r') as inf:
+
+ outsbf = lldb.SBFile(outf.fileno(), "w", False)
+ status = self.debugger.SetOutputFile(outsbf)
+ self.assertTrue(status.Success())
+
+ insbf = lldb.SBFile(inf.fileno(), "r", False)
+ status = self.debugger.SetInputFile(insbf)
+ self.assertTrue(status.Success())
+
+ opts = lldb.SBCommandInterpreterRunOptions()
+ self.debugger.RunCommandInterpreter(True, False, opts, 0, False, False)
+ self.debugger.GetOutputFile().Flush()
+
+ with open(self.out_filename, 'r') as f:
+ self.assertTrue(re.search(r'Show a list of all debugger commands', f.read()))
+
+
+ @add_test_categories(['pyapi'])
+ @no_debug_info_test
+ def test_fileno_error(self):
+ with open(self.out_filename, 'w') as f:
+
+ sbf = lldb.SBFile(f.fileno(), 'w', False)
+ status = self.debugger.SetErrorFile(sbf)
+ self.assertTrue(status.Success())
+
+ self.handleCmd('lolwut', check=False, collect_result=False)
+
+ self.debugger.GetErrorFile().Write(b'\nzork\n')
+
+ with open(self.out_filename, 'r') as f:
+ errors = f.read()
+ self.assertTrue(re.search(r'error:.*lolwut', errors))
+ self.assertTrue(re.search(r'zork', errors))
+
+ #FIXME This shouldn't fail for python2 either.
+ @add_test_categories(['pyapi'])
+ @no_debug_info_test
+ @skipIf(py_version=['<', (3,)])
+ def test_replace_stdout(self):
+ f = io.StringIO()
+ with replace_stdout(f):
+ self.assertEqual(sys.stdout, f)
+ self.handleCmd('script sys.stdout.write("lol")',
+ collect_result=False, check=False)
+ self.assertEqual(sys.stdout, f)
OpenPOWER on IntegriCloud