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 NO_DEBUG_INFO_TESTCASE = True 14c5011aedSJim Ingham 15c5011aedSJim Ingham def test_container_add(self): 16c5011aedSJim Ingham self.container_add() 17c5011aedSJim Ingham 18c5011aedSJim Ingham def check_command_tree_exists(self): 19c5011aedSJim Ingham """This makes sure we can still run the command tree we added.""" 20c5011aedSJim Ingham self.runCmd("test-multi") 21c5011aedSJim Ingham self.runCmd("test-multi test-multi-sub") 22c5011aedSJim Ingham self.runCmd("test-multi test-multi-sub welcome") 23c5011aedSJim Ingham 24c5011aedSJim Ingham def container_add(self): 25c5011aedSJim Ingham # Make sure we can't overwrite built-in commands: 26*2238dcc3SJonas Devlieghere self.expect( 27*2238dcc3SJonas Devlieghere "command container add process", 28*2238dcc3SJonas Devlieghere "Can't replace builtin container command", 29*2238dcc3SJonas Devlieghere substrs=["can't replace builtin command"], 30*2238dcc3SJonas Devlieghere error=True, 31*2238dcc3SJonas Devlieghere ) 32*2238dcc3SJonas Devlieghere self.expect( 33*2238dcc3SJonas Devlieghere "command container add process non_such_subcommand", 34*2238dcc3SJonas Devlieghere "Can't add to built-in subcommand", 35*2238dcc3SJonas Devlieghere substrs=["Path component: 'process' is not a user command"], 36*2238dcc3SJonas Devlieghere error=True, 37*2238dcc3SJonas Devlieghere ) 38*2238dcc3SJonas Devlieghere self.expect( 39*2238dcc3SJonas Devlieghere "command container add process launch", 40*2238dcc3SJonas Devlieghere "Can't replace builtin subcommand", 41*2238dcc3SJonas Devlieghere substrs=["Path component: 'process' is not a user command"], 42*2238dcc3SJonas Devlieghere error=True, 43*2238dcc3SJonas Devlieghere ) 44c5011aedSJim Ingham 45c5011aedSJim Ingham # Now lets make a container command: 46c5011aedSJim Ingham self.runCmd("command container add -h 'A test container command' test-multi") 47c5011aedSJim Ingham # Make sure the help works: 48*2238dcc3SJonas Devlieghere self.expect( 49*2238dcc3SJonas Devlieghere "help test-multi", 50*2238dcc3SJonas Devlieghere "Help works for top-level multi", 51*2238dcc3SJonas Devlieghere substrs=["A test container command"], 52*2238dcc3SJonas Devlieghere ) 53c5011aedSJim Ingham # Add a subcommand: 54*2238dcc3SJonas Devlieghere self.runCmd( 55*2238dcc3SJonas Devlieghere "command container add -h 'A test container sub-command' test-multi test-multi-sub" 56*2238dcc3SJonas Devlieghere ) 57c5011aedSJim Ingham # Make sure the help works: 58*2238dcc3SJonas Devlieghere self.expect( 59*2238dcc3SJonas Devlieghere "help test-multi", 60*2238dcc3SJonas Devlieghere "Help shows sub-multi", 61*2238dcc3SJonas Devlieghere substrs=[ 62*2238dcc3SJonas Devlieghere "A test container command", 63*2238dcc3SJonas Devlieghere "test-multi-sub -- A test container sub-command", 64*2238dcc3SJonas Devlieghere ], 65*2238dcc3SJonas Devlieghere ) 66*2238dcc3SJonas Devlieghere self.expect( 67*2238dcc3SJonas Devlieghere "help test-multi test-multi-sub", 68*2238dcc3SJonas Devlieghere "Help shows sub-multi", 69*2238dcc3SJonas Devlieghere substrs=["A test container sub-command"], 70*2238dcc3SJonas Devlieghere ) 71c5011aedSJim Ingham 72c5011aedSJim Ingham # Now add a script based command to the container command: 73c5011aedSJim Ingham self.runCmd("command script import welcome.py") 74*2238dcc3SJonas Devlieghere self.runCmd( 75*2238dcc3SJonas Devlieghere "command script add -c welcome.WelcomeCommand test-multi test-multi-sub welcome" 76*2238dcc3SJonas Devlieghere ) 77c5011aedSJim Ingham # Make sure the help still works: 78*2238dcc3SJonas Devlieghere self.expect( 79*2238dcc3SJonas Devlieghere "help test-multi test-multi-sub", 80*2238dcc3SJonas Devlieghere "Listing subcommands works", 81*2238dcc3SJonas Devlieghere substrs=[ 82*2238dcc3SJonas Devlieghere "A test container sub-command", 83*2238dcc3SJonas Devlieghere "welcome -- Just a docstring for Welcome", 84*2238dcc3SJonas Devlieghere ], 85*2238dcc3SJonas Devlieghere ) 86*2238dcc3SJonas Devlieghere self.expect( 87*2238dcc3SJonas Devlieghere "help test-multi test-multi-sub welcome", 88*2238dcc3SJonas Devlieghere "Subcommand help works", 89*2238dcc3SJonas Devlieghere substrs=["Just a docstring for Welcome"], 90*2238dcc3SJonas Devlieghere ) 91c5011aedSJim Ingham # And make sure it actually works: 92*2238dcc3SJonas Devlieghere self.expect( 93*2238dcc3SJonas Devlieghere "test-multi test-multi-sub welcome friend", 94*2238dcc3SJonas Devlieghere "Test command works", 95*2238dcc3SJonas Devlieghere substrs=["Hello friend, welcome to LLDB"], 96*2238dcc3SJonas Devlieghere ) 97c5011aedSJim Ingham 98d518ed42SJim Ingham # Make sure we can make an alias to this: 99*2238dcc3SJonas Devlieghere self.runCmd( 100*2238dcc3SJonas Devlieghere "command alias my-welcome test-multi test-multi-sub welcome", 101*2238dcc3SJonas Devlieghere "We can make an alias to multi-word", 102*2238dcc3SJonas Devlieghere ) 103*2238dcc3SJonas Devlieghere self.expect( 104*2238dcc3SJonas Devlieghere "my-welcome friend", 105*2238dcc3SJonas Devlieghere "Test command works", 106*2238dcc3SJonas Devlieghere substrs=["Hello friend, welcome to LLDB"], 107*2238dcc3SJonas Devlieghere ) 108d518ed42SJim Ingham self.runCmd("command unalias my-welcome") 109d518ed42SJim Ingham 1101f7b58f2SJim Ingham # Make sure overwriting works on the leaf command. First using the 1111f7b58f2SJim Ingham # explicit option so we should not be able to remove extant commands by default: 1121f7b58f2SJim Ingham 113*2238dcc3SJonas Devlieghere self.expect( 114*2238dcc3SJonas Devlieghere "command script add -c welcome.WelcomeCommand2 test-multi test-multi-sub welcome", 115c5011aedSJim Ingham "overwrite command w/o -o", 116*2238dcc3SJonas Devlieghere substrs=["cannot add command: sub-command already exists"], 117*2238dcc3SJonas Devlieghere error=True, 118*2238dcc3SJonas Devlieghere ) 119c5011aedSJim Ingham # But we can with the -o option: 120*2238dcc3SJonas Devlieghere self.runCmd( 121*2238dcc3SJonas Devlieghere "command script add -c welcome.WelcomeCommand2 -o test-multi test-multi-sub welcome" 122*2238dcc3SJonas Devlieghere ) 123c5011aedSJim Ingham # Make sure we really did overwrite: 124*2238dcc3SJonas Devlieghere self.expect( 125*2238dcc3SJonas Devlieghere "test-multi test-multi-sub welcome friend", 126*2238dcc3SJonas Devlieghere "Used the new command class", 127*2238dcc3SJonas Devlieghere substrs=["Hello friend, welcome again to LLDB"], 128*2238dcc3SJonas Devlieghere ) 129*2238dcc3SJonas Devlieghere self.expect( 130*2238dcc3SJonas Devlieghere "apropos welcome", 131*2238dcc3SJonas Devlieghere "welcome should show up in apropos", 132*2238dcc3SJonas Devlieghere substrs=["A docstring for the second Welcome"], 133*2238dcc3SJonas Devlieghere ) 134*2238dcc3SJonas Devlieghere self.expect( 135*2238dcc3SJonas Devlieghere "help test-multi test-multi-sub welcome", 136*2238dcc3SJonas Devlieghere "welcome should show up in help", 137*2238dcc3SJonas Devlieghere substrs=["A docstring for the second Welcome"], 138*2238dcc3SJonas Devlieghere ) 1398c3a6fe3SJim Ingham self.expect("help", "test-multi should show up in help", substrs=["test-multi"]) 140c5011aedSJim Ingham 1411f7b58f2SJim Ingham # Now switch the default and make sure we can now delete w/o the overwrite option: 1421f7b58f2SJim Ingham self.runCmd("settings set interpreter.require-overwrite 0") 143*2238dcc3SJonas Devlieghere self.runCmd( 144*2238dcc3SJonas Devlieghere "command script add -c welcome.WelcomeCommand test-multi test-multi-sub welcome" 145*2238dcc3SJonas Devlieghere ) 1461f7b58f2SJim Ingham # Make sure we really did overwrite: 147*2238dcc3SJonas Devlieghere self.expect( 148*2238dcc3SJonas Devlieghere "test-multi test-multi-sub welcome friend", 149*2238dcc3SJonas Devlieghere "Used the new command class", 150*2238dcc3SJonas Devlieghere substrs=["Hello friend, welcome to LLDB"], 151*2238dcc3SJonas Devlieghere ) 1521f7b58f2SJim Ingham 153c5011aedSJim Ingham # Make sure we give good errors when the input is wrong: 154*2238dcc3SJonas Devlieghere self.expect( 155*2238dcc3SJonas Devlieghere "command script delete test-mult test-multi-sub welcome", 156*2238dcc3SJonas Devlieghere "Delete script command - wrong first path component", 157*2238dcc3SJonas Devlieghere substrs=["'test-mult' not found"], 158*2238dcc3SJonas Devlieghere error=True, 159*2238dcc3SJonas Devlieghere ) 160c5011aedSJim Ingham 161*2238dcc3SJonas Devlieghere self.expect( 162*2238dcc3SJonas Devlieghere "command script delete test-multi test-multi-su welcome", 163*2238dcc3SJonas Devlieghere "Delete script command - wrong second path component", 164*2238dcc3SJonas Devlieghere substrs=["'test-multi-su' not found"], 165*2238dcc3SJonas Devlieghere error=True, 166*2238dcc3SJonas Devlieghere ) 167c5011aedSJim Ingham self.check_command_tree_exists() 168c5011aedSJim Ingham 169*2238dcc3SJonas Devlieghere self.expect( 170*2238dcc3SJonas Devlieghere "command script delete test-multi test-multi-sub welcom", 171*2238dcc3SJonas Devlieghere "Delete script command - wrong leaf component", 172*2238dcc3SJonas Devlieghere substrs=["'welcom' not found"], 173*2238dcc3SJonas Devlieghere error=True, 174*2238dcc3SJonas Devlieghere ) 175c5011aedSJim Ingham self.check_command_tree_exists() 176c5011aedSJim Ingham 177*2238dcc3SJonas Devlieghere self.expect( 178*2238dcc3SJonas Devlieghere "command script delete test-multi test-multi-sub", 179*2238dcc3SJonas Devlieghere "Delete script command - no leaf component", 180*2238dcc3SJonas Devlieghere substrs=["subcommand 'test-multi-sub' is not a user command"], 181*2238dcc3SJonas Devlieghere error=True, 182*2238dcc3SJonas Devlieghere ) 183c5011aedSJim Ingham self.check_command_tree_exists() 184c5011aedSJim Ingham 185c5011aedSJim Ingham # You can't use command script delete to delete container commands: 186*2238dcc3SJonas Devlieghere self.expect( 187*2238dcc3SJonas Devlieghere "command script delete test-multi", 188*2238dcc3SJonas Devlieghere "Delete script - can't delete container", 189*2238dcc3SJonas Devlieghere substrs=["command 'test-multi' is a multi-word command."], 190*2238dcc3SJonas Devlieghere error=True, 191*2238dcc3SJonas Devlieghere ) 192*2238dcc3SJonas Devlieghere self.expect( 193*2238dcc3SJonas Devlieghere "command script delete test-multi test-multi-sub", 194*2238dcc3SJonas Devlieghere "Delete script - can't delete container", 195*2238dcc3SJonas Devlieghere substrs=["subcommand 'test-multi-sub' is not a user command"], 196*2238dcc3SJonas Devlieghere error=True, 197*2238dcc3SJonas Devlieghere ) 198c5011aedSJim Ingham 199c5011aedSJim Ingham # You can't use command container delete to delete scripted commands: 200*2238dcc3SJonas Devlieghere self.expect( 201*2238dcc3SJonas Devlieghere "command container delete test-multi test-multi-sub welcome", 202*2238dcc3SJonas Devlieghere "command container can't delete user commands", 203*2238dcc3SJonas Devlieghere substrs=["subcommand 'welcome' is not a container command"], 204*2238dcc3SJonas Devlieghere error=True, 205*2238dcc3SJonas Devlieghere ) 206c5011aedSJim Ingham 207c5011aedSJim Ingham # Also make sure you can't alias on top of container commands: 208*2238dcc3SJonas Devlieghere self.expect( 209*2238dcc3SJonas Devlieghere "command alias test-multi process launch", 210*2238dcc3SJonas Devlieghere "Tried to alias on top of a container command", 211*2238dcc3SJonas Devlieghere substrs=[ 212*2238dcc3SJonas Devlieghere "'test-multi' is a user container command and cannot be overwritten." 213*2238dcc3SJonas Devlieghere ], 214*2238dcc3SJonas Devlieghere error=True, 215*2238dcc3SJonas Devlieghere ) 216c5011aedSJim Ingham self.check_command_tree_exists() 217c5011aedSJim Ingham 218c5011aedSJim Ingham # Also assert that we can't delete builtin commands: 219*2238dcc3SJonas Devlieghere self.expect( 220*2238dcc3SJonas Devlieghere "command script delete process launch", 221*2238dcc3SJonas Devlieghere "Delete builtin command fails", 222*2238dcc3SJonas Devlieghere substrs=["command 'process' is not a user command"], 223*2238dcc3SJonas Devlieghere error=True, 224*2238dcc3SJonas Devlieghere ) 225c5011aedSJim Ingham # Now let's do the version that works 226*2238dcc3SJonas Devlieghere self.expect( 227*2238dcc3SJonas Devlieghere "command script delete test-multi test-multi-sub welcome", 228*2238dcc3SJonas Devlieghere "Delete script command by path", 229*2238dcc3SJonas Devlieghere substrs=["Deleted command: test-multi test-multi-sub welcome"], 230*2238dcc3SJonas Devlieghere ) 231c5011aedSJim Ingham 232c5011aedSJim Ingham # Now overwrite the sub-command, it should end up empty: 233*2238dcc3SJonas Devlieghere self.runCmd( 234*2238dcc3SJonas Devlieghere "command container add -h 'A different help string' -o test-multi test-multi-sub" 235*2238dcc3SJonas Devlieghere ) 236c5011aedSJim Ingham # welcome should be gone: 237*2238dcc3SJonas Devlieghere self.expect( 238*2238dcc3SJonas Devlieghere "test-multi test-multi-sub welcome friend", 239*2238dcc3SJonas Devlieghere "did remove subcommand", 240*2238dcc3SJonas Devlieghere substrs=["'test-multi-sub' does not have any subcommands."], 241*2238dcc3SJonas Devlieghere error=True, 242*2238dcc3SJonas Devlieghere ) 243c5011aedSJim Ingham # We should have the new help: 244*2238dcc3SJonas Devlieghere self.expect( 245*2238dcc3SJonas Devlieghere "help test-multi test-multi-sub", 246*2238dcc3SJonas Devlieghere "help changed", 247*2238dcc3SJonas Devlieghere substrs=["A different help string"], 248*2238dcc3SJonas Devlieghere ) 249c5011aedSJim Ingham 250c5011aedSJim Ingham # Now try deleting commands. 251c5011aedSJim Ingham self.runCmd("command container delete test-multi test-multi-sub") 252*2238dcc3SJonas Devlieghere self.expect( 253*2238dcc3SJonas Devlieghere "test-multi test-multi-sub", 254*2238dcc3SJonas Devlieghere "Command is not active", 255*2238dcc3SJonas Devlieghere error=True, 256*2238dcc3SJonas Devlieghere substrs=["'test-multi' does not have any subcommands"], 257*2238dcc3SJonas Devlieghere ) 258c5011aedSJim Ingham self.expect("help test-multi", matching=False, substrs=["test-multi-sub"]) 259c5011aedSJim Ingham 260c5011aedSJim Ingham # Next the root command: 261c5011aedSJim Ingham self.runCmd("command container delete test-multi") 262*2238dcc3SJonas Devlieghere self.expect( 263*2238dcc3SJonas Devlieghere "test-multi", 264*2238dcc3SJonas Devlieghere "Root command gone", 265*2238dcc3SJonas Devlieghere substrs=["'test-multi' is not a valid command."], 266*2238dcc3SJonas Devlieghere error=True, 267*2238dcc3SJonas Devlieghere ) 268