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