xref: /llvm-project/lldb/test/API/commands/command/container/TestContainerCommands.py (revision 39ea676d9d0ea467b7e5fe2d5c25d22a2d906041)
1"""
2Test user added container commands
3"""
4
5
6import sys
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10
11
12class TestCmdContainer(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def test_container_add(self):
18        self.container_add()
19
20    def check_command_tree_exists(self):
21        """This makes sure we can still run the command tree we added."""
22        self.runCmd("test-multi")
23        self.runCmd("test-multi test-multi-sub")
24        self.runCmd("test-multi test-multi-sub welcome")
25
26    def container_add(self):
27        # Make sure we can't overwrite built-in commands:
28        self.expect("command container add process", "Can't replace builtin container command",
29                    substrs=["can't replace builtin command"], error=True)
30        self.expect("command container add process non_such_subcommand", "Can't add to built-in subcommand",
31                    substrs=["Path component: 'process' is not a user command"], error=True)
32        self.expect("command container add process launch", "Can't replace builtin subcommand",
33                    substrs=["Path component: 'process' is not a user command"], error=True)
34
35        # Now lets make a container command:
36        self.runCmd("command container add -h 'A test container command' test-multi")
37        # Make sure the help works:
38        self.expect("help test-multi", "Help works for top-level multi",
39                    substrs=["A test container command"])
40        # Add a subcommand:
41        self.runCmd("command container add -h 'A test container sub-command' test-multi test-multi-sub")
42        # Make sure the help works:
43        self.expect("help test-multi", "Help shows sub-multi",
44                    substrs=["A test container command", "test-multi-sub -- A test container sub-command"])
45        self.expect("help test-multi test-multi-sub", "Help shows sub-multi",
46                    substrs=["A test container sub-command"])
47
48        # Now add a script based command to the container command:
49        self.runCmd("command script import welcome.py")
50        self.runCmd("command script add -c welcome.WelcomeCommand test-multi test-multi-sub welcome")
51        # Make sure the help still works:
52        self.expect("help test-multi test-multi-sub", "Listing subcommands works",
53                    substrs=["A test container sub-command", "welcome -- Just a docstring for Welcome"])
54        self.expect("help test-multi test-multi-sub welcome", "Subcommand help works",
55                    substrs=["Just a docstring for Welcome"])
56        # And make sure it actually works:
57        self.expect("test-multi test-multi-sub welcome friend", "Test command works",
58                    substrs=["Hello friend, welcome to LLDB"])
59
60        # Make sure overwriting works, first the leaf command:
61        # We should not be able to remove extant commands by default:
62        self.expect("command script add -c welcome.WelcomeCommand2 test-multi test-multi-sub welcome",
63                    "overwrite command w/o -o",
64                    substrs=["cannot add command: sub-command already exists"], error=True)
65        # But we can with the -o option:
66        self.runCmd("command script add -c welcome.WelcomeCommand2 -o test-multi test-multi-sub welcome")
67        # Make sure we really did overwrite:
68        self.expect("test-multi test-multi-sub welcome friend", "Used the new command class",
69                    substrs=["Hello friend, welcome again to LLDB"])
70
71        self.expect("apropos welcome", "welcome should show up in apropos", substrs=["A docstring for the second Welcome"])
72
73        # Make sure we give good errors when the input is wrong:
74        self.expect("command script delete test-mult test-multi-sub welcome", "Delete script command - wrong first path component",
75                    substrs=["'test-mult' not found"], error=True)
76
77        self.expect("command script delete test-multi test-multi-su welcome", "Delete script command - wrong second path component",
78                    substrs=["'test-multi-su' not found"], error=True)
79        self.check_command_tree_exists()
80
81        self.expect("command script delete test-multi test-multi-sub welcom", "Delete script command - wrong leaf component",
82                    substrs=["'welcom' not found"], error=True)
83        self.check_command_tree_exists()
84
85        self.expect("command script delete test-multi test-multi-sub", "Delete script command - no leaf component",
86                    substrs=["subcommand 'test-multi-sub' is not a user command"], error=True)
87        self.check_command_tree_exists()
88
89        # You can't use command script delete to delete container commands:
90        self.expect("command script delete test-multi", "Delete script - can't delete container",
91                    substrs=["command 'test-multi' is a multi-word command."], error=True)
92        self.expect("command script delete test-multi test-multi-sub", "Delete script - can't delete container",
93                    substrs=["subcommand 'test-multi-sub' is not a user command"], error = True)
94
95        # You can't use command container delete to delete scripted commands:
96        self.expect("command container delete test-multi test-multi-sub welcome", "command container can't delete user commands",
97                    substrs=["subcommand 'welcome' is not a container command"], error = True)
98
99        # Also make sure you can't alias on top of container commands:
100        self.expect("command alias test-multi process launch", "Tried to alias on top of a container command",
101                    substrs=["'test-multi' is a user container command and cannot be overwritten."], error=True)
102        self.check_command_tree_exists()
103
104        # Also assert that we can't delete builtin commands:
105        self.expect("command script delete process launch", "Delete builtin command fails", substrs=["command 'process' is not a user command"], error=True)
106        # Now let's do the version that works
107        self.expect("command script delete test-multi test-multi-sub welcome", "Delete script command by path", substrs=["Deleted command: test-multi test-multi-sub welcome"])
108
109        # Now overwrite the sub-command, it should end up empty:
110        self.runCmd("command container add -h 'A different help string' -o test-multi test-multi-sub")
111        # welcome should be gone:
112        self.expect("test-multi test-multi-sub welcome friend", "did remove subcommand",
113                    substrs=["'test-multi-sub' does not have any subcommands."], error=True)
114        # We should have the new help:
115        self.expect("help test-multi test-multi-sub", "help changed",
116                    substrs=["A different help string"])
117
118        # Now try deleting commands.
119        self.runCmd("command container delete test-multi test-multi-sub")
120        self.expect("test-multi test-multi-sub", "Command is not active", error=True,
121                    substrs = ["'test-multi' does not have any subcommands"])
122        self.expect("help test-multi", matching=False, substrs=["test-multi-sub"])
123
124
125        # Next the root command:
126        self.runCmd("command container delete test-multi")
127        self.expect("test-multi", "Root command gone", substrs=["'test-multi' is not a valid command."], error=True)
128