Skip to content

Commit da0eb65

Browse files
committed
refactor: use tmp_path fixture for temporary files
Replace tempfile operations with pytest's tmp_path fixture in 2 tests: - test_message_file_flag: Replace tempfile.mktemp() with tmp_path - test_read_option_with_external_file: Replace NamedTemporaryFile with tmp_path Benefits: - Removes tempfile import dependency - Automatic cleanup (no manual os.unlink/os.remove needed) - Safer and more reliable (no deprecated mktemp) - Standard pytest pattern for better maintainability - Eliminates try/finally blocks for cleanup
1 parent 5a42d6d commit da0eb65

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

tests/basic/test_main.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import os
44
import platform
55
import subprocess
6-
import tempfile
76
import types
87
from pathlib import Path
98
from unittest.mock import AsyncMock, MagicMock
@@ -371,11 +370,10 @@ def test_env_file_override(dummy_io, git_temp_dir, mocker, monkeypatch):
371370
assert os.environ["E"] == "existing"
372371

373372

374-
def test_message_file_flag(dummy_io, git_temp_dir, mocker):
373+
def test_message_file_flag(dummy_io, git_temp_dir, mocker, tmp_path):
375374
message_file_content = "This is a test message from a file."
376-
message_file_path = tempfile.mktemp()
377-
with open(message_file_path, "w", encoding="utf-8") as message_file:
378-
message_file.write(message_file_content)
375+
message_file = tmp_path / "message.txt"
376+
message_file.write_text(message_file_content, encoding="utf-8")
379377

380378
# Create a mock async function for the run method
381379
async def mock_run(*args, **kwargs):
@@ -389,14 +387,12 @@ async def mock_run(*args, **kwargs):
389387
MockCoder.return_value = mock_coder_instance
390388

391389
main(
392-
["--yes-always", "--message-file", message_file_path],
390+
["--yes-always", "--message-file", str(message_file)],
393391
**dummy_io,
394392
)
395393
# Check that run was called with the correct message
396394
mock_coder_instance.run.assert_called_once_with(with_message=message_file_content)
397395

398-
os.remove(message_file_path)
399-
400396

401397
def test_encodings_arg(dummy_io, git_temp_dir, mocker):
402398
fname = "foo.py"
@@ -762,22 +758,18 @@ def test_read_option(dummy_io, git_temp_dir):
762758
assert str(Path(test_file).resolve()) in coder.abs_read_only_fnames
763759

764760

765-
def test_read_option_with_external_file(dummy_io, git_temp_dir):
766-
with tempfile.NamedTemporaryFile(mode="w", delete=False) as external_file:
767-
external_file.write("External file content")
768-
external_file_path = external_file.name
761+
def test_read_option_with_external_file(dummy_io, git_temp_dir, tmp_path):
762+
external_file = tmp_path / "external_file.txt"
763+
external_file.write_text("External file content")
769764

770-
try:
771-
coder = main(
772-
["--read", external_file_path, "--exit", "--yes-always"],
773-
**dummy_io,
774-
return_coder=True,
775-
)
765+
coder = main(
766+
["--read", str(external_file), "--exit", "--yes-always"],
767+
**dummy_io,
768+
return_coder=True,
769+
)
776770

777-
real_external_file_path = os.path.realpath(external_file_path)
778-
assert real_external_file_path in coder.abs_read_only_fnames
779-
finally:
780-
os.unlink(external_file_path)
771+
real_external_file_path = os.path.realpath(str(external_file))
772+
assert real_external_file_path in coder.abs_read_only_fnames
781773

782774

783775
def test_model_metadata_file(dummy_io, git_temp_dir):

0 commit comments

Comments
 (0)