xref: /llvm-project/lldb/packages/Python/lldbsuite/test/lldbpexpect.py (revision 1351a9b19ecf48ebb2caad8ae6d8776a6dc1786d)
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,
27               dimensions=None, run_under=None, post_spawn=None,
28               use_colors=False):
29        logfile = getattr(sys.stdout, 'buffer',
30                            sys.stdout) if self.TraceOn() else None
31
32        args = []
33        if run_under is not None:
34            args += run_under
35        args += [lldbtest_config.lldbExec, '--no-lldbinit']
36        if not use_colors:
37            args += '--no-use-colors'
38        for cmd in self.setUpCommands():
39            if "use-color false" in cmd and use_colors:
40                continue
41            args += ['-O', cmd]
42        if executable is not None:
43            args += ['--file', executable]
44        if extra_args is not None:
45            args.extend(extra_args)
46
47        env = dict(os.environ)
48        env["TERM"] = "vt100"
49        env["HOME"] = self.getBuildDir()
50
51        import pexpect
52        self.child = pexpect.spawn(
53                args[0], args=args[1:], logfile=logfile,
54                timeout=timeout, dimensions=dimensions, env=env)
55        self.child.ptyproc.delayafterclose = timeout/10
56        self.child.ptyproc.delayafterterminate = timeout/10
57
58        if post_spawn is not None:
59            post_spawn()
60        self.expect_prompt()
61        for cmd in self.setUpCommands():
62            if "use-color false" in cmd and use_colors:
63                continue
64            self.child.expect_exact(cmd)
65            self.expect_prompt()
66        if executable is not None:
67            self.child.expect_exact("target create")
68            self.child.expect_exact("Current executable set to")
69            self.expect_prompt()
70
71    def expect(self, cmd, substrs=None):
72        self.assertNotIn('\n', cmd)
73        # If 'substrs' is a string then this code would just check that every
74        # character of the string is in the output.
75        assert not isinstance(substrs, six.string_types), \
76            "substrs must be a collection of strings"
77
78        self.child.sendline(cmd)
79        if substrs is not None:
80            for s in substrs:
81                self.child.expect_exact(s)
82        self.expect_prompt()
83
84    def quit(self, gracefully=True):
85        self.child.sendeof()
86        self.child.close(force=not gracefully)
87        self.child = None
88
89    def cursor_forward_escape_seq(self, chars_to_move):
90        """
91        Returns the escape sequence to move the cursor forward/right
92        by a certain amount of characters.
93        """
94        return b"\x1b\[" + str(chars_to_move).encode("utf-8") + b"C"
95