Skip to content

Commit 28dea88

Browse files
authored
Always add new line on modify (#961)
* add error logging on create pr's git push * add stderr * ensure newline on patch * fix tests
1 parent b7e9f04 commit 28dea88

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

patchwork/steps/ModifyCode/ModifyCode.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ def replace_code_in_file(
3939
new_code: str,
4040
) -> None:
4141
path = Path(file_path)
42+
new_code_lines = new_code.splitlines(keepends=True)
43+
if len(new_code_lines) > 0 and not new_code_lines[-1].endswith("\n"):
44+
new_code_lines[-1] += "\n"
45+
4246
if path.exists() and start_line is not None and end_line is not None:
4347
"""Replaces specified lines in a file with new code."""
4448
text = path.read_text()
4549

4650
lines = text.splitlines(keepends=True)
4751

4852
# Insert the new code at the start line after converting it into a list of lines
49-
lines[start_line:end_line] = handle_indent(lines, new_code.splitlines(keepends=True), start_line, end_line)
53+
lines[start_line:end_line] = handle_indent(lines, new_code_lines, start_line, end_line)
5054
else:
51-
lines = new_code.splitlines(keepends=True)
55+
lines = new_code_lines
5256

5357
# Save the modified contents back to the file
5458
save_file_contents(file_path, "".join(lines))

tests/steps/test_ModifyCode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_replace_code_in_file(tmp_path):
4040
end_line = 3
4141
new_code = "new line 1\nnew line 2"
4242
replace_code_in_file(str(file_path), start_line, end_line, new_code)
43-
assert file_path.read_text() == "line 1\nnew line 1\nnew line 2"
43+
assert file_path.read_text() == "line 1\nnew line 1\nnew line 2\n"
4444

4545

4646
def test_modify_code_init():

0 commit comments

Comments
 (0)