Skip to content

Commit

Permalink
Always add new line on modify (#961)
Browse files Browse the repository at this point in the history
* add error logging on create pr's git push

* add stderr

* ensure newline on patch

* fix tests
  • Loading branch information
CTY-git authored Oct 22, 2024
1 parent b7e9f04 commit 28dea88
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
8 changes: 6 additions & 2 deletions patchwork/steps/ModifyCode/ModifyCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@ def replace_code_in_file(
new_code: str,
) -> None:
path = Path(file_path)
new_code_lines = new_code.splitlines(keepends=True)
if len(new_code_lines) > 0 and not new_code_lines[-1].endswith("\n"):
new_code_lines[-1] += "\n"

if path.exists() and start_line is not None and end_line is not None:
"""Replaces specified lines in a file with new code."""
text = path.read_text()

lines = text.splitlines(keepends=True)

# Insert the new code at the start line after converting it into a list of lines
lines[start_line:end_line] = handle_indent(lines, new_code.splitlines(keepends=True), start_line, end_line)
lines[start_line:end_line] = handle_indent(lines, new_code_lines, start_line, end_line)
else:
lines = new_code.splitlines(keepends=True)
lines = new_code_lines

# Save the modified contents back to the file
save_file_contents(file_path, "".join(lines))
Expand Down
2 changes: 1 addition & 1 deletion tests/steps/test_ModifyCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_replace_code_in_file(tmp_path):
end_line = 3
new_code = "new line 1\nnew line 2"
replace_code_in_file(str(file_path), start_line, end_line, new_code)
assert file_path.read_text() == "line 1\nnew line 1\nnew line 2"
assert file_path.read_text() == "line 1\nnew line 1\nnew line 2\n"


def test_modify_code_init():
Expand Down

0 comments on commit 28dea88

Please sign in to comment.