fix(tools): clarify identical old and new string error

This commit is contained in:
Elisa Martinez Abad 2026-07-28 06:17:01 -07:00 committed by kshitij
parent 4a2198bf51
commit 31a04db465
3 changed files with 49 additions and 1 deletions

View File

@ -179,6 +179,19 @@ class TestPatchReplace:
assert Path(path).read_text() == "hello earth\n"
def test_identical_replacement_explains_no_change(self, ops, tmp_path):
path = str(tmp_path / "unchanged.txt")
Path(path).write_text("hello world\n")
result = ops.patch_replace(path, "world", "world")
assert result.success is False
assert result.error is not None
assert "No edit was applied" in result.error
assert "existing text to replace in old_string" in result.error
assert "replacement text in new_string" in result.error
assert Path(path).read_text() == "hello world\n"
def test_multiline_patch(self, ops, tmp_path):
path = str(tmp_path / "multi.txt")
Path(path).write_text("line1\nline2\nline3\n")

View File

@ -25,6 +25,15 @@ class TestExactMatch:
assert count == 0
assert err is not None
def test_identical_strings(self):
new, count, _, err = fuzzy_find_and_replace("abc", "abc", "abc")
assert count == 0
assert new == "abc"
assert err == (
"No edit was applied because old_string and new_string are identical. "
"Provide the existing text to replace in old_string and the changed "
"replacement text in new_string."
)
def test_multiline_exact(self):
content = "line1\nline2\nline3"
@ -431,6 +440,28 @@ class TestFormatNoMatchHint:
)
assert result == ""
def test_silent_on_identical_strings(self):
"""old_string == new_string — hint irrelevant."""
result = self.fmt(
"No edit was applied because old_string and new_string are identical. "
"Provide the existing text to replace in old_string and the changed "
"replacement text in new_string.",
0, "foo", "foo bar\n",
)
assert result == ""
def test_silent_when_match_count_nonzero(self):
"""If match succeeded, we shouldn't be in the error path — defense in depth."""
result = self.fmt(
"Could not find a match for old_string in the file",
1, "foo", "foo bar\n",
)
assert result == ""
def test_silent_on_none_error(self):
"""No error at all — no hint."""
result = self.fmt(None, 0, "foo", "bar\n")
assert result == ""
def test_silent_when_no_similar_content(self):
"""Even for a valid no-match error, skip hint when nothing similar exists."""

View File

@ -143,7 +143,11 @@ def fuzzy_find_and_replace(content: str, old_string: str, new_string: str,
return content, 0, None, "old_string is only whitespace — provide non-blank text to match"
if old_string == new_string:
return content, 0, None, "old_string and new_string are identical"
return content, 0, None, (
"No edit was applied because old_string and new_string are identical. "
"Provide the existing text to replace in old_string and the changed "
"replacement text in new_string."
)
# Try each matching strategy in order
strategies: List[Tuple[str, Callable]] = [