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