From a1318e39eabee78b786fc8c55fa77487fd057659 Mon Sep 17 00:00:00 2001 From: cangen185-sudo <234582933+cangen185-sudo@users.noreply.github.com> Date: Thu, 22 Oct 2026 12:31:54 +0800 Subject: [PATCH] fix(file-io): match normalized multiline reads during edit --- reme/steps/file_io/edit.py | 17 +++- tests/unit/test_read_newlines.py | 135 ++++++++++++++++++++++++++++++- 2 files changed, 145 insertions(+), 7 deletions(-) diff --git a/reme/steps/file_io/edit.py b/reme/steps/file_io/edit.py index 2e412d54..e71ea14f 100644 --- a/reme/steps/file_io/edit.py +++ b/reme/steps/file_io/edit.py @@ -1,5 +1,7 @@ """Find-and-replace text in a markdown file body (front matter is preserved).""" +import re + import frontmatter import yaml @@ -89,13 +91,20 @@ class EditStep(BaseStep): body = raw_text not_found_msg = f"text to replace was not found in {target}" - if old_str not in body: + if "\n" in old_str and "\r" not in old_str: + # Match the LF view returned by read without rewriting unmatched text. + # A CRLF pair is one line break, even when matching consecutive breaks. + newline = r"(?:\r\n|\r(?!\n)|(?" + app_context = ApplicationContext(workspace_dir=str(tmp_path)) + + async def run(): + reader = read_module.ReadStep(app_context=app_context) + await reader(path=filename, start_line=2, end_line=3) + assert reader.context.response.success is True + editor = edit_module.EditStep(app_context=app_context) + await editor(path=filename, old=reader.context.response.answer, new=replacement) + assert editor.context.response.success is True + assert path.read_bytes() == (prefix + replacement + suffix).encode(encoding) + + asyncio.run(run()) + + +def test_edit_normalized_needle_replaces_all_newline_styles(tmp_path): + """A mixed-newline body must not leave non-LF matches behind.""" + path = tmp_path / "note.txt" + path.write_bytes(b"prefix\r\nfirst\r\nsecond\nmiddle\rfirst\nsecond\r\nfirst\rsecond\r\nsuffix") + app_context = ApplicationContext(workspace_dir=str(tmp_path)) + + async def run(): + editor = edit_module.EditStep(app_context=app_context) + await editor(path=path.name, old="first\nsecond", new="done") + assert editor.context.response.success is True + assert "Replaced 3 occurrence(s)" in editor.context.response.answer + assert path.read_bytes() == b"prefix\r\ndone\nmiddle\rdone\r\ndone\r\nsuffix" + + asyncio.run(run()) + + +@pytest.mark.parametrize("separator", ["\r\n", "\r\n\r\n", "\r\r", "\n\n"]) +def test_edit_does_not_treat_crlf_as_two_line_breaks(tmp_path, separator): + """A normalized blank line cannot match a single CRLF separator.""" + path = tmp_path / "note.txt" + source = ("first" + separator + "second").encode("utf-8") + path.write_bytes(source) + app_context = ApplicationContext(workspace_dir=str(tmp_path)) + + async def run(): + editor = edit_module.EditStep(app_context=app_context) + await editor(path=path.name, old="first\n\nsecond", new="wrong") + if separator == "\r\n": + assert editor.context.response.success is False + assert "not found" in editor.context.response.answer + assert path.read_bytes() == source + else: + assert editor.context.response.success is True + assert path.read_bytes() == b"wrong" + + asyncio.run(run()) + + +def test_edit_normalized_needle_preserves_frontmatter(tmp_path): + """Universal-newline matching stays inside the Markdown body.""" + path = tmp_path / "note.md" + path.write_bytes(b"---\nname: first\ndescription: second\n---\nfirst\rsecond\rsuffix") + app_context = ApplicationContext(workspace_dir=str(tmp_path)) + + async def run(): + editor = edit_module.EditStep(app_context=app_context) + await editor(path=path.name, old="first\nsecond", new="done") + assert editor.context.response.success is True + assert path.read_bytes() == b"---\ndescription: second\nname: first\n---\n\ndone\rsuffix\n" + + before = path.read_bytes() + await editor(path=path.name, old="description: second\nname: first", new="wrong") + assert editor.context.response.success is False + assert path.read_bytes() == before + + asyncio.run(run()) + + +@pytest.mark.parametrize("old", ["first", "first\rsecond", "first\r\nsecond"]) +def test_edit_literal_needles_preserve_existing_matching(tmp_path, old): + """Single-line and explicitly raw-newline needles retain exact replacement behavior.""" + path = tmp_path / "note.txt" + source = "prefix\r\nfirst\rsecond / first\r\nsecond / first\nsecond\r\nsuffix" + path.write_bytes(source.encode("utf-8")) + app_context = ApplicationContext(workspace_dir=str(tmp_path)) + + async def run(): + editor = edit_module.EditStep(app_context=app_context) + await editor(path=path.name, old=old, new="done") + assert editor.context.response.success is True + assert path.read_bytes() == source.replace(old, "done").encode("utf-8") + + asyncio.run(run())