Skip to content

Commit

Permalink
Fix comment stripper and a few tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Domiii committed Dec 9, 2024
1 parent 50b3864 commit b1b6ece
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
26 changes: 13 additions & 13 deletions openhands/resolver/resolve_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,22 +134,20 @@ def strip_replay_comment(file_name: str, line: str):
f.writelines(lines)


def strip_replay_comments(git_patch: str) -> None:
REPLAY_COMMENT_PATTERN = r'^\+.*(?:\s+//|\{/\*.*?\*/\})'


def strip_replay_comments(base_path: str, git_patch: str) -> None:
"""Strip all replay comments from the git patch."""
logger.info('Stripping Replay comments...')

logger.info('Dumping contents of /workspace...')
for root, dirs, files in os.walk('/workspace'):
logger.info(f'Root: {root}, Dirs: {dirs}, Files: {files}')
logger.info('Done dumping contents of /workspace.')

lines = git_patch.splitlines()
current_file = ''
for line in lines:
logger.info(f'Processing line: {line}')
if re.match(r'^\+\+\+ b/', line):
current_file = f'/workspace/{line[6:]}'
if re.match(r'^\+.*\s+//', line.lstrip()):
current_file = os.path.join(base_path, line[6:])
if re.match(REPLAY_COMMENT_PATTERN, line.lstrip()):
strip_replay_comment(current_file, line[1:])


Expand Down Expand Up @@ -191,18 +189,20 @@ async def complete_runtime(
if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
raise RuntimeError(f'Failed to set git config. Observation: {obs}')

# Strip comments.
base_git_patch = await get_git_patch(runtime, base_commit)
assert runtime.config.workspace_base is not None
strip_replay_comments(runtime.config.workspace_base, base_git_patch)
git_patch = await get_git_patch(runtime, base_commit)

# git add.
action = CmdRunAction(command='git add -A')
logger.info(action, extra={'msg_type': 'ACTION'})
obs = runtime.run_action(action)
logger.info(obs, extra={'msg_type': 'OBSERVATION'})
if not isinstance(obs, CmdOutputObservation) or obs.exit_code != 0:
raise RuntimeError(f'Failed to git add. Observation: {obs}')

# base_git_patch = await get_git_patch(runtime, base_commit)
# strip_replay_comments(base_git_patch)

git_patch = await get_git_patch(runtime, base_commit)

logger.info('-' * 30)
logger.info('END Runtime Completion Fn')
logger.info('-' * 30)
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/resolver/test_resolve_with_replay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import re
from unittest.mock import mock_open, patch

import pytest

from openhands.resolver.resolve_issue import (
REPLAY_COMMENT_PATTERN,
strip_replay_comments,
)


def test_strip_replay_comments_basic():
git_patch = """
+++ b/test.cpp
+ int x = 5; // replay comment
+ int y = 10; {/* replay comment */}
"""

with patch('builtins.open', mock_open()):
with patch('os.path.exists') as mock_exists:
mock_exists.return_value = True
strip_replay_comments('/base', git_patch)


def test_strip_replay_comments_no_matches():
git_patch = """
+++ b/test.cpp
+ int x = 5;
+ int y = 10;
"""

with patch('builtins.open', mock_open()):
with patch('os.path.exists') as mock_exists:
mock_exists.return_value = True
strip_replay_comments('/base', git_patch)


def test_strip_replay_comments_multiple_files():
git_patch = """
+++ b/test1.cpp
+ int x = 5; // replay
+++ b/test2.cpp
+ int y = 10; {/* replay */}
"""

with patch('builtins.open', mock_open()):
with patch('os.path.exists') as mock_exists:
mock_exists.return_value = True
strip_replay_comments('/base', git_patch)


@pytest.mark.parametrize(
'line,expected',
[
('+ int x = 5; // replay', True),
('+ int x = 5; {/* replay */}', True),
('+ int x = 5;', False),
('- int x = 5; // replay', False),
],
)
def test_replay_comment_pattern(line, expected):
matches = bool(re.match(REPLAY_COMMENT_PATTERN, line.lstrip()))
assert (
matches == expected
), f'\nPattern: {REPLAY_COMMENT_PATTERN}\nLine: {line}\nExpected: {expected}\nGot: {matches}\nMatch object: {re.match(REPLAY_COMMENT_PATTERN, line.lstrip())}'

0 comments on commit b1b6ece

Please sign in to comment.