3
3
4
4
import os
5
5
import random
6
+ from pathlib import Path
6
7
from typing import TYPE_CHECKING
7
8
8
9
from tests ._server .conftest import get_session_manager
@@ -29,23 +30,24 @@ def test_rename(client: TestClient) -> None:
29
30
).file_router .get_unique_file_key ()
30
31
31
32
assert current_filename
32
- assert os .path .exists (current_filename )
33
+ current_path = Path (current_filename )
34
+ assert current_path .exists ()
33
35
34
- directory = os . path . dirname ( current_filename )
36
+ directory = current_path . parent
35
37
random_name = random .randint (0 , 100000 )
36
- new_filename = f" { directory } / test_{ random_name } .py"
38
+ new_path = directory / f" test_{ random_name } .py"
37
39
38
40
response = client .post (
39
41
"/api/kernel/rename" ,
40
42
headers = HEADERS ,
41
43
json = {
42
- "filename" : new_filename ,
44
+ "filename" : str ( new_path ) ,
43
45
},
44
46
)
45
47
assert response .json () == {"success" : True }
46
48
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 ()
49
51
50
52
51
53
@with_session (SESSION_ID )
@@ -63,13 +65,14 @@ def test_read_code(client: TestClient) -> None:
63
65
def test_save_file (client : TestClient ) -> None :
64
66
filename = get_session_manager (client ).file_router .get_unique_file_key ()
65
67
assert filename
68
+ path = Path (filename )
66
69
67
70
response = client .post (
68
71
"/api/kernel/save" ,
69
72
headers = HEADERS ,
70
73
json = {
71
74
"cell_ids" : ["1" ],
72
- "filename" : filename ,
75
+ "filename" : str ( path ) ,
73
76
"codes" : ["import marimo as mo" ],
74
77
"names" : ["my_cell" ],
75
78
"configs" : [
@@ -82,7 +85,7 @@ def test_save_file(client: TestClient) -> None:
82
85
)
83
86
assert response .status_code == 200 , response .text
84
87
assert "import marimo" in response .text
85
- file_contents = open ( filename ). read ()
88
+ file_contents = path . read_text ()
86
89
assert "import marimo as mo" in file_contents
87
90
assert "@app.cell(hide_code=True)" in file_contents
88
91
assert "my_cell" in file_contents
@@ -93,7 +96,7 @@ def test_save_file(client: TestClient) -> None:
93
96
headers = HEADERS ,
94
97
json = {
95
98
"cell_ids" : ["1" ],
96
- "filename" : filename ,
99
+ "filename" : str ( path ) ,
97
100
"codes" : ["import marimo as mo" ],
98
101
"names" : ["__" ],
99
102
"configs" : [
@@ -109,22 +112,23 @@ def test_save_file(client: TestClient) -> None:
109
112
def test_save_with_header (client : TestClient ) -> None :
110
113
filename = get_session_manager (client ).file_router .get_unique_file_key ()
111
114
assert filename
112
- assert os .path .exists (filename )
115
+ path = Path (filename )
116
+ assert path .exists ()
113
117
114
118
header = (
115
119
'"""This is a docstring"""\n \n ' + "# Copyright 2024\n # Linter ignore\n "
116
120
)
117
121
# Prepend a header to the file
118
- contents = open ( filename ). read ()
122
+ contents = path . read_text ()
119
123
contents = header + contents
120
- open ( filename , "w" , encoding = "UTF-8" ). write ( contents )
124
+ path . write_text ( contents , encoding = "UTF-8" )
121
125
122
126
response = client .post (
123
127
"/api/kernel/save" ,
124
128
headers = HEADERS ,
125
129
json = {
126
130
"cell_ids" : ["1" ],
127
- "filename" : filename ,
131
+ "filename" : str ( path ) ,
128
132
"codes" : ["import marimo as mo" ],
129
133
"names" : ["my_cell" ],
130
134
"configs" : [
@@ -138,7 +142,7 @@ def test_save_with_header(client: TestClient) -> None:
138
142
139
143
assert response .status_code == 200 , response .text
140
144
assert "import marimo" in response .text
141
- file_contents = open ( filename ). read ()
145
+ file_contents = path . read_text ()
142
146
assert "import marimo as mo" in file_contents
143
147
# Race condition with uv (seen in python 3.10)
144
148
if file_contents .startswith ("# ///" ):
@@ -152,7 +156,8 @@ def test_save_with_header(client: TestClient) -> None:
152
156
def test_save_with_invalid_file (client : TestClient ) -> None :
153
157
filename = get_session_manager (client ).file_router .get_unique_file_key ()
154
158
assert filename
155
- assert os .path .exists (filename )
159
+ path = Path (filename )
160
+ assert path .exists ()
156
161
157
162
header = (
158
163
'"""This is a docstring"""\n \n '
@@ -161,16 +166,16 @@ def test_save_with_invalid_file(client: TestClient) -> None:
161
166
)
162
167
163
168
# Prepend a header to the file
164
- contents = open ( filename ). read ()
169
+ contents = path . read_text ()
165
170
contents = header + contents
166
- open ( filename , "w" , encoding = "UTF-8" ). write (contents )
171
+ path . write_text (contents )
167
172
168
173
response = client .post (
169
174
"/api/kernel/save" ,
170
175
headers = HEADERS ,
171
176
json = {
172
177
"cell_ids" : ["1" ],
173
- "filename" : filename ,
178
+ "filename" : str ( path ) ,
174
179
"codes" : ["import marimo as mo" ],
175
180
"names" : ["my_cell" ],
176
181
"configs" : [
@@ -184,7 +189,7 @@ def test_save_with_invalid_file(client: TestClient) -> None:
184
189
185
190
assert response .status_code == 200 , response .text
186
191
assert "import marimo" in response .text
187
- file_contents = open ( filename ). read ()
192
+ file_contents = path . read_text ()
188
193
assert "@app.cell(hide_code=True)" in file_contents
189
194
assert "my_cell" in file_contents
190
195
@@ -222,8 +227,9 @@ def test_save_file_cannot_rename(client: TestClient) -> None:
222
227
def test_save_app_config (client : TestClient ) -> None :
223
228
filename = get_session_manager (client ).file_router .get_unique_file_key ()
224
229
assert filename
230
+ path = Path (filename )
225
231
226
- file_contents = open ( filename ). read ()
232
+ file_contents = path . read_text ()
227
233
assert 'marimo.App(width="medium"' not in file_contents
228
234
229
235
response = client .post (
@@ -235,16 +241,17 @@ def test_save_app_config(client: TestClient) -> None:
235
241
)
236
242
assert response .status_code == 200 , response .text
237
243
assert "import marimo" in response .text
238
- file_contents = open ( filename ). read ()
244
+ file_contents = path . read_text ()
239
245
assert 'marimo.App(width="medium"' in file_contents
240
246
241
247
242
248
@with_session (SESSION_ID )
243
249
def test_copy_file (client : TestClient ) -> None :
244
250
filename = get_session_manager (client ).file_router .get_unique_file_key ()
245
251
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 ()
248
255
assert "import marimo as mo" in file_contents
249
256
assert 'marimo.App(width="full"' in file_contents
250
257
0 commit comments