| 1 |
""" |
| 2 |
Test script for the 'cmd' module |
| 3 |
Original by Michael Schneider |
| 4 |
""" |
| 5 |
|
| 6 |
|
| 7 |
import cmd |
| 8 |
import sys |
| 9 |
import doctest |
| 10 |
import unittest |
| 11 |
import io |
| 12 |
import textwrap |
| 13 |
from test import support |
| 14 |
from test.support.import_helper import import_module |
| 15 |
from test.support.pty_helper import run_pty |
| 16 |
|
| 17 |
class samplecmdclass(cmd.Cmd): |
| 18 |
""" |
| 19 |
Instance the sampleclass: |
| 20 |
>>> mycmd = samplecmdclass() |
| 21 |
|
| 22 |
Test for the function parseline(): |
| 23 |
>>> mycmd.parseline("") |
| 24 |
(None, None, '') |
| 25 |
>>> mycmd.parseline("?") |
| 26 |
('help', '', 'help ') |
| 27 |
>>> mycmd.parseline("?help") |
| 28 |
('help', 'help', 'help help') |
| 29 |
>>> mycmd.parseline("!") |
| 30 |
('shell', '', 'shell ') |
| 31 |
>>> mycmd.parseline("!command") |
| 32 |
('shell', 'command', 'shell command') |
| 33 |
>>> mycmd.parseline("func") |
| 34 |
('func', '', 'func') |
| 35 |
>>> mycmd.parseline("func arg1") |
| 36 |
('func', 'arg1', 'func arg1') |
| 37 |
|
| 38 |
|
| 39 |
Test for the function onecmd(): |
| 40 |
>>> mycmd.onecmd("") |
| 41 |
>>> mycmd.onecmd("add 4 5") |
| 42 |
9 |
| 43 |
>>> mycmd.onecmd("") |
| 44 |
9 |
| 45 |
>>> mycmd.onecmd("test") |
| 46 |
*** Unknown syntax: test |
| 47 |
|
| 48 |
Test for the function emptyline(): |
| 49 |
>>> mycmd.emptyline() |
| 50 |
*** Unknown syntax: test |
| 51 |
|
| 52 |
Test for the function default(): |
| 53 |
>>> mycmd.default("default") |
| 54 |
*** Unknown syntax: default |
| 55 |
|
| 56 |
Test for the function completedefault(): |
| 57 |
>>> mycmd.completedefault() |
| 58 |
This is the completedefault method |
| 59 |
>>> mycmd.completenames("a") |
| 60 |
['add'] |
| 61 |
|
| 62 |
Test for the function completenames(): |
| 63 |
>>> mycmd.completenames("12") |
| 64 |
[] |
| 65 |
>>> mycmd.completenames("help") |
| 66 |
['help'] |
| 67 |
|
| 68 |
Test for the function complete_help(): |
| 69 |
>>> mycmd.complete_help("a") |
| 70 |
['add'] |
| 71 |
>>> mycmd.complete_help("he") |
| 72 |
['help'] |
| 73 |
>>> mycmd.complete_help("12") |
| 74 |
[] |
| 75 |
>>> sorted(mycmd.complete_help("")) |
| 76 |
['add', 'exit', 'help', 'life', 'meaning', 'shell'] |
| 77 |
|
| 78 |
Test for the function do_help(): |
| 79 |
>>> mycmd.do_help("testet") |
| 80 |
*** No help on testet |
| 81 |
>>> mycmd.do_help("add") |
| 82 |
help text for add |
| 83 |
>>> mycmd.onecmd("help add") |
| 84 |
help text for add |
| 85 |
>>> mycmd.onecmd("help meaning") # doctest: +NORMALIZE_WHITESPACE |
| 86 |
Try and be nice to people, avoid eating fat, read a good book every |
| 87 |
now and then, get some walking in, and try to live together in peace |
| 88 |
and harmony with people of all creeds and nations. |
| 89 |
>>> mycmd.do_help("") |
| 90 |
<BLANKLINE> |
| 91 |
Documented commands (type help <topic>): |
| 92 |
======================================== |
| 93 |
add help |
| 94 |
<BLANKLINE> |
| 95 |
Miscellaneous help topics: |
| 96 |
========================== |
| 97 |
life meaning |
| 98 |
<BLANKLINE> |
| 99 |
Undocumented commands: |
| 100 |
====================== |
| 101 |
exit shell |
| 102 |
<BLANKLINE> |
| 103 |
|
| 104 |
Test for the function print_topics(): |
| 105 |
>>> mycmd.print_topics("header", ["command1", "command2"], 2 ,10) |
| 106 |
header |
| 107 |
====== |
| 108 |
command1 |
| 109 |
command2 |
| 110 |
<BLANKLINE> |
| 111 |
|
| 112 |
Test for the function columnize(): |
| 113 |
>>> mycmd.columnize([str(i) for i in range(20)]) |
| 114 |
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
| 115 |
>>> mycmd.columnize([str(i) for i in range(20)], 10) |
| 116 |
0 7 14 |
| 117 |
1 8 15 |
| 118 |
2 9 16 |
| 119 |
3 10 17 |
| 120 |
4 11 18 |
| 121 |
5 12 19 |
| 122 |
6 13 |
| 123 |
|
| 124 |
This is an interactive test, put some commands in the cmdqueue attribute |
| 125 |
and let it execute |
| 126 |
This test includes the preloop(), postloop(), default(), emptyline(), |
| 127 |
parseline(), do_help() functions |
| 128 |
>>> mycmd.use_rawinput=0 |
| 129 |
|
| 130 |
>>> mycmd.cmdqueue=["add", "add 4 5", "", "help", "help add", "exit"] |
| 131 |
>>> mycmd.cmdloop() # doctest: +REPORT_NDIFF |
| 132 |
Hello from preloop |
| 133 |
*** invalid number of arguments |
| 134 |
9 |
| 135 |
9 |
| 136 |
<BLANKLINE> |
| 137 |
Documented commands (type help <topic>): |
| 138 |
======================================== |
| 139 |
add help |
| 140 |
<BLANKLINE> |
| 141 |
Miscellaneous help topics: |
| 142 |
========================== |
| 143 |
life meaning |
| 144 |
<BLANKLINE> |
| 145 |
Undocumented commands: |
| 146 |
====================== |
| 147 |
exit shell |
| 148 |
<BLANKLINE> |
| 149 |
help text for add |
| 150 |
Hello from postloop |
| 151 |
""" |
| 152 |
|
| 153 |
def preloop(self): |
| 154 |
print("Hello from preloop") |
| 155 |
|
| 156 |
def postloop(self): |
| 157 |
print("Hello from postloop") |
| 158 |
|
| 159 |
def completedefault(self, *ignored): |
| 160 |
print("This is the completedefault method") |
| 161 |
|
| 162 |
def complete_command(self): |
| 163 |
print("complete command") |
| 164 |
|
| 165 |
def do_shell(self, s): |
| 166 |
pass |
| 167 |
|
| 168 |
def do_add(self, s): |
| 169 |
l = s.split() |
| 170 |
if len(l) != 2: |
| 171 |
print("*** invalid number of arguments") |
| 172 |
return |
| 173 |
try: |
| 174 |
l = [int(i) for i in l] |
| 175 |
except ValueError: |
| 176 |
print("*** arguments should be numbers") |
| 177 |
return |
| 178 |
print(l[0]+l[1]) |
| 179 |
|
| 180 |
def help_add(self): |
| 181 |
print("help text for add") |
| 182 |
return |
| 183 |
|
| 184 |
def help_meaning(self): |
| 185 |
print("Try and be nice to people, avoid eating fat, read a " |
| 186 |
"good book every now and then, get some walking in, " |
| 187 |
"and try to live together in peace and harmony with " |
| 188 |
"people of all creeds and nations.") |
| 189 |
return |
| 190 |
|
| 191 |
def help_life(self): |
| 192 |
print("Always look on the bright side of life") |
| 193 |
return |
| 194 |
|
| 195 |
def do_exit(self, arg): |
| 196 |
return True |
| 197 |
|
| 198 |
|
| 199 |
class TestAlternateInput(unittest.TestCase): |
| 200 |
|
| 201 |
class simplecmd(cmd.Cmd): |
| 202 |
|
| 203 |
def do_print(self, args): |
| 204 |
print(args, file=self.stdout) |
| 205 |
|
| 206 |
def do_EOF(self, args): |
| 207 |
return True |
| 208 |
|
| 209 |
|
| 210 |
class simplecmd2(simplecmd): |
| 211 |
|
| 212 |
def do_EOF(self, args): |
| 213 |
print('*** Unknown syntax: EOF', file=self.stdout) |
| 214 |
return True |
| 215 |
|
| 216 |
|
| 217 |
def test_file_with_missing_final_nl(self): |
| 218 |
input = io.StringIO("print test\nprint test2") |
| 219 |
output = io.StringIO() |
| 220 |
cmd = self.simplecmd(stdin=input, stdout=output) |
| 221 |
cmd.use_rawinput = False |
| 222 |
cmd.cmdloop() |
| 223 |
self.assertMultiLineEqual(output.getvalue(), |
| 224 |
("(Cmd) test\n" |
| 225 |
"(Cmd) test2\n" |
| 226 |
"(Cmd) ")) |
| 227 |
|
| 228 |
|
| 229 |
def test_input_reset_at_EOF(self): |
| 230 |
input = io.StringIO("print test\nprint test2") |
| 231 |
output = io.StringIO() |
| 232 |
cmd = self.simplecmd2(stdin=input, stdout=output) |
| 233 |
cmd.use_rawinput = False |
| 234 |
cmd.cmdloop() |
| 235 |
self.assertMultiLineEqual(output.getvalue(), |
| 236 |
("(Cmd) test\n" |
| 237 |
"(Cmd) test2\n" |
| 238 |
"(Cmd) *** Unknown syntax: EOF\n")) |
| 239 |
input = io.StringIO("print \n\n") |
| 240 |
output = io.StringIO() |
| 241 |
cmd.stdin = input |
| 242 |
cmd.stdout = output |
| 243 |
cmd.cmdloop() |
| 244 |
self.assertMultiLineEqual(output.getvalue(), |
| 245 |
("(Cmd) \n" |
| 246 |
"(Cmd) \n" |
| 247 |
"(Cmd) *** Unknown syntax: EOF\n")) |
| 248 |
|
| 249 |
|
| 250 |
class CmdPrintExceptionClass(cmd.Cmd): |
| 251 |
""" |
| 252 |
GH-80731 |
| 253 |
cmd.Cmd should print the correct exception in default() |
| 254 |
>>> mycmd = CmdPrintExceptionClass() |
| 255 |
>>> try: |
| 256 |
... raise ValueError("test") |
| 257 |
... except ValueError: |
| 258 |
... mycmd.onecmd("not important") |
| 259 |
(<class 'ValueError'>, ValueError('test')) |
| 260 |
""" |
| 261 |
|
| 262 |
def default(self, line): |
| 263 |
print(sys.exc_info()[:2]) |
| 264 |
|
| 265 |
|
| 266 |
@support.requires_subprocess() |
| 267 |
class CmdTestReadline(unittest.TestCase): |
| 268 |
def setUpClass(): |
| 269 |
# Ensure that the readline module is loaded |
| 270 |
# If this fails, the test is skipped because SkipTest will be raised |
| 271 |
readline = import_module('readline') |
| 272 |
|
| 273 |
def test_basic_completion(self): |
| 274 |
script = textwrap.dedent(""" |
| 275 |
import cmd |
| 276 |
class simplecmd(cmd.Cmd): |
| 277 |
def do_tab_completion_test(self, args): |
| 278 |
print('tab completion success') |
| 279 |
return True |
| 280 |
|
| 281 |
simplecmd().cmdloop() |
| 282 |
""") |
| 283 |
|
| 284 |
# 't' and complete 'ab_completion_test' to 'tab_completion_test' |
| 285 |
input = b"t\t\n" |
| 286 |
|
| 287 |
output = run_pty(script, input) |
| 288 |
|
| 289 |
self.assertIn(b'ab_completion_test', output) |
| 290 |
self.assertIn(b'tab completion success', output) |
| 291 |
|
| 292 |
def load_tests(loader, tests, pattern): |
| 293 |
tests.addTest(doctest.DocTestSuite()) |
| 294 |
return tests |
| 295 |
|
| 296 |
|
| 297 |
if __name__ == "__main__": |
| 298 |
if "-i" in sys.argv: |
| 299 |
samplecmdclass().cmdloop() |
| 300 |
else: |
| 301 |
unittest.main() |