From e431979b8bf489fb2604b2474b4416eb622c7e84 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Sat, 4 Jan 2025 16:26:38 +0200 Subject: [PATCH 1/4] fix: handle newline issues in patch generation --- pr_agent/algo/utils.py | 2 ++ tests/unittest/test_extend_patch.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/pr_agent/algo/utils.py b/pr_agent/algo/utils.py index 9b49c0601..e38895686 100644 --- a/pr_agent/algo/utils.py +++ b/pr_agent/algo/utils.py @@ -588,6 +588,8 @@ def load_large_diff(filename, new_file_content_str: str, original_file_content_s return "" try: + original_file_content_str = original_file_content_str.rstrip() + "\n" + new_file_content_str = new_file_content_str.rstrip() + "\n" diff = difflib.unified_diff(original_file_content_str.splitlines(keepends=True), new_file_content_str.splitlines(keepends=True)) if get_settings().config.verbosity_level >= 2 and show_warning: diff --git a/tests/unittest/test_extend_patch.py b/tests/unittest/test_extend_patch.py index dfe4c5026..9f95f5f0c 100644 --- a/tests/unittest/test_extend_patch.py +++ b/tests/unittest/test_extend_patch.py @@ -3,6 +3,7 @@ from pr_agent.algo.git_patch_processing import extend_patch from pr_agent.algo.pr_processing import pr_generate_extended_diff from pr_agent.algo.token_handler import TokenHandler +from pr_agent.algo.utils import load_large_diff from pr_agent.config_loader import get_settings @@ -157,3 +158,28 @@ def test_extend_patches_with_extra_lines(self, token_handler, pr_languages): p0_extended = patches_extended_with_extra_lines[0].strip() assert p0_extended == "## File: 'file1'\n\n@@ -3,8 +3,8 @@ \n line0\n line1\n-original content\n+modified content\n line2\n line3\n line4\n line5\n line6" + + +class TestLoadLargeDiff: + def test_no_newline(self): + patch = load_large_diff("test.py", + """\ + old content 1 + some new content + another line + """, + """ + old content 1 + old content 2""") + + patch_expected="""\ +--- ++++ +@@ -1,3 +1,3 @@ +- + old content 1 +- old content 2 ++ some new content ++ another line +""" + assert patch == patch_expected \ No newline at end of file From 2df4bc8b53e49c1f1716d8fdb1931ef1e1ebfbbd Mon Sep 17 00:00:00 2001 From: Tal Date: Sat, 4 Jan 2025 16:28:25 +0200 Subject: [PATCH 2/4] Update pr_agent/algo/utils.py Co-authored-by: qodo-merge-pro-for-open-source[bot] <189517486+qodo-merge-pro-for-open-source[bot]@users.noreply.github.com> --- pr_agent/algo/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pr_agent/algo/utils.py b/pr_agent/algo/utils.py index e38895686..f7a7d686c 100644 --- a/pr_agent/algo/utils.py +++ b/pr_agent/algo/utils.py @@ -588,8 +588,8 @@ def load_large_diff(filename, new_file_content_str: str, original_file_content_s return "" try: - original_file_content_str = original_file_content_str.rstrip() + "\n" - new_file_content_str = new_file_content_str.rstrip() + "\n" + original_file_content_str = (original_file_content_str or "").rstrip() + "\n" + new_file_content_str = (new_file_content_str or "").rstrip() + "\n" diff = difflib.unified_diff(original_file_content_str.splitlines(keepends=True), new_file_content_str.splitlines(keepends=True)) if get_settings().config.verbosity_level >= 2 and show_warning: From d4d58babd5745041a4aa21ac78fd03f9835c3e01 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Sat, 4 Jan 2025 16:30:33 +0200 Subject: [PATCH 3/4] fix: handle empty inputs in patch generation --- tests/unittest/test_extend_patch.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/unittest/test_extend_patch.py b/tests/unittest/test_extend_patch.py index 9f95f5f0c..71537d485 100644 --- a/tests/unittest/test_extend_patch.py +++ b/tests/unittest/test_extend_patch.py @@ -182,4 +182,10 @@ def test_no_newline(self): + some new content + another line """ - assert patch == patch_expected \ No newline at end of file + assert patch == patch_expected + + def test_empty_inputs(self): + assert load_large_diff("test.py", "", "") == "" + assert load_large_diff("test.py", None, None) == "" + assert (load_large_diff("test.py", "content\n", "") == + '--- \n+++ \n@@ -1 +1 @@\n-\n+content\n') \ No newline at end of file From 793f76fec58a6a67ab5034241c8aafcb126cf056 Mon Sep 17 00:00:00 2001 From: mrT23 Date: Sat, 4 Jan 2025 16:34:35 +0200 Subject: [PATCH 4/4] fix: add missing newlines at the end of patch strings in tests --- tests/unittest/test_bitbucket_provider.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unittest/test_bitbucket_provider.py b/tests/unittest/test_bitbucket_provider.py index d883d55b0..27c9cb0e0 100644 --- a/tests/unittest/test_bitbucket_provider.py +++ b/tests/unittest/test_bitbucket_provider.py @@ -251,7 +251,7 @@ def test_get_diff_files_multi_merge_diverge_60(self): FilePatchInfo( 'file\nwith\nmultiple\nlines\nto\nemulate\na\nreal\nfile', 'readme\nwithout\nsome\nlines\nto\nsimulate\na\nreal\nfile', - '--- \n+++ \n@@ -1,9 +1,9 @@\n-file\n-with\n-multiple\n+readme\n+without\n+some\n lines\n to\n-emulate\n+simulate\n a\n real\n file', + '--- \n+++ \n@@ -1,9 +1,9 @@\n-file\n-with\n-multiple\n+readme\n+without\n+some\n lines\n to\n-emulate\n+simulate\n a\n real\n file\n', 'Readme.md', edit_type=EDIT_TYPE.MODIFIED, ) @@ -273,7 +273,7 @@ def test_get_diff_files_multi_merge_diverge_70(self): FilePatchInfo( 'file\nwith\nsome\nlines\nto\nemulate\na\nreal\nfile', 'readme\nwithout\nsome\nlines\nto\nsimulate\na\nreal\nfile', - '--- \n+++ \n@@ -1,9 +1,9 @@\n-file\n-with\n+readme\n+without\n some\n lines\n to\n-emulate\n+simulate\n a\n real\n file', + '--- \n+++ \n@@ -1,9 +1,9 @@\n-file\n-with\n+readme\n+without\n some\n lines\n to\n-emulate\n+simulate\n a\n real\n file\n', 'Readme.md', edit_type=EDIT_TYPE.MODIFIED, ) @@ -295,7 +295,7 @@ def test_get_diff_files_multi_merge_diverge_816(self): FilePatchInfo( 'file\nwith\nsome\nlines\nto\nemulate\na\nreal\nfile', 'readme\nwithout\nsome\nlines\nto\nsimulate\na\nreal\nfile', - '--- \n+++ \n@@ -1,9 +1,9 @@\n-file\n-with\n+readme\n+without\n some\n lines\n to\n-emulate\n+simulate\n a\n real\n file', + '--- \n+++ \n@@ -1,9 +1,9 @@\n-file\n-with\n+readme\n+without\n some\n lines\n to\n-emulate\n+simulate\n a\n real\n file\n', 'Readme.md', edit_type=EDIT_TYPE.MODIFIED, )