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