forked from OpenHands/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_git_hooks.py
More file actions
158 lines (126 loc) Β· 5.99 KB
/
test_git_hooks.py
File metadata and controls
158 lines (126 loc) Β· 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from unittest.mock import MagicMock, call
import pytest
from openhands.events.action import CmdRunAction, FileReadAction
from openhands.events.observation import (
CmdOutputObservation,
ErrorObservation,
FileReadObservation,
)
from openhands.runtime.base import Runtime
class TestGitHooks:
@pytest.fixture
def mock_runtime(self):
# Create a mock runtime
mock_runtime = MagicMock(spec=Runtime)
mock_runtime.status_callback = None
# Set up read to return different values based on the path
def mock_read(action):
if action.path == '.openhands/pre-commit.sh':
return FileReadObservation(
content="#!/bin/bash\necho 'Test pre-commit hook'\nexit 0",
path='.openhands/pre-commit.sh',
)
elif action.path == '.git/hooks/pre-commit':
# Simulate no existing pre-commit hook
return ErrorObservation(content='File not found')
return ErrorObservation(content='Unexpected path')
mock_runtime.read.side_effect = mock_read
mock_runtime.run_action.return_value = CmdOutputObservation(
content='', exit_code=0, command='test command'
)
mock_runtime.write.return_value = None
return mock_runtime
def test_maybe_setup_git_hooks_success(self, mock_runtime):
# Test successful setup of git hooks
Runtime.maybe_setup_git_hooks(mock_runtime)
# Verify that the runtime tried to read the pre-commit script
assert mock_runtime.read.call_args_list[0] == call(
FileReadAction(path='.openhands/pre-commit.sh')
)
# Verify that the runtime created the git hooks directory
# We can't directly compare the CmdRunAction objects, so we check if run_action was called
assert mock_runtime.run_action.called
# Verify that the runtime made the pre-commit script executable
# We can't directly compare the CmdRunAction objects, so we check if run_action was called
assert mock_runtime.run_action.called
# Verify that the runtime wrote the pre-commit hook
assert mock_runtime.write.called
# Verify that the runtime made the pre-commit hook executable
# We can't directly compare the CmdRunAction objects, so we check if run_action was called
assert mock_runtime.run_action.call_count >= 3
# Verify that the runtime logged success
mock_runtime.log.assert_called_with(
'info', 'Git pre-commit hook installed successfully'
)
def test_maybe_setup_git_hooks_no_script(self, mock_runtime):
# Test when pre-commit script doesn't exist
mock_runtime.read.side_effect = lambda action: ErrorObservation(
content='File not found'
)
Runtime.maybe_setup_git_hooks(mock_runtime)
# Verify that the runtime tried to read the pre-commit script
mock_runtime.read.assert_called_with(
FileReadAction(path='.openhands/pre-commit.sh')
)
# Verify that no other actions were taken
mock_runtime.run_action.assert_not_called()
mock_runtime.write.assert_not_called()
def test_maybe_setup_git_hooks_mkdir_failure(self, mock_runtime):
# Test failure to create git hooks directory
def mock_run_action(action):
if (
isinstance(action, CmdRunAction)
and action.command == 'mkdir -p .git/hooks'
):
return CmdOutputObservation(
content='Permission denied',
exit_code=1,
command='mkdir -p .git/hooks',
)
return CmdOutputObservation(content='', exit_code=0, command=action.command)
mock_runtime.run_action.side_effect = mock_run_action
Runtime.maybe_setup_git_hooks(mock_runtime)
# Verify that the runtime tried to create the git hooks directory
assert mock_runtime.run_action.called
# Verify that the runtime logged an error
mock_runtime.log.assert_called_with(
'error', 'Failed to create git hooks directory: Permission denied'
)
# Verify that no other actions were taken
mock_runtime.write.assert_not_called()
def test_maybe_setup_git_hooks_with_existing_hook(self, mock_runtime):
# Test when there's an existing pre-commit hook
def mock_read(action):
if action.path == '.openhands/pre-commit.sh':
return FileReadObservation(
content="#!/bin/bash\necho 'Test pre-commit hook'\nexit 0",
path='.openhands/pre-commit.sh',
)
elif action.path == '.git/hooks/pre-commit':
# Simulate existing pre-commit hook
return FileReadObservation(
content="#!/bin/bash\necho 'Existing hook'\nexit 0",
path='.git/hooks/pre-commit',
)
return ErrorObservation(content='Unexpected path')
mock_runtime.read.side_effect = mock_read
Runtime.maybe_setup_git_hooks(mock_runtime)
# Verify that the runtime tried to read both scripts
assert len(mock_runtime.read.call_args_list) >= 2
# Verify that the runtime preserved the existing hook
assert mock_runtime.log.call_args_list[0] == call(
'info', 'Preserving existing pre-commit hook'
)
# Verify that the runtime moved the existing hook
move_calls = [
call
for call in mock_runtime.run_action.call_args_list
if isinstance(call[0][0], CmdRunAction) and 'mv' in call[0][0].command
]
assert len(move_calls) > 0
# Verify that the runtime wrote the new pre-commit hook
assert mock_runtime.write.called
# Verify that the runtime logged success
assert mock_runtime.log.call_args_list[-1] == call(
'info', 'Git pre-commit hook installed successfully'
)