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