Skip to content

Commit 96159f4

Browse files
committed
refactor(tests): pytest tmp_path instead of TMP_DIR
1 parent 0ef4977 commit 96159f4

2 files changed

Lines changed: 20 additions & 24 deletions

File tree

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import shutil
2-
3-
import pytest, os, csv
1+
import pytest
2+
import os
3+
import csv
44

55
from backend.config.paths import TMP_FILE_DIR
66
from backend.file_utils.csv_methods_writer import write_launch_info_to_csv
@@ -13,7 +13,7 @@
1313
@pytest.mark.parametrize(
1414
"filepaths",
1515
[
16-
([TMP_FILE_DIR + "/test0"]),
16+
([TMP_FILE_DIR + "/test"]),
1717
([TMP_FILE_DIR + "/test1", TMP_FILE_DIR + "/test2"]),
1818
],
1919
)
@@ -25,37 +25,37 @@ def test_removing_tmp_files(filepaths):
2525
assert not os.path.exists(path)
2626

2727

28-
def test_save_upload_file():
28+
def test_save_upload_file(tmp_path):
2929
test_content = b"Hello, World!" * 1000
30-
upload_dir = TMP_FILE_DIR + "/test_save_upload/"
31-
dest_path = upload_dir + "test.txt"
30+
dest_path = tmp_path / "test.txt"
3231

3332
mock_file = BytesIO(test_content)
3433
upload_file = Mock(spec=UploadFile)
3534
upload_file.file = mock_file
3635

37-
save_upload_file(upload_file, dest_path)
36+
save_upload_file(upload_file, str(dest_path))
3837

39-
assert os.path.exists(dest_path)
38+
assert dest_path.exists()
4039
with open(dest_path, "rb") as f:
4140
saved_content = f.read()
42-
shutil.rmtree(upload_dir)
41+
4342
assert saved_content == test_content
4443

4544

46-
def test_write_launch_info_to_csv():
47-
upload_dir = TMP_FILE_DIR + "/test_write_launch_info_to_csv/"
48-
os.makedirs(upload_dir)
49-
output_file = upload_dir + "launch_info.csv"
45+
def test_write_launch_info_to_csv(tmp_path):
46+
output_file = tmp_path / "launch_info.csv"
47+
5048
parsed_methods = [
5149
"ManuallyCollected.dll,BinSearchMain",
5250
"ManuallyCollected.dll,BellmanFord",
5351
"ManuallyCollected.dll,BinaryMaze1BFS",
5452
]
5553

56-
write_launch_info_to_csv(parsed_methods=parsed_methods, output_file=output_file)
54+
write_launch_info_to_csv(
55+
parsed_methods=parsed_methods, output_file=str(output_file)
56+
)
5757

58-
assert os.path.exists(output_file)
58+
assert output_file.exists()
5959

6060
with open(output_file, "r") as f:
6161
reader = csv.reader(f)
@@ -68,5 +68,5 @@ def test_write_launch_info_to_csv():
6868
["ManuallyCollected.dll", "BellmanFord"],
6969
["ManuallyCollected.dll", "BinaryMaze1BFS"],
7070
]
71-
shutil.rmtree(upload_dir)
71+
7272
assert rows[1:] == expected_data

backend/launch_service/tests/test_setup_functions.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
import os
2-
import shutil
32
from unittest.mock import Mock, patch
43

5-
from backend.config.paths import TMP_FILE_DIR, DOCKER_DIR, RESOURCES_DIR
4+
from backend.config.paths import DOCKER_DIR, RESOURCES_DIR
65
from backend.launch_service.app_setup import fetch_dataset, IMAGE_NAME, build_container
76

87

9-
def test_fetch_dataset():
8+
def test_fetch_dataset(tmp_path):
109
url = "https://example.com/dataset.csv"
1110
test_content = b"dll,method\nManuallyCollected.dll,BinSearchMain"
12-
upload_dir = TMP_FILE_DIR + "/test_dataset_upload/"
13-
os.makedirs(upload_dir)
14-
data_file = upload_dir + "dataset.json"
11+
data_file = tmp_path / "dataset.json"
1512

1613
with patch("requests.get") as mock_get:
1714
mock_response = Mock()
@@ -25,7 +22,6 @@ def test_fetch_dataset():
2522
assert os.path.exists(data_file)
2623
with open(data_file, "rb") as f:
2724
assert f.read() == test_content
28-
shutil.rmtree(upload_dir)
2925
mock_get.assert_called_once_with(url)
3026
mock_response.raise_for_status.assert_called_once()
3127

0 commit comments

Comments
 (0)