-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_git.py
More file actions
243 lines (202 loc) · 10.1 KB
/
test_git.py
File metadata and controls
243 lines (202 loc) · 10.1 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import unittest
from pathlib import Path
from unittest.mock import MagicMock, mock_open, patch
import git
class TestMiniGitCLI(unittest.TestCase):
def test_object_exists_true(self):
with patch("git.Path.is_file", return_value=True):
result = git.object_exists("abc123", Path("."))
self.assertTrue(result)
def test_object_exists_false(self):
with patch("git.Path.is_file", return_value=False):
result = git.object_exists("abc123", Path("."))
self.assertFalse(result)
def test_hash_object_file_not_found(self):
with patch.object(git.logger, "error"):
result = git.hash_object("nonexistent.txt", Path("."), False)
self.assertEqual(result, (None, None, None))
def test_hash_object_success(self):
with patch("builtins.open", mock_open(read_data=b"test content")):
sha1, content, full_content = git.hash_object("test.txt", Path("."), False)
self.assertIsNotNone(sha1)
self.assertEqual(content, b"test content")
self.assertIn(b"blob", full_content)
def test_initialize_git_repository(self):
with patch("git.Path.mkdir") as mock_mkdir, patch("builtins.open", mock_open()) as mock_file, patch.object(
git.logger, "info"
):
git.initialize_git_repository()
mock_mkdir.assert_called()
mock_file.assert_called()
def test_read_git_object_not_found(self):
with patch("git.Path.is_file", return_value=False):
result = git.read_git_object("abc123", Path("."))
self.assertEqual(result, (None, None, None))
def test_read_index_empty(self):
with patch("git.Path.exists", return_value=False):
result = git.read_index(Path(".git/index"))
self.assertEqual(result, [])
def test_get_obj_type_str(self):
self.assertEqual(git.get_obj_type_str(1), "commit")
self.assertEqual(git.get_obj_type_str(2), "tree")
self.assertEqual(git.get_obj_type_str(3), "blob")
self.assertEqual(git.get_obj_type_str(4), "tag")
self.assertEqual(git.get_obj_type_str(99), "unknown")
def test_find_repo_root_none(self):
with patch("git.Path.is_dir", return_value=False):
result = git.find_repo_root()
self.assertIsNone(result)
def test_find_repo_root_found(self):
with patch("git.Path.is_dir", return_value=True):
result = git.find_repo_root()
self.assertIsNotNone(result)
def test_git_status_clean(self):
with patch("git.read_index", return_value=[]), patch("git.os.walk", return_value=[(".", [], [])]), patch.object(
git.logger, "info"
) as mock_log:
git.git_status(Path("."))
mock_log.assert_any_call("Nothing to commit, working tree clean")
def test_get_author_info_defaults(self):
with patch("git.os.getenv", return_value=None), patch("git.Path.is_file", return_value=False):
name, email = git.get_author_info(Path("."))
self.assertEqual(name, "Unknown User")
self.assertEqual(email, "unknown.email@example.com")
def test_get_author_info_env_vars(self):
with patch(
"git.os.getenv", side_effect=lambda x: "Test User" if x == "GIT_AUTHOR_NAME" else "test@example.com"
):
name, email = git.get_author_info(Path("."))
self.assertEqual(name, "Test User")
self.assertEqual(email, "test@example.com")
def test_find_head_commit_main(self):
refs_text = "0044prefixabc123def456789012345678901234567890 refs/heads/main\n"
result = git.find_head_commit(refs_text)
self.assertEqual(result, "efixabc123def456789012345678901234567890")
def test_find_head_commit_master(self):
refs_text = "0044prefixdef456789012345678901234567890abcdef refs/heads/master\n"
result = git.find_head_commit(refs_text)
self.assertEqual(result, "efixdef456789012345678901234567890abcdef")
def test_find_head_commit_none(self):
refs_text = "001e# service=git-upload-pack\n0000"
result = git.find_head_commit(refs_text)
self.assertIsNone(result)
def test_get_credentials(self):
with patch("builtins.input", return_value="testuser"), patch("git.getpass.getpass", return_value="testpass"):
username, password = git.get_credentials()
self.assertEqual(username, "testuser")
self.assertEqual(password, "testpass")
def test_get_current_branch(self):
with patch("git.Path.is_file", return_value=True), patch(
"builtins.open", mock_open(read_data="ref: refs/heads/main\n")
):
result = git.get_current_branch(Path("."))
self.assertEqual(result, "main")
def test_get_current_branch_detached(self):
with patch("git.Path.is_file", return_value=True), patch(
"builtins.open", mock_open(read_data="abc123def456\n")
):
result = git.get_current_branch(Path("."))
self.assertEqual(result, "detached HEAD")
def test_get_current_branch_no_head(self):
with patch("git.Path.is_file", return_value=False):
result = git.get_current_branch(Path("."))
self.assertIsNone(result)
def test_read_git_config_not_found(self):
with patch("git.Path.is_file", return_value=False):
result = git.read_git_config(Path("."))
self.assertIsNone(result)
def test_read_git_config_found(self):
with patch("git.Path.is_file", return_value=True), patch("git.ConfigParser.read"):
result = git.read_git_config(Path("."))
self.assertIsNotNone(result)
def test_process_files_single_file(self):
with patch("git.Path.is_file", return_value=True), patch.object(git.logger, "info"):
result = git.process_files("test.txt")
self.assertIsNotNone(result)
self.assertEqual(len(result), 1)
def test_process_files_dot(self):
mock_files = [Path("file1.txt"), Path("file2.txt")]
with patch.object(Path, "glob", return_value=mock_files), patch.object(git.logger, "info"), patch.object(
Path, "is_file", return_value=True
):
result = git.process_files(".")
self.assertIsNotNone(result)
self.assertEqual(len(result), 2)
def test_ls_remote_success(self):
mock_response = MagicMock()
mock_response.read.return_value = (
b"abc123def456789012345678901234567890 refs/heads/main\n"
b"def456789012345678901234567890abcdef refs/heads/master\n"
)
mock_response.__enter__ = MagicMock(return_value=mock_response)
mock_response.__exit__ = MagicMock(return_value=None)
with patch("git.urllib.request.urlopen", return_value=mock_response), patch.object(git.logger, "info"), patch(
"git.re.findall",
return_value=[
("abc123def456789012345678901234567890", "refs/heads/main"),
("def456789012345678901234567890abcdef", "refs/heads/master"),
],
):
result = git.ls_remote("https://github.com/user/repo.git")
self.assertIsNotNone(result)
self.assertIn("refs/heads/main", result)
def test_ls_remote_http_error(self):
with patch(
"git.urllib.request.urlopen", side_effect=git.urllib.error.HTTPError("url", 404, "Not Found", {}, None)
), patch.object(git.logger, "error"):
result = git.ls_remote("https://github.com/user/repo.git")
self.assertIsNone(result)
def test_read_header(self):
data = b"\x15" # type=1, size=5 (0001 0101)
obj_type, size, cursor = git.read_header(data, 0)
self.assertEqual(obj_type, 1)
self.assertEqual(size, 5)
self.assertEqual(cursor, 1)
def test_read_offset(self):
data = b"\x05" # offset=5
bytes_read, offset = git.read_offset(data, 0)
self.assertEqual(bytes_read, 1)
self.assertEqual(offset, 5)
def test_read_delta_size(self):
data = b"\x05" # size=5
size, cursor = git.read_delta_size(data, 0)
self.assertEqual(size, 5)
self.assertEqual(cursor, 1)
def test_apply_delta_simple(self):
base_data = b"hello world"
delta_data = b"\x0b\x05\x05hello" # source_size=11, dest_size=5, insert 5 bytes 'hello'
result = git.apply_delta(base_data, delta_data)
self.assertEqual(result, b"hello")
def test_cat_file_content(self):
with patch("git.read_git_object", return_value=(b"test content", b"blob", 12)), patch(
"builtins.print"
) as mock_print:
git.cat_file("p", "abc123", Path("."))
mock_print.assert_called_with("test content", end="")
def test_cat_file_type(self):
with patch("git.read_git_object", return_value=(b"test content", b"blob", 12)), patch.object(
git.logger, "info"
) as mock_log:
git.cat_file("t", "abc123", Path("."))
mock_log.assert_called_with("blob")
def test_cat_file_not_found(self):
with patch("git.read_git_object", return_value=(None, None, None)), patch.object(
git.logger, "error"
) as mock_log:
git.cat_file("p", "abc123", Path("."))
mock_log.assert_called_with("Error: Object 'abc123' not found.")
def test_ls_files(self):
entries = [{"path": "file1.txt"}, {"path": "file2.txt"}]
with patch("git.read_index", return_value=entries), patch.object(git.logger, "info") as mock_log:
git.ls_files(Path("."))
mock_log.assert_any_call("file1.txt")
mock_log.assert_any_call("file2.txt")
def test_write_tree_command(self):
with patch("git.read_index", return_value=[]), patch("git.write_tree", return_value="abc123"), patch.object(
git.logger, "info"
) as mock_log:
result = git.write_tree_command(Path("."))
self.assertEqual(result, "abc123")
mock_log.assert_called_with("abc123")
if __name__ == "__main__":
unittest.main()