Skip to content

Commit f3e8009

Browse files
committed
Give the corrupt-history tests their own temp files
test_history_file_bad_compression and test_history_file_bad_json wrote to a fixed /tmp/doesntmatter, which on the Windows runner is D:\tmp and does not exist on a fresh machine. They passed only when the preceding permission-error test had already run: mocking builtins.open there does not stop the history setup from creating the file's parent directory, so that test created D:\tmp as a side effect. Under xdist the order is not fixed, and adding tests elsewhere shifted the schedule so both writers reached a worker first and failed on every Windows job. All three tests now use tmp_path, so none depends on a directory existing or on another test having run. Each passes alone on a single worker. Validation: 2587 passed, 6 skipped with coverage; make check, make test, make docs-test and git diff --check passed.
1 parent 08c7355 commit f3e8009

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

‎tests/test_history.py‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -910,38 +910,39 @@ def test_history_cannot_create_directory(mocker, capsys) -> None:
910910
assert "Error creating persistent history file directory" in err
911911

912912

913-
def test_history_file_permission_error(mocker, capsys) -> None:
913+
def test_history_file_permission_error(mocker, capsys, tmp_path) -> None:
914914
mock_open = mocker.patch("builtins.open")
915915
mock_open.side_effect = PermissionError
916916

917-
cmd2.Cmd(persistent_history_file="/tmp/doesntmatter")
917+
# A path under tmp_path rather than a fixed one: mocking open() does not stop the
918+
# history setup from creating the file's parent directory, and a fixed path would leave
919+
# that directory behind as a side effect other tests could come to depend on.
920+
cmd2.Cmd(persistent_history_file=str(tmp_path / "doesntmatter"))
918921
out, err = capsys.readouterr()
919922
assert not out
920923
assert "Cannot read persistent history file" in err
921924

922925

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")
926+
def test_history_file_bad_compression(capsys, tmp_path) -> None:
927+
history_file = tmp_path / "doesntmatter"
928+
history_file.write_bytes(b"THIS IS NOT COMPRESSED DATA")
927929

928-
cmd2.Cmd(persistent_history_file=history_file)
930+
cmd2.Cmd(persistent_history_file=str(history_file))
929931
out, err = capsys.readouterr()
930932
assert not out
931933
assert "Error decompressing persistent history data" in err
932934

933935

934-
def test_history_file_bad_json(mocker, capsys) -> None:
936+
def test_history_file_bad_json(capsys, tmp_path) -> None:
935937
import lzma
936938

937939
data = b"THIS IS NOT JSON"
938940
compressed_data = lzma.compress(data)
939941

940-
history_file = "/tmp/doesntmatter"
941-
with open(history_file, "wb") as f:
942-
f.write(compressed_data)
942+
history_file = tmp_path / "doesntmatter"
943+
history_file.write_bytes(compressed_data)
943944

944-
cmd2.Cmd(persistent_history_file=history_file)
945+
cmd2.Cmd(persistent_history_file=str(history_file))
945946
out, err = capsys.readouterr()
946947
assert not out
947948
assert "Error processing persistent history data" in err

0 commit comments

Comments
 (0)