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