-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(patch): guard against None match in hunk header extraction #2330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gvago
wants to merge
3
commits into
The-PR-Agent:main
Choose a base branch
from
gvago:fix/hunk-header-parse-crash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+169
−8
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| """ | ||
| Tests for malformed @@ hunk headers in decouple_and_convert_to_hunks_with_lines_numbers(). | ||
|
|
||
| Verifies that: | ||
| 1. A malformed @@ line does not crash the function. | ||
| 2. Content from valid hunks before and after a malformed @@ is preserved. | ||
| 3. When a malformed @@ is the last @@-starting line, the preceding hunk is | ||
| still finalized correctly. | ||
| """ | ||
|
|
||
| from pr_agent.algo.git_patch_processing import decouple_and_convert_to_hunks_with_lines_numbers | ||
| from pr_agent.config_loader import get_settings | ||
|
|
||
| get_settings(use_context=False).set("CONFIG.CLI_MODE", True) | ||
|
|
||
|
|
||
| class _FakeFile: | ||
| """Minimal file object expected by the function under test.""" | ||
|
|
||
| def __init__(self, filename="test_file.py"): | ||
| self.filename = filename | ||
|
|
||
|
|
||
| class TestMalformedHunkHeader: | ||
| def test_malformed_hunk_does_not_crash(self): | ||
| """A patch whose only @@ line is malformed should not raise.""" | ||
| patch = "@@ THIS IS NOT A VALID HUNK HEADER @@\n+added line\n-removed line" | ||
| result = decouple_and_convert_to_hunks_with_lines_numbers(patch, _FakeFile()) | ||
| assert isinstance(result, str) | ||
|
|
||
| def test_valid_hunk_before_malformed_is_preserved(self): | ||
| """Content from a valid hunk that precedes a malformed @@ must appear in output.""" | ||
| patch = ( | ||
| "@@ -1,3 +1,4 @@ section\n" | ||
| " context\n" | ||
| "+added_line\n" | ||
| " more_context\n" | ||
| "@@ MALFORMED @@ not a real header\n" | ||
| ) | ||
| result = decouple_and_convert_to_hunks_with_lines_numbers(patch, _FakeFile()) | ||
| # The valid hunk's added line must be present | ||
| assert "+added_line" in result | ||
| assert "__new hunk__" in result | ||
|
|
||
| def test_malformed_hunk_between_two_valid_hunks(self): | ||
| """A malformed @@ between two valid hunks must not drop either hunk's content.""" | ||
| patch = ( | ||
| "@@ -1,3 +1,4 @@ first_section\n" | ||
| " ctx1\n" | ||
| "+add1\n" | ||
| " ctx2\n" | ||
| "@@ GARBAGE @@ not valid\n" | ||
| "@@ -10,2 +11,3 @@ second_section\n" | ||
| " ctx3\n" | ||
| "+add2\n" | ||
| ) | ||
| result = decouple_and_convert_to_hunks_with_lines_numbers(patch, _FakeFile()) | ||
| assert "+add1" in result, "First hunk content was dropped" | ||
| assert "+add2" in result, "Second hunk content was dropped" | ||
|
|
||
| def test_trailing_malformed_hunk_does_not_drop_last_valid(self): | ||
| """When a malformed @@ is the LAST @@-line, the previous hunk must still be finalized.""" | ||
| patch = ( | ||
| "@@ -5,3 +5,4 @@ my_func\n" | ||
| " existing_line\n" | ||
| "+new_line\n" | ||
| " another_existing\n" | ||
| "@@ INVALID HEADER\n" | ||
| ) | ||
| result = decouple_and_convert_to_hunks_with_lines_numbers(patch, _FakeFile()) | ||
| # The valid hunk before the malformed trailing @@ must be present | ||
| assert "+new_line" in result | ||
| assert "__new hunk__" in result | ||
| # The malformed header text should NOT appear as a hunk header in the output | ||
| assert "INVALID HEADER" not in result | ||
|
|
||
| def test_line_numbers_correct_with_malformed_between(self): | ||
| """Line numbers from valid hunks are correct even when a malformed @@ sits between them.""" | ||
| patch = ( | ||
| "@@ -1,2 +1,3 @@ header1\n" | ||
| " line_a\n" | ||
| "+line_b\n" | ||
| "@@ NOT_VALID\n" | ||
| "@@ -10,2 +11,3 @@ header2\n" | ||
| " line_c\n" | ||
| "+line_d\n" | ||
| ) | ||
| result = decouple_and_convert_to_hunks_with_lines_numbers(patch, _FakeFile()) | ||
| # First hunk starts at new-line 1: "1 line_a", "2 +line_b" | ||
| assert "1 line_a" in result | ||
| assert "2 +line_b" in result | ||
| # Second hunk starts at new-line 11: "11 line_c", "12 +line_d" | ||
| assert "11 line_c" in result | ||
| assert "12 +line_d" in result | ||
|
|
||
| def test_only_malformed_hunks_returns_file_header_only(self): | ||
| """A patch with ONLY malformed @@ lines should return just the file header.""" | ||
| patch = ( | ||
| "@@ BROKEN1 @@\n" | ||
| "+orphan_add\n" | ||
| "@@ BROKEN2 @@\n" | ||
| "-orphan_del\n" | ||
| ) | ||
| result = decouple_and_convert_to_hunks_with_lines_numbers(patch, _FakeFile()) | ||
| # No valid hunk was ever parsed, so no __new hunk__ / __old hunk__ sections | ||
| assert "__new hunk__" not in result | ||
| assert "__old hunk__" not in result | ||
|
|
||
| def test_deletion_only_hunk_before_malformed(self): | ||
| """A hunk with only deletions before a malformed @@ is still finalized.""" | ||
| patch = ( | ||
| "@@ -1,3 +1,2 @@ section\n" | ||
| " context\n" | ||
| "-removed_line\n" | ||
| "@@ NOT VALID @@\n" | ||
| ) | ||
| result = decouple_and_convert_to_hunks_with_lines_numbers(patch, _FakeFile()) | ||
| assert "-removed_line" in result | ||
| assert "__old hunk__" in result | ||
|
|
||
| def test_orphan_lines_after_malformed_not_joined_to_next_hunk(self): | ||
| """Orphan lines between a malformed @@ and the next valid @@ must be discarded. | ||
|
|
||
| Without clearing the buffers unconditionally on every @@ line, orphan | ||
| lines collected after a malformed @@ (where match=None) leak into the | ||
| next valid hunk because prev_match is None so the flush block is | ||
| skipped and the buffers are never reset. | ||
| """ | ||
| patch = ( | ||
| "@@ -1,3 +1,4 @@ first_section\n" | ||
| " ctx1\n" | ||
| "+add1\n" | ||
| " ctx2\n" | ||
| "@@ MALFORMED @@ not a real header\n" | ||
| "+orphan_line_should_be_discarded\n" | ||
| "-orphan_del_should_be_discarded\n" | ||
| "@@ -10,2 +11,3 @@ second_section\n" | ||
| " ctx3\n" | ||
| "+add2\n" | ||
| ) | ||
| result = decouple_and_convert_to_hunks_with_lines_numbers(patch, _FakeFile()) | ||
| # Valid hunk content must be present | ||
| assert "+add1" in result, "First hunk content was dropped" | ||
| assert "+add2" in result, "Second hunk content was dropped" | ||
| # Orphan lines must NOT appear in any hunk | ||
| assert "orphan_line_should_be_discarded" not in result, ( | ||
| "Orphan line after malformed @@ leaked into the next hunk" | ||
| ) | ||
| assert "orphan_del_should_be_discarded" not in result, ( | ||
| "Orphan deletion after malformed @@ leaked into the next hunk" | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Eof orphan lines leaked
🐞 Bug≡ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools