Skip to content

Commit 38a4509

Browse files
authored
fix: use Path in test_files.py (#4811)
1 parent f343ccd commit 38a4509

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

tests/_server/api/endpoints/test_files.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import random
6+
from pathlib import Path
67
from typing import TYPE_CHECKING
78

89
from tests._server.conftest import get_session_manager
@@ -29,23 +30,24 @@ def test_rename(client: TestClient) -> None:
2930
).file_router.get_unique_file_key()
3031

3132
assert current_filename
32-
assert os.path.exists(current_filename)
33+
current_path = Path(current_filename)
34+
assert current_path.exists()
3335

34-
directory = os.path.dirname(current_filename)
36+
directory = current_path.parent
3537
random_name = random.randint(0, 100000)
36-
new_filename = f"{directory}/test_{random_name}.py"
38+
new_path = directory / f"test_{random_name}.py"
3739

3840
response = client.post(
3941
"/api/kernel/rename",
4042
headers=HEADERS,
4143
json={
42-
"filename": new_filename,
44+
"filename": str(new_path),
4345
},
4446
)
4547
assert response.json() == {"success": True}
4648

47-
assert os.path.exists(new_filename)
48-
assert not os.path.exists(current_filename)
49+
assert new_path.exists()
50+
assert not current_path.exists()
4951

5052

5153
@with_session(SESSION_ID)
@@ -63,13 +65,14 @@ def test_read_code(client: TestClient) -> None:
6365
def test_save_file(client: TestClient) -> None:
6466
filename = get_session_manager(client).file_router.get_unique_file_key()
6567
assert filename
68+
path = Path(filename)
6669

6770
response = client.post(
6871
"/api/kernel/save",
6972
headers=HEADERS,
7073
json={
7174
"cell_ids": ["1"],
72-
"filename": filename,
75+
"filename": str(path),
7376
"codes": ["import marimo as mo"],
7477
"names": ["my_cell"],
7578
"configs": [
@@ -82,7 +85,7 @@ def test_save_file(client: TestClient) -> None:
8285
)
8386
assert response.status_code == 200, response.text
8487
assert "import marimo" in response.text
85-
file_contents = open(filename).read()
88+
file_contents = path.read_text()
8689
assert "import marimo as mo" in file_contents
8790
assert "@app.cell(hide_code=True)" in file_contents
8891
assert "my_cell" in file_contents
@@ -93,7 +96,7 @@ def test_save_file(client: TestClient) -> None:
9396
headers=HEADERS,
9497
json={
9598
"cell_ids": ["1"],
96-
"filename": filename,
99+
"filename": str(path),
97100
"codes": ["import marimo as mo"],
98101
"names": ["__"],
99102
"configs": [
@@ -109,22 +112,23 @@ def test_save_file(client: TestClient) -> None:
109112
def test_save_with_header(client: TestClient) -> None:
110113
filename = get_session_manager(client).file_router.get_unique_file_key()
111114
assert filename
112-
assert os.path.exists(filename)
115+
path = Path(filename)
116+
assert path.exists()
113117

114118
header = (
115119
'"""This is a docstring"""\n\n' + "# Copyright 2024\n# Linter ignore\n"
116120
)
117121
# Prepend a header to the file
118-
contents = open(filename).read()
122+
contents = path.read_text()
119123
contents = header + contents
120-
open(filename, "w", encoding="UTF-8").write(contents)
124+
path.write_text(contents, encoding="UTF-8")
121125

122126
response = client.post(
123127
"/api/kernel/save",
124128
headers=HEADERS,
125129
json={
126130
"cell_ids": ["1"],
127-
"filename": filename,
131+
"filename": str(path),
128132
"codes": ["import marimo as mo"],
129133
"names": ["my_cell"],
130134
"configs": [
@@ -138,7 +142,7 @@ def test_save_with_header(client: TestClient) -> None:
138142

139143
assert response.status_code == 200, response.text
140144
assert "import marimo" in response.text
141-
file_contents = open(filename).read()
145+
file_contents = path.read_text()
142146
assert "import marimo as mo" in file_contents
143147
# Race condition with uv (seen in python 3.10)
144148
if file_contents.startswith("# ///"):
@@ -152,7 +156,8 @@ def test_save_with_header(client: TestClient) -> None:
152156
def test_save_with_invalid_file(client: TestClient) -> None:
153157
filename = get_session_manager(client).file_router.get_unique_file_key()
154158
assert filename
155-
assert os.path.exists(filename)
159+
path = Path(filename)
160+
assert path.exists()
156161

157162
header = (
158163
'"""This is a docstring"""\n\n'
@@ -161,16 +166,16 @@ def test_save_with_invalid_file(client: TestClient) -> None:
161166
)
162167

163168
# Prepend a header to the file
164-
contents = open(filename).read()
169+
contents = path.read_text()
165170
contents = header + contents
166-
open(filename, "w", encoding="UTF-8").write(contents)
171+
path.write_text(contents)
167172

168173
response = client.post(
169174
"/api/kernel/save",
170175
headers=HEADERS,
171176
json={
172177
"cell_ids": ["1"],
173-
"filename": filename,
178+
"filename": str(path),
174179
"codes": ["import marimo as mo"],
175180
"names": ["my_cell"],
176181
"configs": [
@@ -184,7 +189,7 @@ def test_save_with_invalid_file(client: TestClient) -> None:
184189

185190
assert response.status_code == 200, response.text
186191
assert "import marimo" in response.text
187-
file_contents = open(filename).read()
192+
file_contents = path.read_text()
188193
assert "@app.cell(hide_code=True)" in file_contents
189194
assert "my_cell" in file_contents
190195

@@ -222,8 +227,9 @@ def test_save_file_cannot_rename(client: TestClient) -> None:
222227
def test_save_app_config(client: TestClient) -> None:
223228
filename = get_session_manager(client).file_router.get_unique_file_key()
224229
assert filename
230+
path = Path(filename)
225231

226-
file_contents = open(filename).read()
232+
file_contents = path.read_text()
227233
assert 'marimo.App(width="medium"' not in file_contents
228234

229235
response = client.post(
@@ -235,16 +241,17 @@ def test_save_app_config(client: TestClient) -> None:
235241
)
236242
assert response.status_code == 200, response.text
237243
assert "import marimo" in response.text
238-
file_contents = open(filename).read()
244+
file_contents = path.read_text()
239245
assert 'marimo.App(width="medium"' in file_contents
240246

241247

242248
@with_session(SESSION_ID)
243249
def test_copy_file(client: TestClient) -> None:
244250
filename = get_session_manager(client).file_router.get_unique_file_key()
245251
assert filename
246-
assert os.path.exists(filename)
247-
file_contents = open(filename).read()
252+
path = Path(filename)
253+
assert path.exists()
254+
file_contents = path.read_text()
248255
assert "import marimo as mo" in file_contents
249256
assert 'marimo.App(width="full"' in file_contents
250257

0 commit comments

Comments
 (0)