xref: /llvm-project/lldb/packages/Python/lldbsuite/test/lldbpexpect.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1from __future__ import absolute_import
2
3# System modules
4import os
5import sys
6
7# LLDB Modules
8import lldb
9from .lldbtest import *
10from . import lldbutil
11from lldbsuite.test.decorators import *
12
13
14@skipIfRemote
15@skipIfWindows  # llvm.org/pr22274: need a pexpect replacement for windows
16class PExpectTest(TestBase):
17    NO_DEBUG_INFO_TESTCASE = True
18    PROMPT = "(lldb) "
19
20    def expect_prompt(self):
21        self.child.expect_exact(self.PROMPT)
22
23    def launch(
24        self,
25        executable=None,
26        extra_args=None,
27        timeout=60,
28        dimensions=None,
29        run_under=None,
30        post_spawn=None,
31        use_colors=False,
32    ):
33        logfile = getattr(sys.stdout, "buffer", sys.stdout) if self.TraceOn() else None
34
35        args = []
36        if run_under is not None:
37            args += run_under
38        args += [lldbtest_config.lldbExec, "--no-lldbinit"]
39        if not use_colors:
40            args.append("--no-use-colors")
41        for cmd in self.setUpCommands():
42            if "use-color false" in cmd and use_colors:
43                continue
44            args += ["-O", cmd]
45        if executable is not None:
46            args += ["--file", executable]
47        if extra_args is not None:
48            args.extend(extra_args)
49
50        env = dict(os.environ)
51        env["TERM"] = "vt100"
52        env["HOME"] = self.getBuildDir()
53
54        import pexpect
55
56        self.child = pexpect.spawn(
57            args[0],
58            args=args[1:],
59            logfile=logfile,
60            timeout=timeout,
61            dimensions=dimensions,
62            env=env,
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