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