xref: /llvm-project/lldb/test/API/commands/command/container/TestContainerCommands.py (revision 8c3a6fe37fc36537b939ea75a59fb2d5963246fb)
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 on the leaf command.  First using the
61        # explicit option so we should not be able to remove extant commands by default:
62
63        self.expect("command script add -c welcome.WelcomeCommand2 test-multi test-multi-sub welcome",
64                    "overwrite command w/o -o",
65                    substrs=["cannot add command: sub-command already exists"], error=True)
66        # But we can with the -o option:
67        self.runCmd("command script add -c welcome.WelcomeCommand2 -o test-multi test-multi-sub welcome")
68        # Make sure we really did overwrite:
69        self.expect("test-multi test-multi-sub welcome friend", "Used the new command class",
70                    substrs=["Hello friend, welcome again to LLDB"])
71        self.expect("apropos welcome", "welcome should show up in apropos", substrs=["A docstring for the second Welcome"])
72        self.expect("help test-multi test-multi-sub welcome", "welcome should show up in help", substrs=["A docstring for the second Welcome"])
73        self.expect("help", "test-multi should show up in help", substrs=["test-multi"])
74
75        # Now switch the default and make sure we can now delete w/o the overwrite option:
76        self.runCmd("settings set interpreter.require-overwrite 0")
77        self.runCmd("command script add -c welcome.WelcomeCommand test-multi test-multi-sub welcome")
78        # Make sure we really did overwrite:
79        self.expect("test-multi test-multi-sub welcome friend", "Used the new command class",
80                    substrs=["Hello friend, welcome to LLDB"])
81
82        # Make sure we give good errors when the input is wrong:
83        self.expect("command script delete test-mult test-multi-sub welcome", "Delete script command - wrong first path component",
84                    substrs=["'test-mult' not found"], error=True)
85
86        self.expect("command script delete test-multi test-multi-su welcome", "Delete script command - wrong second path component",
87                    substrs=["'test-multi-su' not found"], error=True)
88        self.check_command_tree_exists()
89
90        self.expect("command script delete test-multi test-multi-sub welcom", "Delete script command - wrong leaf component",
91                    substrs=["'welcom' not found"], error=True)
92        self.check_command_tree_exists()
93
94        self.expect("command script delete test-multi test-multi-sub", "Delete script command - no leaf component",
95                    substrs=["subcommand 'test-multi-sub' is not a user command"], error=True)
96        self.check_command_tree_exists()
97
98        # You can't use command script delete to delete container commands:
99        self.expect("command script delete test-multi", "Delete script - can't delete container",
100                    substrs=["command 'test-multi' is a multi-word command."], error=True)
101        self.expect("command script delete test-multi test-multi-sub", "Delete script - can't delete container",
102                    substrs=["subcommand 'test-multi-sub' is not a user command"], error = True)
103
104        # You can't use command container delete to delete scripted commands:
105        self.expect("command container delete test-multi test-multi-sub welcome", "command container can't delete user commands",
106                    substrs=["subcommand 'welcome' is not a container command"], error = True)
107
108        # Also make sure you can't alias on top of container commands:
109        self.expect("command alias test-multi process launch", "Tried to alias on top of a container command",
110                    substrs=["'test-multi' is a user container command and cannot be overwritten."], error=True)
111        self.check_command_tree_exists()
112
113        # Also assert that we can't delete builtin commands:
114        self.expect("command script delete process launch", "Delete builtin command fails", substrs=["command 'process' is not a user command"], error=True)
115        # Now let's do the version that works
116        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"])
117
118        # Now overwrite the sub-command, it should end up empty:
119        self.runCmd("command container add -h 'A different help string' -o test-multi test-multi-sub")
120        # welcome should be gone:
121        self.expect("test-multi test-multi-sub welcome friend", "did remove subcommand",
122                    substrs=["'test-multi-sub' does not have any subcommands."], error=True)
123        # We should have the new help:
124        self.expect("help test-multi test-multi-sub", "help changed",
125                    substrs=["A different help string"])
126
127        # Now try deleting commands.
128        self.runCmd("command container delete test-multi test-multi-sub")
129        self.expect("test-multi test-multi-sub", "Command is not active", error=True,
130                    substrs = ["'test-multi' does not have any subcommands"])
131        self.expect("help test-multi", matching=False, substrs=["test-multi-sub"])
132
133
134        # Next the root command:
135        self.runCmd("command container delete test-multi")
136        self.expect("test-multi", "Root command gone", substrs=["'test-multi' is not a valid command."], error=True)
137