| 1 |
"""Test history functions of cmd2""" |
| 2 |
|
| 3 |
import contextlib |
| 4 |
import os |
| 5 |
import tempfile |
| 6 |
from unittest import ( |
| 7 |
mock, |
| 8 |
) |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
import cmd2 |
| 13 |
|
| 14 |
from .conftest import ( |
| 15 |
normalize, |
| 16 |
run_cmd, |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
def verify_hi_last_result(app: cmd2.Cmd, expected_length: int) -> None: |
| 21 |
"""Verifies app.last_result when it contains a dictionary of HistoryItems""" |
| 22 |
assert len(app.last_result) == expected_length |
| 23 |
|
| 24 |
|
| 25 |
for key in app.last_result: |
| 26 |
|
| 27 |
assert app.last_result[key] == app.history[key - 1] |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
def test_pt_add_history_item() -> None: |
| 34 |
from prompt_toolkit import PromptSession |
| 35 |
from prompt_toolkit.history import InMemoryHistory |
| 36 |
from prompt_toolkit.input import DummyInput |
| 37 |
from prompt_toolkit.output import DummyOutput |
| 38 |
|
| 39 |
|
| 40 |
history = InMemoryHistory() |
| 41 |
history.append_string("command one") |
| 42 |
history.append_string("command two") |
| 43 |
assert "command one" in history.get_strings() |
| 44 |
assert len(history.get_strings()) == 2 |
| 45 |
|
| 46 |
|
| 47 |
session = PromptSession(history=history, input=DummyInput(), output=DummyOutput()) |
| 48 |
|
| 49 |
session.history.get_strings().append("new command") |
| 50 |
assert "new command" not in session.history.get_strings() |
| 51 |
assert len(history.get_strings()) == 2 |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
@pytest.fixture |
| 58 |
def hist(): |
| 59 |
from cmd2.cmd2 import ( |
| 60 |
History, |
| 61 |
HistoryItem, |
| 62 |
) |
| 63 |
from cmd2.parsing import ( |
| 64 |
Statement, |
| 65 |
) |
| 66 |
|
| 67 |
return History( |
| 68 |
[ |
| 69 |
HistoryItem(Statement("", raw="first")), |
| 70 |
HistoryItem(Statement("", raw="second")), |
| 71 |
HistoryItem(Statement("", raw="third")), |
| 72 |
HistoryItem(Statement("", raw="fourth")), |
| 73 |
] |
| 74 |
) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
hist_json = ( |
| 79 |
"{\n" |
| 80 |
' "history_version": "4.0.0",\n' |
| 81 |
' "history_items": [\n' |
| 82 |
" {\n" |
| 83 |
' "statement": {\n' |
| 84 |
' "args": "",\n' |
| 85 |
' "raw": "first",\n' |
| 86 |
' "command": "",\n' |
| 87 |
' "multiline_command": false,\n' |
| 88 |
' "terminator": "",\n' |
| 89 |
' "suffix": "",\n' |
| 90 |
' "redirector": "",\n' |
| 91 |
' "redirect_to": ""\n' |
| 92 |
" }\n" |
| 93 |
" },\n" |
| 94 |
" {\n" |
| 95 |
' "statement": {\n' |
| 96 |
' "args": "",\n' |
| 97 |
' "raw": "second",\n' |
| 98 |
' "command": "",\n' |
| 99 |
' "multiline_command": false,\n' |
| 100 |
' "terminator": "",\n' |
| 101 |
' "suffix": "",\n' |
| 102 |
' "redirector": "",\n' |
| 103 |
' "redirect_to": ""\n' |
| 104 |
" }\n" |
| 105 |
" },\n" |
| 106 |
" {\n" |
| 107 |
' "statement": {\n' |
| 108 |
' "args": "",\n' |
| 109 |
' "raw": "third",\n' |
| 110 |
' "command": "",\n' |
| 111 |
' "multiline_command": false,\n' |
| 112 |
' "terminator": "",\n' |
| 113 |
' "suffix": "",\n' |
| 114 |
' "redirector": "",\n' |
| 115 |
' "redirect_to": ""\n' |
| 116 |
" }\n" |
| 117 |
" },\n" |
| 118 |
" {\n" |
| 119 |
' "statement": {\n' |
| 120 |
' "args": "",\n' |
| 121 |
' "raw": "fourth",\n' |
| 122 |
' "command": "",\n' |
| 123 |
' "multiline_command": false,\n' |
| 124 |
' "terminator": "",\n' |
| 125 |
' "suffix": "",\n' |
| 126 |
' "redirector": "",\n' |
| 127 |
' "redirect_to": ""\n' |
| 128 |
" }\n" |
| 129 |
" }\n" |
| 130 |
" ]\n" |
| 131 |
"}" |
| 132 |
) |
| 133 |
|
| 134 |
|
| 135 |
@pytest.fixture |
| 136 |
def persisted_hist(): |
| 137 |
from cmd2.cmd2 import ( |
| 138 |
History, |
| 139 |
HistoryItem, |
| 140 |
) |
| 141 |
from cmd2.parsing import ( |
| 142 |
Statement, |
| 143 |
) |
| 144 |
|
| 145 |
h = History( |
| 146 |
[ |
| 147 |
HistoryItem(Statement("", raw="first")), |
| 148 |
HistoryItem(Statement("", raw="second")), |
| 149 |
HistoryItem(Statement("", raw="third")), |
| 150 |
HistoryItem(Statement("", raw="fourth")), |
| 151 |
] |
| 152 |
) |
| 153 |
h.start_session() |
| 154 |
h.append(Statement("", raw="fifth")) |
| 155 |
h.append(Statement("", raw="sixth")) |
| 156 |
return h |
| 157 |
|
| 158 |
|
| 159 |
def test_history_class_span(hist) -> None: |
| 160 |
span = hist.span("2..") |
| 161 |
assert len(span) == 3 |
| 162 |
assert span[2].statement.raw == "second" |
| 163 |
assert span[3].statement.raw == "third" |
| 164 |
assert span[4].statement.raw == "fourth" |
| 165 |
|
| 166 |
span = hist.span("2:") |
| 167 |
assert len(span) == 3 |
| 168 |
assert span[2].statement.raw == "second" |
| 169 |
assert span[3].statement.raw == "third" |
| 170 |
assert span[4].statement.raw == "fourth" |
| 171 |
|
| 172 |
span = hist.span("-2..") |
| 173 |
assert len(span) == 2 |
| 174 |
assert span[3].statement.raw == "third" |
| 175 |
assert span[4].statement.raw == "fourth" |
| 176 |
|
| 177 |
span = hist.span("-2:") |
| 178 |
assert len(span) == 2 |
| 179 |
assert span[3].statement.raw == "third" |
| 180 |
assert span[4].statement.raw == "fourth" |
| 181 |
|
| 182 |
span = hist.span("1..3") |
| 183 |
assert len(span) == 3 |
| 184 |
assert span[1].statement.raw == "first" |
| 185 |
assert span[2].statement.raw == "second" |
| 186 |
assert span[3].statement.raw == "third" |
| 187 |
|
| 188 |
span = hist.span("1:3") |
| 189 |
assert len(span) == 3 |
| 190 |
assert span[1].statement.raw == "first" |
| 191 |
assert span[2].statement.raw == "second" |
| 192 |
assert span[3].statement.raw == "third" |
| 193 |
|
| 194 |
span = hist.span("2:-1") |
| 195 |
assert len(span) == 3 |
| 196 |
assert span[2].statement.raw == "second" |
| 197 |
assert span[3].statement.raw == "third" |
| 198 |
assert span[4].statement.raw == "fourth" |
| 199 |
|
| 200 |
span = hist.span("-3:4") |
| 201 |
assert len(span) == 3 |
| 202 |
assert span[2].statement.raw == "second" |
| 203 |
assert span[3].statement.raw == "third" |
| 204 |
assert span[4].statement.raw == "fourth" |
| 205 |
|
| 206 |
span = hist.span("-4:-2") |
| 207 |
assert len(span) == 3 |
| 208 |
assert span[1].statement.raw == "first" |
| 209 |
assert span[2].statement.raw == "second" |
| 210 |
assert span[3].statement.raw == "third" |
| 211 |
|
| 212 |
span = hist.span(":-2") |
| 213 |
assert len(span) == 3 |
| 214 |
assert span[1].statement.raw == "first" |
| 215 |
assert span[2].statement.raw == "second" |
| 216 |
assert span[3].statement.raw == "third" |
| 217 |
|
| 218 |
span = hist.span("..-2") |
| 219 |
assert len(span) == 3 |
| 220 |
assert span[1].statement.raw == "first" |
| 221 |
assert span[2].statement.raw == "second" |
| 222 |
assert span[3].statement.raw == "third" |
| 223 |
|
| 224 |
value_errors = ["fred", "fred:joe", "2", "-2", "a..b", "2 ..", "1 : 3", "1:0", "0:3"] |
| 225 |
expected_err = "History indices must be positive or negative integers, and may not be zero." |
| 226 |
for tryit in value_errors: |
| 227 |
with pytest.raises(ValueError, match=expected_err): |
| 228 |
hist.span(tryit) |
| 229 |
|
| 230 |
|
| 231 |
def test_persisted_history_span(persisted_hist) -> None: |
| 232 |
span = persisted_hist.span("2..") |
| 233 |
assert len(span) == 5 |
| 234 |
assert span[2].statement.raw == "second" |
| 235 |
assert span[3].statement.raw == "third" |
| 236 |
assert span[4].statement.raw == "fourth" |
| 237 |
assert span[5].statement.raw == "fifth" |
| 238 |
assert span[6].statement.raw == "sixth" |
| 239 |
|
| 240 |
span = persisted_hist.span("-2..") |
| 241 |
assert len(span) == 2 |
| 242 |
assert span[5].statement.raw == "fifth" |
| 243 |
assert span[6].statement.raw == "sixth" |
| 244 |
|
| 245 |
span = persisted_hist.span("1..3") |
| 246 |
assert len(span) == 3 |
| 247 |
assert span[1].statement.raw == "first" |
| 248 |
assert span[2].statement.raw == "second" |
| 249 |
assert span[3].statement.raw == "third" |
| 250 |
|
| 251 |
span = persisted_hist.span("2:-1") |
| 252 |
assert len(span) == 5 |
| 253 |
assert span[2].statement.raw == "second" |
| 254 |
assert span[3].statement.raw == "third" |
| 255 |
assert span[4].statement.raw == "fourth" |
| 256 |
assert span[5].statement.raw == "fifth" |
| 257 |
assert span[6].statement.raw == "sixth" |
| 258 |
|
| 259 |
span = persisted_hist.span("-3:4") |
| 260 |
assert len(span) == 1 |
| 261 |
assert span[4].statement.raw == "fourth" |
| 262 |
|
| 263 |
span = persisted_hist.span(":-2", include_persisted=True) |
| 264 |
assert len(span) == 5 |
| 265 |
assert span[1].statement.raw == "first" |
| 266 |
assert span[2].statement.raw == "second" |
| 267 |
assert span[3].statement.raw == "third" |
| 268 |
assert span[4].statement.raw == "fourth" |
| 269 |
assert span[5].statement.raw == "fifth" |
| 270 |
|
| 271 |
span = persisted_hist.span(":-2", include_persisted=False) |
| 272 |
assert len(span) == 1 |
| 273 |
assert span[5].statement.raw == "fifth" |
| 274 |
|
| 275 |
value_errors = ["fred", "fred:joe", "2", "-2", "a..b", "2 ..", "1 : 3", "1:0", "0:3"] |
| 276 |
expected_err = "History indices must be positive or negative integers, and may not be zero." |
| 277 |
for tryit in value_errors: |
| 278 |
with pytest.raises(ValueError, match=expected_err): |
| 279 |
persisted_hist.span(tryit) |
| 280 |
|
| 281 |
|
| 282 |
def test_history_class_get(hist) -> None: |
| 283 |
assert hist.get(1).statement.raw == "first" |
| 284 |
assert hist.get(3).statement.raw == "third" |
| 285 |
assert hist.get(-2) == hist[-2] |
| 286 |
assert hist.get(-1).statement.raw == "fourth" |
| 287 |
|
| 288 |
with pytest.raises(IndexError): |
| 289 |
hist.get(0) |
| 290 |
|
| 291 |
with pytest.raises(IndexError): |
| 292 |
hist.get(5) |
| 293 |
|
| 294 |
|
| 295 |
def test_history_str_search(hist) -> None: |
| 296 |
items = hist.str_search("ir") |
| 297 |
assert len(items) == 2 |
| 298 |
assert items[1].statement.raw == "first" |
| 299 |
assert items[3].statement.raw == "third" |
| 300 |
|
| 301 |
items = hist.str_search("rth") |
| 302 |
assert len(items) == 1 |
| 303 |
assert items[4].statement.raw == "fourth" |
| 304 |
|
| 305 |
|
| 306 |
def test_history_regex_search(hist) -> None: |
| 307 |
items = hist.regex_search("/i.*d/") |
| 308 |
assert len(items) == 1 |
| 309 |
assert items[3].statement.raw == "third" |
| 310 |
|
| 311 |
items = hist.regex_search("s[a-z]+ond") |
| 312 |
assert len(items) == 1 |
| 313 |
assert items[2].statement.raw == "second" |
| 314 |
|
| 315 |
|
| 316 |
def test_history_max_length_zero(hist) -> None: |
| 317 |
hist.truncate(0) |
| 318 |
assert len(hist) == 0 |
| 319 |
|
| 320 |
|
| 321 |
def test_history_max_length_negative(hist) -> None: |
| 322 |
hist.truncate(-1) |
| 323 |
assert len(hist) == 0 |
| 324 |
|
| 325 |
|
| 326 |
def test_history_max_length(hist) -> None: |
| 327 |
hist.truncate(2) |
| 328 |
assert len(hist) == 2 |
| 329 |
assert hist.get(1).statement.raw == "third" |
| 330 |
assert hist.get(2).statement.raw == "fourth" |
| 331 |
|
| 332 |
|
| 333 |
def test_history_to_json(hist) -> None: |
| 334 |
assert hist_json == hist.to_json() |
| 335 |
|
| 336 |
|
| 337 |
def test_history_from_json(hist) -> None: |
| 338 |
import json |
| 339 |
|
| 340 |
from cmd2.history import ( |
| 341 |
History, |
| 342 |
) |
| 343 |
|
| 344 |
assert hist.from_json(hist_json) == hist |
| 345 |
|
| 346 |
|
| 347 |
with pytest.raises(json.JSONDecodeError): |
| 348 |
hist.from_json("") |
| 349 |
|
| 350 |
|
| 351 |
with pytest.raises(KeyError): |
| 352 |
hist.from_json("{}") |
| 353 |
|
| 354 |
|
| 355 |
backed_up_ver = History._history_version |
| 356 |
History._history_version = "BAD_VERSION" |
| 357 |
invalid_ver_json = hist.to_json() |
| 358 |
History._history_version = backed_up_ver |
| 359 |
|
| 360 |
expected_err = f"Unsupported history file version: BAD_VERSION. This application uses version {History._history_version}." |
| 361 |
with pytest.raises(ValueError, match=expected_err): |
| 362 |
hist.from_json(invalid_ver_json) |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
@pytest.fixture |
| 369 |
def histitem(): |
| 370 |
from cmd2.history import ( |
| 371 |
HistoryItem, |
| 372 |
) |
| 373 |
from cmd2.parsing import ( |
| 374 |
Statement, |
| 375 |
) |
| 376 |
|
| 377 |
statement = Statement( |
| 378 |
"history", |
| 379 |
raw="help history", |
| 380 |
command="help", |
| 381 |
) |
| 382 |
return HistoryItem(statement) |
| 383 |
|
| 384 |
|
| 385 |
@pytest.fixture |
| 386 |
def parser(): |
| 387 |
from cmd2.parsing import ( |
| 388 |
StatementParser, |
| 389 |
) |
| 390 |
|
| 391 |
return StatementParser( |
| 392 |
terminators=[";", "&"], |
| 393 |
multiline_commands=["multiline"], |
| 394 |
aliases={ |
| 395 |
"helpalias": "help", |
| 396 |
"42": "theanswer", |
| 397 |
"l": "!ls -al", |
| 398 |
"anothermultiline": "multiline", |
| 399 |
"fake": "run_pyscript", |
| 400 |
}, |
| 401 |
shortcuts={"?": "help", "!": "shell"}, |
| 402 |
) |
| 403 |
|
| 404 |
|
| 405 |
def test_multiline_histitem(parser) -> None: |
| 406 |
from cmd2.history import ( |
| 407 |
History, |
| 408 |
) |
| 409 |
|
| 410 |
line = "multiline foo\nbar\n\n" |
| 411 |
statement = parser.parse(line) |
| 412 |
history = History() |
| 413 |
history.append(statement) |
| 414 |
assert len(history) == 1 |
| 415 |
hist_item = history[0] |
| 416 |
assert hist_item.raw == line |
| 417 |
pr_lines = hist_item.pr(1).splitlines() |
| 418 |
assert pr_lines[0].endswith("multiline foo bar") |
| 419 |
|
| 420 |
|
| 421 |
def test_multiline_with_quotes_histitem(parser) -> None: |
| 422 |
|
| 423 |
from cmd2.history import ( |
| 424 |
History, |
| 425 |
) |
| 426 |
|
| 427 |
line = 'Look, "There are newlines\n and spaces \n "\n in\nquotes.\n;\n' |
| 428 |
statement = parser.parse(line) |
| 429 |
history = History() |
| 430 |
history.append(statement) |
| 431 |
assert len(history) == 1 |
| 432 |
hist_item = history[0] |
| 433 |
assert hist_item.raw == line |
| 434 |
|
| 435 |
|
| 436 |
pr_lines = hist_item.pr(1).splitlines() |
| 437 |
assert pr_lines[0].endswith('Look, "There are newlines') |
| 438 |
assert pr_lines[1] == " and spaces " |
| 439 |
assert pr_lines[2] == ' " in quotes.;' |
| 440 |
|
| 441 |
|
| 442 |
def test_multiline_histitem_verbose(parser) -> None: |
| 443 |
from cmd2.history import ( |
| 444 |
History, |
| 445 |
) |
| 446 |
|
| 447 |
line = "multiline foo\nbar\n\n" |
| 448 |
statement = parser.parse(line) |
| 449 |
history = History() |
| 450 |
history.append(statement) |
| 451 |
assert len(history) == 1 |
| 452 |
hist_item = history[0] |
| 453 |
assert hist_item.raw == line |
| 454 |
pr_lines = hist_item.pr(1, verbose=True).splitlines() |
| 455 |
assert pr_lines[0].endswith("multiline foo") |
| 456 |
assert pr_lines[1] == "bar" |
| 457 |
|
| 458 |
|
| 459 |
def test_single_line_format_blank(parser) -> None: |
| 460 |
from cmd2.history import ( |
| 461 |
single_line_format, |
| 462 |
) |
| 463 |
|
| 464 |
line = "" |
| 465 |
statement = parser.parse(line) |
| 466 |
assert single_line_format(statement) == line |
| 467 |
|
| 468 |
|
| 469 |
def test_history_item_instantiate() -> None: |
| 470 |
from cmd2.history import ( |
| 471 |
HistoryItem, |
| 472 |
) |
| 473 |
from cmd2.parsing import ( |
| 474 |
Statement, |
| 475 |
) |
| 476 |
|
| 477 |
Statement( |
| 478 |
"history", |
| 479 |
raw="help history", |
| 480 |
command="help", |
| 481 |
) |
| 482 |
with pytest.raises(TypeError): |
| 483 |
_ = HistoryItem() |
| 484 |
|
| 485 |
|
| 486 |
def test_history_item_properties(histitem) -> None: |
| 487 |
assert histitem.raw == "help history" |
| 488 |
assert histitem.expanded == "help history" |
| 489 |
assert str(histitem) == "help history" |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
def test_base_history(base_app) -> None: |
| 496 |
run_cmd(base_app, "help") |
| 497 |
run_cmd(base_app, "shortcuts") |
| 498 |
out, _err = run_cmd(base_app, "history") |
| 499 |
expected = normalize( |
| 500 |
""" |
| 501 |
1 help |
| 502 |
2 shortcuts |
| 503 |
""" |
| 504 |
) |
| 505 |
assert out == expected |
| 506 |
|
| 507 |
out, _err = run_cmd(base_app, "history he") |
| 508 |
expected = normalize( |
| 509 |
""" |
| 510 |
1 help |
| 511 |
""" |
| 512 |
) |
| 513 |
assert out == expected |
| 514 |
verify_hi_last_result(base_app, 1) |
| 515 |
|
| 516 |
out, _err = run_cmd(base_app, "history sh") |
| 517 |
expected = normalize( |
| 518 |
""" |
| 519 |
2 shortcuts |
| 520 |
""" |
| 521 |
) |
| 522 |
assert out == expected |
| 523 |
verify_hi_last_result(base_app, 1) |
| 524 |
|
| 525 |
|
| 526 |
def test_history_script_format(base_app) -> None: |
| 527 |
run_cmd(base_app, "help") |
| 528 |
run_cmd(base_app, "shortcuts") |
| 529 |
out, _err = run_cmd(base_app, "history -s") |
| 530 |
expected = normalize( |
| 531 |
""" |
| 532 |
help |
| 533 |
shortcuts |
| 534 |
""" |
| 535 |
) |
| 536 |
assert out == expected |
| 537 |
verify_hi_last_result(base_app, 2) |
| 538 |
|
| 539 |
|
| 540 |
def test_history_with_string_argument(base_app) -> None: |
| 541 |
run_cmd(base_app, "help") |
| 542 |
run_cmd(base_app, "shortcuts") |
| 543 |
run_cmd(base_app, "help history") |
| 544 |
out, _err = run_cmd(base_app, "history help") |
| 545 |
expected = normalize( |
| 546 |
""" |
| 547 |
1 help |
| 548 |
3 help history |
| 549 |
""" |
| 550 |
) |
| 551 |
assert out == expected |
| 552 |
verify_hi_last_result(base_app, 2) |
| 553 |
|
| 554 |
|
| 555 |
def test_history_expanded_with_string_argument(base_app) -> None: |
| 556 |
run_cmd(base_app, "alias create sc shortcuts") |
| 557 |
run_cmd(base_app, "help") |
| 558 |
run_cmd(base_app, "help history") |
| 559 |
run_cmd(base_app, "sc") |
| 560 |
out, _err = run_cmd(base_app, "history -v shortcuts") |
| 561 |
expected = normalize( |
| 562 |
""" |
| 563 |
1 alias create sc shortcuts |
| 564 |
4 sc |
| 565 |
4x shortcuts |
| 566 |
""" |
| 567 |
) |
| 568 |
assert out == expected |
| 569 |
verify_hi_last_result(base_app, 2) |
| 570 |
|
| 571 |
|
| 572 |
def test_history_expanded_with_regex_argument(base_app) -> None: |
| 573 |
run_cmd(base_app, "alias create sc shortcuts") |
| 574 |
run_cmd(base_app, "help") |
| 575 |
run_cmd(base_app, "help history") |
| 576 |
run_cmd(base_app, "sc") |
| 577 |
out, _err = run_cmd(base_app, "history -v /sh.*cuts/") |
| 578 |
expected = normalize( |
| 579 |
""" |
| 580 |
1 alias create sc shortcuts |
| 581 |
4 sc |
| 582 |
4x shortcuts |
| 583 |
""" |
| 584 |
) |
| 585 |
assert out == expected |
| 586 |
verify_hi_last_result(base_app, 2) |
| 587 |
|
| 588 |
|
| 589 |
def test_history_with_integer_argument(base_app) -> None: |
| 590 |
run_cmd(base_app, "help") |
| 591 |
run_cmd(base_app, "shortcuts") |
| 592 |
out, _err = run_cmd(base_app, "history 1") |
| 593 |
expected = normalize( |
| 594 |
""" |
| 595 |
1 help |
| 596 |
""" |
| 597 |
) |
| 598 |
assert out == expected |
| 599 |
verify_hi_last_result(base_app, 1) |
| 600 |
|
| 601 |
|
| 602 |
def test_history_with_integer_span(base_app) -> None: |
| 603 |
run_cmd(base_app, "help") |
| 604 |
run_cmd(base_app, "shortcuts") |
| 605 |
run_cmd(base_app, "help history") |
| 606 |
out, _err = run_cmd(base_app, "history 1..2") |
| 607 |
expected = normalize( |
| 608 |
""" |
| 609 |
1 help |
| 610 |
2 shortcuts |
| 611 |
""" |
| 612 |
) |
| 613 |
assert out == expected |
| 614 |
verify_hi_last_result(base_app, 2) |
| 615 |
|
| 616 |
|
| 617 |
def test_history_with_span_start(base_app) -> None: |
| 618 |
run_cmd(base_app, "help") |
| 619 |
run_cmd(base_app, "shortcuts") |
| 620 |
run_cmd(base_app, "help history") |
| 621 |
out, _err = run_cmd(base_app, "history 2:") |
| 622 |
expected = normalize( |
| 623 |
""" |
| 624 |
2 shortcuts |
| 625 |
3 help history |
| 626 |
""" |
| 627 |
) |
| 628 |
assert out == expected |
| 629 |
verify_hi_last_result(base_app, 2) |
| 630 |
|
| 631 |
|
| 632 |
def test_history_with_span_end(base_app) -> None: |
| 633 |
run_cmd(base_app, "help") |
| 634 |
run_cmd(base_app, "shortcuts") |
| 635 |
run_cmd(base_app, "help history") |
| 636 |
out, _err = run_cmd(base_app, "history :2") |
| 637 |
expected = normalize( |
| 638 |
""" |
| 639 |
1 help |
| 640 |
2 shortcuts |
| 641 |
""" |
| 642 |
) |
| 643 |
assert out == expected |
| 644 |
verify_hi_last_result(base_app, 2) |
| 645 |
|
| 646 |
|
| 647 |
def test_history_with_span_index_error(base_app) -> None: |
| 648 |
run_cmd(base_app, "help") |
| 649 |
run_cmd(base_app, "help history") |
| 650 |
run_cmd(base_app, "!ls -hal :") |
| 651 |
expected_err = "History indices must be positive or negative integers, and may not be zero." |
| 652 |
with pytest.raises(ValueError, match=expected_err): |
| 653 |
base_app.onecmd('history "hal :"') |
| 654 |
|
| 655 |
|
| 656 |
def test_history_output_file() -> None: |
| 657 |
app = cmd2.Cmd(multiline_commands=["alias"]) |
| 658 |
run_cmd(app, "help") |
| 659 |
run_cmd(app, "shortcuts") |
| 660 |
run_cmd(app, "help history") |
| 661 |
run_cmd(app, "alias create my_alias history;") |
| 662 |
|
| 663 |
fd, fname = tempfile.mkstemp(prefix="", suffix=".txt") |
| 664 |
os.close(fd) |
| 665 |
run_cmd(app, f'history -o "{fname}"') |
| 666 |
assert app.last_result is True |
| 667 |
|
| 668 |
expected = normalize("help\nshortcuts\nhelp history\nalias create my_alias history;") |
| 669 |
with open(fname) as f: |
| 670 |
content = normalize(f.read()) |
| 671 |
assert content == expected |
| 672 |
|
| 673 |
|
| 674 |
def test_history_bad_output_file(base_app) -> None: |
| 675 |
run_cmd(base_app, "help") |
| 676 |
run_cmd(base_app, "shortcuts") |
| 677 |
run_cmd(base_app, "help history") |
| 678 |
|
| 679 |
fname = os.path.join(os.path.sep, "fake", "fake", "fake") |
| 680 |
out, err = run_cmd(base_app, f'history -o "{fname}"') |
| 681 |
|
| 682 |
assert not out |
| 683 |
assert "Error saving" in err[0] |
| 684 |
assert base_app.last_result is False |
| 685 |
|
| 686 |
|
| 687 |
def test_history_edit(monkeypatch) -> None: |
| 688 |
app = cmd2.Cmd(multiline_commands=["alias"]) |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
app.editor = "fooedit" |
| 693 |
|
| 694 |
|
| 695 |
edit_mock = mock.MagicMock(name="run_editor") |
| 696 |
monkeypatch.setattr("cmd2.Cmd.run_editor", edit_mock) |
| 697 |
|
| 698 |
|
| 699 |
run_script_mock = mock.MagicMock(name="do_run_script") |
| 700 |
monkeypatch.setattr("cmd2.Cmd.do_run_script", run_script_mock) |
| 701 |
|
| 702 |
|
| 703 |
run_cmd(app, "help") |
| 704 |
run_cmd(app, "alias create my_alias history;") |
| 705 |
|
| 706 |
run_cmd(app, "history -e 1:2") |
| 707 |
|
| 708 |
|
| 709 |
edit_mock.assert_called_once() |
| 710 |
run_script_mock.assert_called_once() |
| 711 |
|
| 712 |
|
| 713 |
def test_history_run_all_commands(base_app) -> None: |
| 714 |
|
| 715 |
run_cmd(base_app, "shortcuts") |
| 716 |
out, err = run_cmd(base_app, "history -r") |
| 717 |
|
| 718 |
assert not out |
| 719 |
assert err[0].startswith("Cowardly refusing to run all") |
| 720 |
assert base_app.last_result is False |
| 721 |
|
| 722 |
|
| 723 |
def test_history_run_one_command(base_app) -> None: |
| 724 |
out1, _err1 = run_cmd(base_app, "help") |
| 725 |
out2, _err2 = run_cmd(base_app, "history -r 1") |
| 726 |
assert out1 == out2 |
| 727 |
assert base_app.last_result is True |
| 728 |
|
| 729 |
|
| 730 |
def test_history_clear(mocker, hist_file) -> None: |
| 731 |
|
| 732 |
app = cmd2.Cmd(persistent_history_file=hist_file) |
| 733 |
run_cmd(app, "help") |
| 734 |
run_cmd(app, "alias") |
| 735 |
|
| 736 |
|
| 737 |
out, err = run_cmd(app, "history") |
| 738 |
assert out |
| 739 |
verify_hi_last_result(app, 2) |
| 740 |
|
| 741 |
|
| 742 |
run_cmd(app, "history --clear") |
| 743 |
assert app.last_result is True |
| 744 |
|
| 745 |
|
| 746 |
out, err = run_cmd(app, "history") |
| 747 |
assert out == [] |
| 748 |
assert not os.path.exists(hist_file) |
| 749 |
verify_hi_last_result(app, 0) |
| 750 |
|
| 751 |
|
| 752 |
run_cmd(app, "history --clear") |
| 753 |
assert app.last_result is True |
| 754 |
|
| 755 |
|
| 756 |
mock_remove = mocker.patch("os.remove") |
| 757 |
mock_remove.side_effect = OSError |
| 758 |
|
| 759 |
out, err = run_cmd(app, "history --clear") |
| 760 |
assert out == [] |
| 761 |
assert "Error removing history file" in err[0] |
| 762 |
assert app.last_result is False |
| 763 |
|
| 764 |
|
| 765 |
def test_history_verbose_with_other_options(base_app) -> None: |
| 766 |
|
| 767 |
options_to_test = ["-r", "-e", "-o file", "-c", "-x"] |
| 768 |
for opt in options_to_test: |
| 769 |
out, _err = run_cmd(base_app, "history -v " + opt) |
| 770 |
assert "-v cannot be used with any other options" in out |
| 771 |
assert base_app.last_result is False |
| 772 |
|
| 773 |
|
| 774 |
def test_history_verbose(base_app) -> None: |
| 775 |
|
| 776 |
run_cmd(base_app, "alias create s shortcuts") |
| 777 |
run_cmd(base_app, "s") |
| 778 |
out, _err = run_cmd(base_app, "history -v") |
| 779 |
|
| 780 |
expected = normalize( |
| 781 |
""" |
| 782 |
1 alias create s shortcuts |
| 783 |
2 s |
| 784 |
2x shortcuts |
| 785 |
""" |
| 786 |
) |
| 787 |
assert out == expected |
| 788 |
verify_hi_last_result(base_app, 2) |
| 789 |
|
| 790 |
|
| 791 |
def test_history_script_with_invalid_options(base_app) -> None: |
| 792 |
|
| 793 |
options_to_test = ["-r", "-e", "-o file", "-c"] |
| 794 |
for opt in options_to_test: |
| 795 |
out, _err = run_cmd(base_app, "history -s " + opt) |
| 796 |
assert "-s and -x cannot be used with -c, -r, -e, or -o" in out |
| 797 |
assert base_app.last_result is False |
| 798 |
|
| 799 |
|
| 800 |
def test_history_script(base_app) -> None: |
| 801 |
cmds = ["alias create s shortcuts", "s"] |
| 802 |
for cmd in cmds: |
| 803 |
run_cmd(base_app, cmd) |
| 804 |
out, _err = run_cmd(base_app, "history -s") |
| 805 |
assert out == cmds |
| 806 |
verify_hi_last_result(base_app, 2) |
| 807 |
|
| 808 |
|
| 809 |
def test_history_expanded_with_invalid_options(base_app) -> None: |
| 810 |
|
| 811 |
options_to_test = ["-r", "-e", "-o file", "-c"] |
| 812 |
for opt in options_to_test: |
| 813 |
out, _err = run_cmd(base_app, "history -x " + opt) |
| 814 |
assert "-s and -x cannot be used with -c, -r, -e, or -o" in out |
| 815 |
assert base_app.last_result is False |
| 816 |
|
| 817 |
|
| 818 |
def test_history_expanded(base_app) -> None: |
| 819 |
|
| 820 |
cmds = ["alias create s shortcuts", "s"] |
| 821 |
for cmd in cmds: |
| 822 |
run_cmd(base_app, cmd) |
| 823 |
out, _err = run_cmd(base_app, "history -x") |
| 824 |
expected = [" 1 alias create s shortcuts", " 2 shortcuts"] |
| 825 |
assert out == expected |
| 826 |
verify_hi_last_result(base_app, 2) |
| 827 |
|
| 828 |
|
| 829 |
def test_history_script_expanded(base_app) -> None: |
| 830 |
|
| 831 |
cmds = ["alias create s shortcuts", "s"] |
| 832 |
for cmd in cmds: |
| 833 |
run_cmd(base_app, cmd) |
| 834 |
out, _err = run_cmd(base_app, "history -sx") |
| 835 |
expected = ["alias create s shortcuts", "shortcuts"] |
| 836 |
assert out == expected |
| 837 |
verify_hi_last_result(base_app, 2) |
| 838 |
|
| 839 |
|
| 840 |
def test_exclude_from_history(base_app) -> None: |
| 841 |
|
| 842 |
run_cmd(base_app, "history") |
| 843 |
verify_hi_last_result(base_app, 0) |
| 844 |
|
| 845 |
|
| 846 |
out, _err = run_cmd(base_app, "history") |
| 847 |
assert out == [] |
| 848 |
verify_hi_last_result(base_app, 0) |
| 849 |
|
| 850 |
|
| 851 |
run_cmd(base_app, "help") |
| 852 |
|
| 853 |
|
| 854 |
out, _err = run_cmd(base_app, "history") |
| 855 |
expected = normalize(""" 1 help""") |
| 856 |
assert out == expected |
| 857 |
verify_hi_last_result(base_app, 1) |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
@pytest.fixture(scope="session") |
| 864 |
def hist_file(): |
| 865 |
fd, filename = tempfile.mkstemp(prefix="hist_file", suffix=".dat") |
| 866 |
os.close(fd) |
| 867 |
yield filename |
| 868 |
|
| 869 |
with contextlib.suppress(FileNotFoundError): |
| 870 |
os.remove(filename) |
| 871 |
|
| 872 |
|
| 873 |
def test_history_file_is_directory(capsys) -> None: |
| 874 |
with tempfile.TemporaryDirectory() as test_dir: |
| 875 |
|
| 876 |
cmd2.Cmd(persistent_history_file=test_dir) |
| 877 |
_, err = capsys.readouterr() |
| 878 |
assert "is a directory" in err |
| 879 |
|
| 880 |
|
| 881 |
def test_history_can_create_directory(mocker) -> None: |
| 882 |
|
| 883 |
|
| 884 |
mocker.patch("atexit.register") |
| 885 |
|
| 886 |
|
| 887 |
with tempfile.TemporaryDirectory() as test_dir: |
| 888 |
pass |
| 889 |
assert not os.path.isdir(test_dir) |
| 890 |
|
| 891 |
|
| 892 |
hist_file_dir = os.path.join(test_dir, "subdir1", "subdir2") |
| 893 |
hist_file = os.path.join(hist_file_dir, "hist_file") |
| 894 |
|
| 895 |
|
| 896 |
cmd2.Cmd(persistent_history_file=hist_file) |
| 897 |
assert os.path.isdir(hist_file_dir) |
| 898 |
|
| 899 |
|
| 900 |
os.rmdir(hist_file_dir) |
| 901 |
|
| 902 |
|
| 903 |
def test_history_cannot_create_directory(mocker, capsys) -> None: |
| 904 |
mock_open = mocker.patch("os.makedirs") |
| 905 |
mock_open.side_effect = OSError |
| 906 |
|
| 907 |
hist_file_path = os.path.join("fake_dir", "file") |
| 908 |
cmd2.Cmd(persistent_history_file=hist_file_path) |
| 909 |
_, err = capsys.readouterr() |
| 910 |
assert "Error creating persistent history file directory" in err |
| 911 |
|
| 912 |
|
| 913 |
def test_history_file_permission_error(mocker, capsys) -> None: |
| 914 |
mock_open = mocker.patch("builtins.open") |
| 915 |
mock_open.side_effect = PermissionError |
| 916 |
|
| 917 |
cmd2.Cmd(persistent_history_file="/tmp/doesntmatter") |
| 918 |
out, err = capsys.readouterr() |
| 919 |
assert not out |
| 920 |
assert "Cannot read persistent history file" in err |
| 921 |
|
| 922 |
|
| 923 |
def test_history_file_bad_compression(mocker, capsys) -> None: |
| 924 |
history_file = "/tmp/doesntmatter" |
| 925 |
with open(history_file, "wb") as f: |
| 926 |
f.write(b"THIS IS NOT COMPRESSED DATA") |
| 927 |
|
| 928 |
cmd2.Cmd(persistent_history_file=history_file) |
| 929 |
out, err = capsys.readouterr() |
| 930 |
assert not out |
| 931 |
assert "Error decompressing persistent history data" in err |
| 932 |
|
| 933 |
|
| 934 |
def test_history_file_bad_json(mocker, capsys) -> None: |
| 935 |
import lzma |
| 936 |
|
| 937 |
data = b"THIS IS NOT JSON" |
| 938 |
compressed_data = lzma.compress(data) |
| 939 |
|
| 940 |
history_file = "/tmp/doesntmatter" |
| 941 |
with open(history_file, "wb") as f: |
| 942 |
f.write(compressed_data) |
| 943 |
|
| 944 |
cmd2.Cmd(persistent_history_file=history_file) |
| 945 |
out, err = capsys.readouterr() |
| 946 |
assert not out |
| 947 |
assert "Error processing persistent history data" in err |
| 948 |
|
| 949 |
|
| 950 |
def test_history_populates_pt(hist_file) -> None: |
| 951 |
|
| 952 |
app = cmd2.Cmd(persistent_history_file=hist_file) |
| 953 |
run_cmd(app, "help") |
| 954 |
run_cmd(app, "shortcuts") |
| 955 |
run_cmd(app, "shortcuts") |
| 956 |
run_cmd(app, "alias") |
| 957 |
|
| 958 |
app._persist_history() |
| 959 |
|
| 960 |
|
| 961 |
app = cmd2.Cmd(persistent_history_file=hist_file) |
| 962 |
assert len(app.history) == 4 |
| 963 |
assert app.history.get(1).statement.raw == "help" |
| 964 |
assert app.history.get(2).statement.raw == "shortcuts" |
| 965 |
assert app.history.get(3).statement.raw == "shortcuts" |
| 966 |
assert app.history.get(4).statement.raw == "alias" |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
pt_history = app.main_session.history.get_strings() |
| 972 |
assert len(pt_history) == 3 |
| 973 |
assert pt_history[0] == "help" |
| 974 |
assert pt_history[1] == "shortcuts" |
| 975 |
assert pt_history[2] == "alias" |
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
def test_persist_history_ensure_no_error_if_no_histfile(base_app, capsys) -> None: |
| 985 |
|
| 986 |
|
| 987 |
base_app._persist_history() |
| 988 |
out, err = capsys.readouterr() |
| 989 |
assert not out |
| 990 |
assert not err |
| 991 |
|
| 992 |
|
| 993 |
def test_persist_history_permission_error(hist_file, mocker, capsys) -> None: |
| 994 |
app = cmd2.Cmd(persistent_history_file=hist_file) |
| 995 |
run_cmd(app, "help") |
| 996 |
mock_open = mocker.patch("builtins.open") |
| 997 |
mock_open.side_effect = PermissionError |
| 998 |
app._persist_history() |
| 999 |
out, err = capsys.readouterr() |
| 1000 |
assert not out |
| 1001 |
assert "Cannot write persistent history file" in err |