Skip to content

Commit 18506b6

Browse files
committed
Test clean handling of oversized history indices
1 parent 4cdc756 commit 18506b6

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎tests/test_history.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import contextlib
55
import os
66
import re
7+
import sys
78
import tempfile
89
from unittest import (
910
mock,
@@ -1324,6 +1325,40 @@ def fake_editor(fname) -> None:
13241325
run_script.assert_called_once()
13251326

13261327

1328+
@pytest.mark.parametrize("sign", ["", "-"])
1329+
@pytest.mark.parametrize("action", ["", "--run", "--edit", "--output-file"])
1330+
@pytest.mark.parametrize("debug", [False, True])
1331+
def test_history_integer_conversion_limit_has_no_side_effects(
1332+
base_app, hist, monkeypatch, tmp_path, sign, action, debug
1333+
) -> None:
1334+
base_app.history = hist
1335+
base_app.debug = debug
1336+
effects = [mock.Mock() for _ in range(4)]
1337+
monkeypatch.setattr("cmd2.cmd2.tempfile.mkstemp", effects[0])
1338+
monkeypatch.setattr(base_app, "run_editor", effects[1])
1339+
monkeypatch.setattr(base_app, "do_run_script", effects[2])
1340+
monkeypatch.setattr(base_app, "runcmds_plus_hooks", effects[3])
1341+
output_file = tmp_path / "existing.txt"
1342+
output_file.write_text("keep me\n")
1343+
options = f'--output-file "{output_file}"' if action == "--output-file" else action
1344+
limit = sys.int_info.str_digits_check_threshold
1345+
selector = sign + "9" * (limit + 1)
1346+
original_limit = sys.get_int_max_str_digits()
1347+
try:
1348+
sys.set_int_max_str_digits(limit)
1349+
out, err = run_cmd(base_app, f"history {options} -- {selector}")
1350+
finally:
1351+
sys.set_int_max_str_digits(original_limit)
1352+
1353+
assert out == []
1354+
assert err == ["No history items in range"]
1355+
assert base_app.last_result is False
1356+
for effect in effects:
1357+
effect.assert_not_called()
1358+
assert output_file.read_text() == "keep me\n"
1359+
assert len(base_app.history) == 4
1360+
1361+
13271362
def test_history_help_explains_selection_and_persisted_actions(base_app) -> None:
13281363
out, err = run_cmd(base_app, "help history")
13291364
text = " ".join(out)

0 commit comments

Comments
 (0)