MCPcopy
hub / github.com/marimo-team/marimo / test_save_with_header

Function test_save_with_header

tests/_server/api/endpoints/test_files.py:159–205  ·  view source on GitHub ↗
(client: TestClient)

Source from the content-addressed store, hash-verified

157)
158@with_session(SESSION_ID)
159def test_save_with_header(client: TestClient) -> None:
160 filename = get_session_manager(client).workspace.get_unique_file_key()
161 assert filename
162 path = Path(filename)
163 assert path.exists()
164
165 copyright_year = datetime.now().year
166 header = (
167 '"""This is a docstring"""\n\n'
168 + f"# Copyright {copyright_year}\n# Linter ignore\n"
169 )
170 # Prepend a header to the file
171 contents = path.read_text()
172 contents = header + contents
173 path.write_text(contents, encoding="UTF-8")
174
175 response = client.post(
176 "/api/kernel/save",
177 headers=HEADERS,
178 json={
179 "cellIds": ["1"],
180 "filename": str(path),
181 "codes": ["import marimo as mo"],
182 "names": ["my_cell"],
183 "configs": [
184 {
185 "hide_code": True,
186 "disabled": False,
187 }
188 ],
189 },
190 )
191
192 assert response.status_code == 200, response.text
193 assert "import marimo" in response.text
194
195 def _assert_contents():
196 file_contents = path.read_text()
197 assert "import marimo as mo" in file_contents
198 # Race condition with uv (seen in python 3.10)
199 if file_contents.startswith("# ///"):
200 file_contents = file_contents.split("# ///")[2].lstrip()
201 assert file_contents.startswith(header.rstrip()), "Header was removed"
202 assert "@app.cell(hide_code=True)" in file_contents
203 assert "my_cell" in file_contents
204
205 try_assert_n_times(5, _assert_contents)
206
207
208@pytest.mark.flaky(reruns=5)

Callers

nothing calls this directly

Calls 8

get_session_managerFunction · 0.90
try_assert_n_timesFunction · 0.90
postMethod · 0.80
get_unique_file_keyMethod · 0.45
existsMethod · 0.45
nowMethod · 0.45
read_textMethod · 0.45
write_textMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…