1"""
2Test some lldb help commands.
3
4See also CommandInterpreter::OutputFormattedHelpText().
5"""
6
7import os, time
8import unittest2
9import lldb
10from lldbtest import *
11
12class HelpCommandTestCase(TestBase):
13
14    mydir = "help"
15
16    def test_simplehelp(self):
17        """A simple test of 'help' command and its output."""
18        self.expect("help",
19            startstr = 'The following is a list of built-in, permanent debugger commands')
20
21        self.expect("help", matching=False,
22                    substrs = ['next'])
23
24        self.expect("help -a", matching=True,
25                    substrs = ['next'])
26
27    def test_help_on_help(self):
28        """Testing the help on the help facility."""
29        self.expect("help help", matching=True,
30                    substrs = ['--show-aliases',
31                               '--hide-user-commands'])
32
33    def version_number_string(self):
34        """Helper function to find the version number string of lldb."""
35        plist = os.path.join(os.environ["LLDB_SRC"], "resources", "LLDB-Info.plist")
36        try:
37            CFBundleVersionSegFound = False
38            with open(plist, 'r') as f:
39                for line in f:
40                    if CFBundleVersionSegFound:
41                        version_line = line.strip()
42                        import re
43                        m = re.match("<string>(.*)</string>", version_line)
44                        if m:
45                            version = m.group(1)
46                            return version
47                        else:
48                            # Unsuccessful, let's juts break out of the for loop.
49                            break
50
51                    if line.find("<key>CFBundleVersion</key>") != -1:
52                        # Found our match.  The next line contains our version
53                        # string, for example:
54                        #
55                        #     <string>38</string>
56                        CFBundleVersionSegFound = True
57
58        except:
59            # Just fallthrough...
60            import traceback
61            traceback.print_exc()
62            pass
63
64        # Use None to signify that we are not able to grok the version number.
65        return None
66
67
68    def test_help_arch(self):
69        """Test 'help arch' which should list of supported architectures."""
70        self.expect("help arch",
71            substrs = ['arm', 'x86_64', 'i386'])
72
73    def test_help_version(self):
74        """Test 'help version' and 'version' commands."""
75        self.expect("help version",
76            substrs = ['Show version of LLDB debugger.'])
77        version_str = self.version_number_string()
78        import re
79        match = re.match('[0-9]+', version_str)
80        if sys.platform.startswith("darwin"):
81            search_regexp = ['lldb-' + (version_str if match else '[0-9]+')]
82        else:
83            search_regexp = ['lldb version (\d|\.)+.*$']
84
85        self.expect("version",
86            patterns = search_regexp)
87
88    def test_help_should_not_crash_lldb(self):
89        """Command 'help disasm' should not crash lldb."""
90        self.runCmd("help disasm", check=False)
91        self.runCmd("help unsigned-integer")
92
93    def test_help_should_not_hang_emacsshell(self):
94        """Command 'settings set term-width 0' should not hang the help command."""
95        self.expect("settings set term-width 0",
96                    COMMAND_FAILED_AS_EXPECTED, error=True,
97            substrs = ['error: 0 is out of range, valid values must be between'])
98        # self.runCmd("settings set term-width 0")
99        self.expect("help",
100            startstr = 'The following is a list of built-in, permanent debugger commands')
101
102    def test_help_breakpoint_set(self):
103        """Test that 'help breakpoint set' does not print out redundant lines of:
104        'breakpoint set [-s <shlib-name>] ...'."""
105        self.expect("help breakpoint set", matching=False,
106            substrs = ['breakpoint set [-s <shlib-name>]'])
107
108    def test_help_image_dump_symtab_should_not_crash(self):
109        """Command 'help image dump symtab' should not crash lldb."""
110        # 'image' is an alias for 'target modules'.
111        self.expect("help image dump symtab",
112            substrs = ['dump symtab',
113                       'sort-order'])
114
115    def test_help_image_du_sym_is_ambiguous(self):
116        """Command 'help image du sym' is ambiguous and spits out the list of candidates."""
117        self.expect("help image du sym",
118                    COMMAND_FAILED_AS_EXPECTED, error=True,
119            substrs = ['error: ambiguous command image du sym',
120                       'symfile',
121                       'symtab'])
122
123    def test_help_image_du_line_should_work(self):
124        """Command 'help image du line' is not ambiguous and should work."""
125        # 'image' is an alias for 'target modules'.
126        self.expect("help image du line",
127            substrs = ['Dump the line table for one or more compilation units'])
128
129    def test_help_target_variable_syntax(self):
130        """Command 'help target variable' should display <variable-name> ..."""
131        self.expect("help target variable",
132            substrs = ['<variable-name> [<variable-name> [...]]'])
133
134    def test_help_watchpoint_and_its_args(self):
135        """Command 'help watchpoint', 'help watchpt-id', and 'help watchpt-id-list' should work."""
136        self.expect("help watchpoint",
137            substrs = ['delete', 'disable', 'enable', 'list'])
138        self.expect("help watchpt-id",
139            substrs = ['<watchpt-id>'])
140        self.expect("help watchpt-id-list",
141            substrs = ['<watchpt-id-list>'])
142
143    def test_help_watchpoint_set(self):
144        """Test that 'help watchpoint set' prints out 'expression' and 'variable'
145        as the possible subcommands."""
146        self.expect("help watchpoint set",
147            substrs = ['The following subcommands are supported:'],
148            patterns = ['expression +--',
149                        'variable +--'])
150
151
152if __name__ == '__main__':
153    import atexit
154    lldb.SBDebugger.Initialize()
155    atexit.register(lambda: lldb.SBDebugger.Terminate())
156    unittest2.main()
157