Skip to content

Commit b3fa14c

Browse files
ouatu-romscolnick
andauthored
make _format_plan respect format_on_save; format enabled/disabled unit tests (#9380)
handles #9348. --------- Co-authored-by: Myles Scolnick <myles@marimo.io>
1 parent 981b3e6 commit b3fa14c

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

marimo/_code_mode/_context.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,10 @@ async def _apply_ops(
16091609
# ------------------------------------------------------------------
16101610

16111611
async def _format_plan(self, plan: list[_PlanEntry]) -> list[_PlanEntry]:
1612-
"""Format new/changed code in the plan with the default formatter."""
1612+
"""Format new/changed code when save-time formatting is enabled."""
1613+
if not self._kernel.user_config["save"]["format_on_save"]:
1614+
return plan
1615+
16131616
existing_code = {cell.id: cell.code for cell in self._document.cells}
16141617

16151618
to_format: dict[CellId_t, str] = {}

tests/_code_mode/test_context.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,42 @@ async def test_update_config_only(self, k: Kernel) -> None:
327327
]
328328
)
329329

330+
async def test_update_code_skips_formatting_when_disabled(
331+
self, k: Kernel
332+
) -> None:
333+
await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
334+
335+
with (
336+
patch.dict(k.user_config["save"], {"format_on_save": False}),
337+
patch(
338+
"marimo._code_mode._context.DefaultFormatter.format",
339+
new_callable=AsyncMock,
340+
) as mock_format,
341+
):
342+
with _ctx(k) as ctx:
343+
async with ctx as nb:
344+
nb.edit_cell("0", code="x= 42")
345+
346+
mock_format.assert_not_awaited()
347+
assert _graph_codes(k) == {"0": "x= 42"}
348+
349+
async def test_update_code_formats_when_enabled(self, k: Kernel) -> None:
350+
await k.run([ExecuteCellCommand(cell_id=CellId_t("0"), code="x = 1")])
351+
352+
with (
353+
patch.dict(k.user_config["save"], {"format_on_save": True}),
354+
patch(
355+
"marimo._code_mode._context.DefaultFormatter.format",
356+
new=AsyncMock(return_value={"0": "x = 42"}),
357+
) as mock_format,
358+
):
359+
with _ctx(k) as ctx:
360+
async with ctx as nb:
361+
nb.edit_cell("0", code="x= 42")
362+
363+
mock_format.assert_awaited_once()
364+
assert _graph_codes(k) == {"0": "x = 42"}
365+
330366

331367
class TestCombined:
332368
async def test_delete_and_add(self, k: Kernel) -> None:

0 commit comments

Comments
 (0)