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