1from __future__ import absolute_import 2 3# System modules 4import os 5import sys 6 7# Third-party modules 8import six 9 10# LLDB Modules 11import lldb 12from .lldbtest import * 13from . import lldbutil 14from lldbsuite.test.decorators import * 15 16@skipIfRemote 17@skipIfWindows # llvm.org/pr22274: need a pexpect replacement for windows 18class PExpectTest(TestBase): 19 20 NO_DEBUG_INFO_TESTCASE = True 21 PROMPT = "(lldb) " 22 23 def expect_prompt(self): 24 self.child.expect_exact(self.PROMPT) 25 26 def launch(self, executable=None, extra_args=None, timeout=30, dimensions=None): 27 logfile = getattr(sys.stdout, 'buffer', 28 sys.stdout) if self.TraceOn() else None 29 30 args = ['--no-lldbinit', '--no-use-colors'] 31 for cmd in self.setUpCommands(): 32 args += ['-O', cmd] 33 if executable is not None: 34 args += ['--file', executable] 35 if extra_args is not None: 36 args.extend(extra_args) 37 38 env = dict(os.environ) 39 env["TERM"]="vt100" 40 41 import pexpect 42 self.child = pexpect.spawn( 43 lldbtest_config.lldbExec, args=args, logfile=logfile, 44 timeout=timeout, dimensions=dimensions, env=env) 45 self.expect_prompt() 46 for cmd in self.setUpCommands(): 47 self.child.expect_exact(cmd) 48 self.expect_prompt() 49 if executable is not None: 50 self.child.expect_exact("target create") 51 self.child.expect_exact("Current executable set to") 52 self.expect_prompt() 53 54 def expect(self, cmd, substrs=None): 55 self.assertNotIn('\n', cmd) 56 self.child.sendline(cmd) 57 # If 'substrs' is a string then this code would just check that every 58 # character of the string is in the output. 59 assert not isinstance(substrs, six.string_types), \ 60 "substrs must be a collection of strings" 61 if substrs is not None: 62 for s in substrs: 63 self.child.expect_exact(s) 64 self.expect_prompt() 65 66 def quit(self, gracefully=True): 67 self.child.sendeof() 68 self.child.close(force=not gracefully) 69 self.child = None 70 71 def cursor_forward_escape_seq(self, chars_to_move): 72 """ 73 Returns the escape sequence to move the cursor forward/right 74 by a certain amount of characters. 75 """ 76 return b"\x1b\[" + str(chars_to_move).encode("utf-8") + b"C" 77