|
4 | 4 | import contextlib |
5 | 5 | import os |
6 | 6 | import re |
| 7 | +import sys |
7 | 8 | import tempfile |
8 | 9 | from unittest import ( |
9 | 10 | mock, |
@@ -1324,6 +1325,40 @@ def fake_editor(fname) -> None: |
1324 | 1325 | run_script.assert_called_once() |
1325 | 1326 |
|
1326 | 1327 |
|
| 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 | + |
1327 | 1362 | def test_history_help_explains_selection_and_persisted_actions(base_app) -> None: |
1328 | 1363 | out, err = run_cmd(base_app, "help history") |
1329 | 1364 | text = " ".join(out) |
|
0 commit comments