Compare commits

...

2 Commits

Author SHA1 Message Date
Graham Neubig
985df5e24c Merge branch 'main' into fix-git-tests-environment-independence 2025-08-18 10:12:10 -04:00
openhands
2bbc37d552 Fix git-related test failures by making tests environment-independent 2025-08-06 01:33:00 +00:00
3 changed files with 133 additions and 29 deletions

View File

@@ -12,6 +12,15 @@ def test_commit_message_with_quotes():
with tempfile.TemporaryDirectory() as temp_dir:
subprocess.run(['git', 'init', temp_dir], check=True)
# Set git configuration locally for this repository
subprocess.run(
['git', '-C', temp_dir, 'config', 'user.email', 'test@example.com'],
check=True,
)
subprocess.run(
['git', '-C', temp_dir, 'config', 'user.name', 'Test User'], check=True
)
# Create a test file and add it to git
test_file = os.path.join(temp_dir, 'test.txt')
with open(test_file, 'w') as f:
@@ -36,8 +45,20 @@ def test_commit_message_with_quotes():
thread_ids=None,
)
# Make the commit
make_commit(temp_dir, issue, 'issue')
# Make the commit with author information
# First, modify the environment to include GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL
old_environ = os.environ.copy()
os.environ['GIT_AUTHOR_NAME'] = 'Test User'
os.environ['GIT_AUTHOR_EMAIL'] = 'test@example.com'
os.environ['GIT_COMMITTER_NAME'] = 'Test User'
os.environ['GIT_COMMITTER_EMAIL'] = 'test@example.com'
try:
make_commit(temp_dir, issue, 'issue')
finally:
# Restore the original environment
os.environ.clear()
os.environ.update(old_environ)
# Get the commit message
result = subprocess.run(
@@ -100,6 +121,14 @@ def test_pr_title_with_quotes(monkeypatch):
def mock_run(*args, **kwargs):
print(f'Running command: {args[0] if args else kwargs.get("args", [])}')
if isinstance(args[0], list) and args[0][0] == 'git':
# Set git environment variables for all git commands
env = kwargs.get('env', os.environ.copy())
env['GIT_AUTHOR_NAME'] = 'Test User'
env['GIT_AUTHOR_EMAIL'] = 'test@example.com'
env['GIT_COMMITTER_NAME'] = 'Test User'
env['GIT_COMMITTER_EMAIL'] = 'test@example.com'
kwargs['env'] = env
if 'push' in args[0]:
return subprocess.CompletedProcess(
args[0], returncode=0, stdout='', stderr=''
@@ -156,11 +185,23 @@ def test_pr_title_with_quotes(monkeypatch):
print('Sending PR...')
from openhands.resolver.send_pull_request import send_pull_request
send_pull_request(
issue=issue,
token='dummy-token',
username='test-user',
platform=ProviderType.GITHUB,
patch_dir=temp_dir,
pr_type='ready',
)
# Set git environment variables
old_environ = os.environ.copy()
os.environ['GIT_AUTHOR_NAME'] = 'Test User'
os.environ['GIT_AUTHOR_EMAIL'] = 'test@example.com'
os.environ['GIT_COMMITTER_NAME'] = 'Test User'
os.environ['GIT_COMMITTER_EMAIL'] = 'test@example.com'
try:
send_pull_request(
issue=issue,
token='dummy-token',
username='test-user',
platform=ProviderType.GITHUB,
patch_dir=temp_dir,
pr_type='ready',
)
finally:
# Restore the original environment
os.environ.clear()
os.environ.update(old_environ)

View File

@@ -13,6 +13,15 @@ def test_commit_message_with_quotes():
with tempfile.TemporaryDirectory() as temp_dir:
subprocess.run(['git', 'init', temp_dir], check=True)
# Set git configuration locally for this repository
subprocess.run(
['git', '-C', temp_dir, 'config', 'user.email', 'test@example.com'],
check=True,
)
subprocess.run(
['git', '-C', temp_dir, 'config', 'user.name', 'Test User'], check=True
)
# Create a test file and add it to git
test_file = os.path.join(temp_dir, 'test.txt')
with open(test_file, 'w') as f:
@@ -37,8 +46,20 @@ def test_commit_message_with_quotes():
thread_ids=None,
)
# Make the commit
make_commit(temp_dir, issue, 'issue')
# Make the commit with author information
# First, modify the environment to include GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL
old_environ = os.environ.copy()
os.environ['GIT_AUTHOR_NAME'] = 'Test User'
os.environ['GIT_AUTHOR_EMAIL'] = 'test@example.com'
os.environ['GIT_COMMITTER_NAME'] = 'Test User'
os.environ['GIT_COMMITTER_EMAIL'] = 'test@example.com'
try:
make_commit(temp_dir, issue, 'issue')
finally:
# Restore the original environment
os.environ.clear()
os.environ.update(old_environ)
# Get the commit message
result = subprocess.run(
@@ -101,6 +122,14 @@ def test_pr_title_with_quotes(monkeypatch):
def mock_run(*args, **kwargs):
logger.info(f'Running command: {args[0] if args else kwargs.get("args", [])}')
if isinstance(args[0], list) and args[0][0] == 'git':
# Set git environment variables for all git commands
env = kwargs.get('env', os.environ.copy())
env['GIT_AUTHOR_NAME'] = 'Test User'
env['GIT_AUTHOR_EMAIL'] = 'test@example.com'
env['GIT_COMMITTER_NAME'] = 'Test User'
env['GIT_COMMITTER_EMAIL'] = 'test@example.com'
kwargs['env'] = env
if 'push' in args[0]:
return subprocess.CompletedProcess(
args[0], returncode=0, stdout='', stderr=''
@@ -156,11 +185,23 @@ def test_pr_title_with_quotes(monkeypatch):
# Try to send a PR - this will fail if the title is incorrectly escaped
logger.info('Sending PR...')
send_pull_request(
issue=issue,
token='dummy-token',
username='test-user',
platform=ProviderType.GITHUB,
patch_dir=temp_dir,
pr_type='ready',
)
# Set git environment variables
old_environ = os.environ.copy()
os.environ['GIT_AUTHOR_NAME'] = 'Test User'
os.environ['GIT_AUTHOR_EMAIL'] = 'test@example.com'
os.environ['GIT_COMMITTER_NAME'] = 'Test User'
os.environ['GIT_COMMITTER_EMAIL'] = 'test@example.com'
try:
send_pull_request(
issue=issue,
token='dummy-token',
username='test-user',
platform=ProviderType.GITHUB,
patch_dir=temp_dir,
pr_type='ready',
)
finally:
# Restore the original environment
os.environ.clear()
os.environ.update(old_environ)

View File

@@ -47,12 +47,21 @@ class TestGitHandler(unittest.TestCase):
def _execute_command(self, cmd, cwd=None):
"""Execute a shell command and return the result."""
# For git commit commands, add the author and committer information directly to the environment
env = os.environ.copy()
if cmd.startswith('git') and 'commit' in cmd:
env['GIT_AUTHOR_NAME'] = 'Test User'
env['GIT_AUTHOR_EMAIL'] = 'test@example.com'
env['GIT_COMMITTER_NAME'] = 'Test User'
env['GIT_COMMITTER_EMAIL'] = 'test@example.com'
result = subprocess.run(
args=cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=cwd,
env=env,
)
stderr = result.stderr or b''
stdout = result.stdout or b''
@@ -92,10 +101,13 @@ class TestGitHandler(unittest.TestCase):
"""Set up real git repositories for testing."""
# Set up origin repository
self.run_command('git init --initial-branch=main', self.origin_dir)
self._execute_command(
"git config user.email 'test@example.com'", self.origin_dir
# Set git configuration locally for this repository using a specific config file
self.run_command(
"git config -f .git/config user.email 'test@example.com'", self.origin_dir
)
self.run_command(
"git config -f .git/config user.name 'Test User'", self.origin_dir
)
self._execute_command("git config user.name 'Test User'", self.origin_dir)
# Set up the initial state...
self.write_file(self.origin_dir, 'unchanged.txt')
@@ -111,9 +123,11 @@ class TestGitHandler(unittest.TestCase):
self.run_command(f'git clone "{self.origin_dir}" "{self.local_dir}"')
self._execute_command(
"git config user.email 'test@example.com'", self.local_dir
"git config -f .git/config user.email 'test@example.com'", self.local_dir
)
self._execute_command(
"git config -f .git/config user.name 'Test User'", self.local_dir
)
self._execute_command("git config user.name 'Test User'", self.local_dir)
self.run_command('git checkout -b feature-branch', self.local_dir)
@@ -141,8 +155,12 @@ class TestGitHandler(unittest.TestCase):
nested_1.mkdir()
nested_1 = str(nested_1)
self.run_command('git init --initial-branch=main', nested_1)
self._execute_command("git config user.email 'test@example.com'", nested_1)
self._execute_command("git config user.name 'Test User'", nested_1)
self._execute_command(
"git config -f .git/config user.email 'test@example.com'", nested_1
)
self._execute_command(
"git config -f .git/config user.name 'Test User'", nested_1
)
self.write_file(nested_1, 'committed_add.txt')
self.run_command('git add .', nested_1)
self.run_command('git commit -m "Initial Commit"', nested_1)
@@ -152,8 +170,12 @@ class TestGitHandler(unittest.TestCase):
nested_2.mkdir()
nested_2 = str(nested_2)
self.run_command('git init --initial-branch=main', nested_2)
self._execute_command("git config user.email 'test@example.com'", nested_2)
self._execute_command("git config user.name 'Test User'", nested_2)
self._execute_command(
"git config -f .git/config user.email 'test@example.com'", nested_2
)
self._execute_command(
"git config -f .git/config user.name 'Test User'", nested_2
)
self.write_file(nested_2, 'committed_add.txt')
self.run_command('git add .', nested_2)
self.run_command('git commit -m "Initial Commit"', nested_2)