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=60, 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 env["HOME"] = self.getBuildDir() 41 42 import pexpect 43 self.child = pexpect.spawn( 44 lldbtest_config.lldbExec, args=args, logfile=logfile, 45 timeout=timeout, dimensions=dimensions, env=env) 46 self.expect_prompt() 47 for cmd in self.setUpCommands(): 48 self.child.expect_exact(cmd) 49 self.expect_prompt() 50 if executable is not None: 51 self.child.expect_exact("target create") 52 self.child.expect_exact("Current executable set to") 53 self.expect_prompt() 54 55 def expect(self, cmd, substrs=None): 56 self.assertNotIn('\n', 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 62 self.child.sendline(cmd) 63 if substrs is not None: 64 for s in substrs: 65 self.child.expect_exact(s) 66 self.expect_prompt() 67 68 def quit(self, gracefully=True): 69 self.child.sendeof() 70 self.child.close(force=not gracefully) 71 self.child = None 72 73 def cursor_forward_escape_seq(self, chars_to_move): 74 """ 75 Returns the escape sequence to move the cursor forward/right 76 by a certain amount of characters. 77 """ 78 return b"\x1b\[" + str(chars_to_move).encode("utf-8") + b"C" 79