xref: /llvm-project/lldb/packages/Python/lldbsuite/test/lldbpexpect.py (revision 1ea909270c6d9ad612a46c2a15c13acc4f6d623a)
1from __future__ import print_function
2from __future__ import absolute_import
3
4# System modules
5import sys
6
7# Third-party modules
8import six
9
10# LLDB Modules
11import lldb
12from .lldbtest import *
13from . import lldbutil
14
15if sys.platform.startswith('win32'):
16    # llvm.org/pr22274: need a pexpect replacement for windows
17    class PExpectTest(object):
18        pass
19else:
20    import pexpect
21
22    class PExpectTest(TestBase):
23
24        NO_DEBUG_INFO_TESTCASE = True
25        PROMPT = "(lldb) "
26
27        def expect_prompt(self):
28            self.child.expect_exact(self.PROMPT)
29
30        def launch(self, executable=None, timeout=30, dimensions=None):
31            logfile = getattr(sys.stdout, 'buffer',
32                              sys.stdout) if self.TraceOn() else None
33            args = ['--no-lldbinit', '--no-use-colors']
34            for cmd in self.setUpCommands():
35                args += ['-O', cmd]
36            if executable is not None:
37                args += ['--file', executable]
38            self.child = pexpect.spawn(
39                    lldbtest_config.lldbExec, args=args, logfile=logfile,
40                    timeout=timeout, dimensions=dimensions)
41            self.expect_prompt()
42            for cmd in self.setUpCommands():
43                self.child.expect_exact(cmd)
44                self.expect_prompt()
45            if executable is not None:
46                self.child.expect_exact("target create")
47                self.child.expect_exact("Current executable set to")
48                self.expect_prompt()
49
50        def expect(self, cmd, substrs=None):
51            self.child.sendline(cmd)
52            if substrs is not None:
53                for s in substrs:
54                    self.child.expect_exact(s)
55            self.expect_prompt()
56
57        def quit(self, gracefully=True):
58            self.child.sendeof()
59            self.child.close(force=not gracefully)
60            self.child = None
61