Skip to content

Commit fee72d8

Browse files
authored
Merge pull request #25 from lzwjava/feat/rename-patch-to-edit
refactor: rename PatchTool to EditTool and apply_patch to edit
2 parents 99c6570 + c48741f commit fee72d8

3 files changed

Lines changed: 27 additions & 27 deletions

File tree

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import unittest
22
import os
3-
from patch_tool import PatchTool
3+
from tools.edit_tool import EditTool
44

55

6-
class TestPatchTool(unittest.TestCase):
6+
class TestEditTool(unittest.TestCase):
77
def setUp(self):
88
self.test_file = "test_file.txt"
99
with open(self.test_file, "w") as f:
@@ -14,19 +14,19 @@ def tearDown(self):
1414
os.remove(self.test_file)
1515

1616
def test_basic_replace(self):
17-
patch = """--- test_file.txt
17+
edit = """--- test_file.txt
1818
+++ test_file.txt
1919
@@ -2,1 +2,1 @@
2020
-Line 2
2121
+Line Two Modified
2222
"""
23-
new_content = PatchTool.apply_patch(self.test_file, patch)
23+
new_content = EditTool.edit(self.test_file, edit)
2424
self.assertIn("Line Two Modified\n", new_content)
2525
self.assertNotIn("Line 2\n", new_content)
2626
self.assertEqual(new_content.splitlines()[1], "Line Two Modified")
2727

2828
def test_multi_line_hunk(self):
29-
patch = """--- test_file.txt
29+
edit = """--- test_file.txt
3030
+++ test_file.txt
3131
@@ -3,2 +3,3 @@
3232
-Line 3
@@ -35,15 +35,15 @@ def test_multi_line_hunk(self):
3535
+Line Three.Five
3636
+Line Four
3737
"""
38-
new_content = PatchTool.apply_patch(self.test_file, patch)
38+
new_content = EditTool.edit(self.test_file, edit)
3939
lines = new_content.splitlines()
4040
self.assertEqual(lines[2], "Line Three")
4141
self.assertEqual(lines[3], "Line Three.Five")
4242
self.assertEqual(lines[4], "Line Four")
4343
self.assertEqual(len(lines), 6)
4444

4545
def test_multiple_hunks(self):
46-
patch = """--- test_file.txt
46+
edit = """--- test_file.txt
4747
+++ test_file.txt
4848
@@ -1,1 +1,1 @@
4949
-Line 1
@@ -52,7 +52,7 @@ def test_multiple_hunks(self):
5252
-Line 5
5353
+Last Line
5454
"""
55-
new_content = PatchTool.apply_patch(self.test_file, patch)
55+
new_content = EditTool.edit(self.test_file, edit)
5656
lines = new_content.splitlines()
5757
self.assertEqual(lines[0], "First Line")
5858
self.assertEqual(lines[-1], "Last Line")
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import re
33

44

5-
class PatchTool:
5+
class EditTool:
66
@staticmethod
7-
def apply_patch(file_path: str, patch_content: str) -> str:
7+
def edit(file_path: str, edit_content: str) -> str:
88
"""
9-
Applies a unified diff patch to a file.
9+
Applies a unified diff edit to a file.
1010
Returns the new content of the file.
1111
"""
1212
if not os.path.exists(file_path):
@@ -15,13 +15,13 @@ def apply_patch(file_path: str, patch_content: str) -> str:
1515
with open(file_path, "r", encoding="utf-8") as f:
1616
content = f.readlines()
1717

18-
patch_lines = patch_content.splitlines(keepends=True)
18+
edit_lines = edit_content.splitlines(keepends=True)
1919
hunk_re = re.compile(r"^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@")
2020

2121
hunks = []
2222
current_hunk = None
2323

24-
for line in patch_lines:
24+
for line in edit_lines:
2525
if line.startswith("---") or line.startswith("+++"):
2626
continue
2727

@@ -58,7 +58,7 @@ def apply_patch(file_path: str, patch_content: str) -> str:
5858
elif h_line.startswith("-"):
5959
pass
6060

61-
# Pad result_lines if the patch references lines beyond current EOF
61+
# Pad result_lines if the edit references lines beyond current EOF
6262
while len(result_lines) < start_in_file:
6363
result_lines.append("\n")
6464

@@ -72,10 +72,10 @@ def apply_patch(file_path: str, patch_content: str) -> str:
7272
import sys
7373

7474
if len(sys.argv) == 3:
75-
path, patch_file = sys.argv[1], sys.argv[2]
76-
with open(patch_file, "r") as pf:
77-
patch = pf.read()
78-
new_text = PatchTool.apply_patch(path, patch)
75+
path, edit_file = sys.argv[1], sys.argv[2]
76+
with open(edit_file, "r") as pf:
77+
edit = pf.read()
78+
new_text = EditTool.edit(path, edit)
7979
with open(path, "w") as f:
8080
f.write(new_text)
81-
print(f"Applied patch to {path}")
81+
print(f"Applied edit to {path}")
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import unittest
22
import os
3-
from tools.patch_tool import PatchTool
3+
from edit_tool import EditTool
44

55

6-
class TestPatchTool(unittest.TestCase):
6+
class TestEditTool(unittest.TestCase):
77
def setUp(self):
88
self.test_file = "test_file.txt"
99
with open(self.test_file, "w") as f:
@@ -14,19 +14,19 @@ def tearDown(self):
1414
os.remove(self.test_file)
1515

1616
def test_basic_replace(self):
17-
patch = """--- test_file.txt
17+
edit = """--- test_file.txt
1818
+++ test_file.txt
1919
@@ -2,1 +2,1 @@
2020
-Line 2
2121
+Line Two Modified
2222
"""
23-
new_content = PatchTool.apply_patch(self.test_file, patch)
23+
new_content = EditTool.edit(self.test_file, edit)
2424
self.assertIn("Line Two Modified\n", new_content)
2525
self.assertNotIn("Line 2\n", new_content)
2626
self.assertEqual(new_content.splitlines()[1], "Line Two Modified")
2727

2828
def test_multi_line_hunk(self):
29-
patch = """--- test_file.txt
29+
edit = """--- test_file.txt
3030
+++ test_file.txt
3131
@@ -3,2 +3,3 @@
3232
-Line 3
@@ -35,15 +35,15 @@ def test_multi_line_hunk(self):
3535
+Line Three.Five
3636
+Line Four
3737
"""
38-
new_content = PatchTool.apply_patch(self.test_file, patch)
38+
new_content = EditTool.edit(self.test_file, edit)
3939
lines = new_content.splitlines()
4040
self.assertEqual(lines[2], "Line Three")
4141
self.assertEqual(lines[3], "Line Three.Five")
4242
self.assertEqual(lines[4], "Line Four")
4343
self.assertEqual(len(lines), 6)
4444

4545
def test_multiple_hunks(self):
46-
patch = """--- test_file.txt
46+
edit = """--- test_file.txt
4747
+++ test_file.txt
4848
@@ -1,1 +1,1 @@
4949
-Line 1
@@ -52,7 +52,7 @@ def test_multiple_hunks(self):
5252
-Line 5
5353
+Last Line
5454
"""
55-
new_content = PatchTool.apply_patch(self.test_file, patch)
55+
new_content = EditTool.edit(self.test_file, edit)
5656
lines = new_content.splitlines()
5757
self.assertEqual(lines[0], "First Line")
5858
self.assertEqual(lines[-1], "Last Line")

0 commit comments

Comments
 (0)