From bf3ea9db38ddd5eaf0e4cd7940b7b23f1ff55d0e Mon Sep 17 00:00:00 2001 From: Konstantinos Date: Fri, 21 Mar 2025 14:54:18 +0100 Subject: [PATCH 1/2] Added regression test for issue #4643 --- tests/test_code_review.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/test_code_review.py b/tests/test_code_review.py index 173a9c253a..beb6347f31 100644 --- a/tests/test_code_review.py +++ b/tests/test_code_review.py @@ -55,3 +55,41 @@ def test_find_comment_scope(): for line_number, expected_scope in target_hunks.items(): assert find_comment_scope(patched_file, line_number) == expected_scope + +def test_generate_processed_output_attaches_comment_to_correct_line(): + # Regression test for https://github.com/mozilla/bugbug/issues/4643 + from bugbug.tools.code_review import generate_processed_output + from unidiff import PatchSet + from bugbug.tools.code_review import InlineComment + import json + + # Mock output from the model + output = json.dumps([ + { + "file": "example.py", + "code_line": 10, + "comment": "This is a test comment." + } + ]) + + # Create a mock patch set + patch_content = """\ +diff --git a/example.py b/example.py +--- a/example.py ++++ b/example.py +@@ -8,0 +9,2 @@ ++def example_function(): ++ pass +""" + patch = PatchSet(patch_content) + + # Generate processed output + comments = list(generate_processed_output(output, patch)) + + # Check that the comment is attached to the correct line + assert len(comments) == 1 + assert isinstance(comments[0], InlineComment) + assert comments[0].filename == "example.py" + assert comments[0].start_line == 10 + assert comments[0].end_line == 10 + assert comments[0].content == "This is a test comment." \ No newline at end of file From 17f3783f7e45cb5f8e9cc45ed2f3027d62ea54f3 Mon Sep 17 00:00:00 2001 From: Konstantinos Date: Fri, 21 Mar 2025 17:51:02 +0100 Subject: [PATCH 2/2] Formatting changes by ruff --- tests/test_code_review.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tests/test_code_review.py b/tests/test_code_review.py index beb6347f31..8b8d05816f 100644 --- a/tests/test_code_review.py +++ b/tests/test_code_review.py @@ -56,21 +56,19 @@ def test_find_comment_scope(): for line_number, expected_scope in target_hunks.items(): assert find_comment_scope(patched_file, line_number) == expected_scope + def test_generate_processed_output_attaches_comment_to_correct_line(): # Regression test for https://github.com/mozilla/bugbug/issues/4643 - from bugbug.tools.code_review import generate_processed_output - from unidiff import PatchSet - from bugbug.tools.code_review import InlineComment import json + from unidiff import PatchSet + + from bugbug.tools.code_review import InlineComment, generate_processed_output + # Mock output from the model - output = json.dumps([ - { - "file": "example.py", - "code_line": 10, - "comment": "This is a test comment." - } - ]) + output = json.dumps( + [{"file": "example.py", "code_line": 10, "comment": "This is a test comment."}] + ) # Create a mock patch set patch_content = """\ @@ -92,4 +90,4 @@ def test_generate_processed_output_attaches_comment_to_correct_line(): assert comments[0].filename == "example.py" assert comments[0].start_line == 10 assert comments[0].end_line == 10 - assert comments[0].content == "This is a test comment." \ No newline at end of file + assert comments[0].content == "This is a test comment."