xref: /llvm-project/lldb/packages/Python/lldbsuite/test/lldbpexpect.py (revision df46f174db5bbedb66d77041308d48e701c77e96)
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
16if sys.platform.startswith('win32'):
17    # llvm.org/pr22274: need a pexpect replacement for windows
18    class PExpectTest(object):
19        pass
20else:
21    import pexpect
22
23    @skipIfRemote
24    class PExpectTest(TestBase):
25
26        NO_DEBUG_INFO_TESTCASE = True
27        PROMPT = "(lldb) "
28
29        def expect_prompt(self):
30            self.child.expect_exact(self.PROMPT)
31
32        def launch(self, executable=None, extra_args=None, timeout=30, dimensions=None):
33            logfile = getattr(sys.stdout, 'buffer',
34                              sys.stdout) if self.TraceOn() else None
35
36            args = ['--no-lldbinit', '--no-use-colors']
37            for cmd in self.setUpCommands():
38                args += ['-O', cmd]
39            if executable is not None:
40                args += ['--file', executable]
41            if extra_args is not None:
42                args.extend(extra_args)
43
44            env = dict(os.environ)
45            env["TERM"]="vt100"
46
47            self.child = pexpect.spawn(
48                    lldbtest_config.lldbExec, args=args, logfile=logfile,
49                    timeout=timeout, dimensions=dimensions, env=env)
50            self.expect_prompt()
51            for cmd in self.setUpCommands():
52                self.child.expect_exact(cmd)
53                self.expect_prompt()
54            if executable is not None:
55                self.child.expect_exact("target create")
56                self.child.expect_exact("Current executable set to")
57                self.expect_prompt()
58
59        def expect(self, cmd, substrs=None):
60            self.assertNotIn('\n', cmd)
61            self.child.sendline(cmd)
62            if substrs is not None:
63                for s in substrs:
64                    self.child.expect_exact(s)
65            self.expect_prompt()
66
67        def quit(self, gracefully=True):
68            self.child.sendeof()
69            self.child.close(force=not gracefully)
70            self.child = None
71