17
17
18
18
import pytest
19
19
20
- from src .helper_lib import get_current_git_hash , get_github_repo_info
20
+ from src .helper_lib import (
21
+ get_current_git_hash ,
22
+ get_github_repo_info ,
23
+ parse_remote_git_output ,
24
+ )
21
25
22
26
23
27
@pytest .fixture
@@ -27,6 +31,103 @@ def temp_dir():
27
31
yield Path (temp_dir )
28
32
29
33
34
+ @pytest .fixture
35
+ def git_repo (temp_dir ):
36
+ """Create a real git repository for testing."""
37
+ git_dir = temp_dir / "test_repo"
38
+ git_dir .mkdir ()
39
+
40
+ # Initialize git repo
41
+ subprocess .run (["git" , "init" ], cwd = git_dir , check = True , capture_output = True )
42
+ subprocess .run (
43
+ [
"git" ,
"config" ,
"user.email" ,
"[email protected] " ],
cwd = git_dir ,
check = True
44
+ )
45
+ subprocess .run (["git" , "config" , "user.name" , "Test User" ], cwd = git_dir , check = True )
46
+
47
+ # Create a test file and commit
48
+ test_file = git_dir / "test_file.py"
49
+ test_file .write_text ("# Test file\n print('hello')\n " )
50
+ subprocess .run (["git" , "add" , "." ], cwd = git_dir , check = True )
51
+ subprocess .run (["git" , "commit" , "-m" , "Initial commit" ], cwd = git_dir , check = True )
52
+
53
+ # Add a remote
54
+ subprocess .run (
55
+ [
"git" ,
"remote" ,
"add" ,
"origin" ,
"[email protected] :test-user/test-repo.git" ],
56
+ cwd = git_dir ,
57
+ check = True ,
58
+ )
59
+ return git_dir
60
+
61
+
62
+ @pytest .fixture
63
+ def git_repo_multiple_remotes (temp_dir ):
64
+ """Create a git repository with multiple remotes for testing."""
65
+ git_dir = temp_dir / "test_repo_multiple"
66
+ git_dir .mkdir ()
67
+
68
+ # Initialize git repo
69
+ subprocess .run (["git" , "init" ], cwd = git_dir , check = True , capture_output = True )
70
+ subprocess .run (
71
+ [
"git" ,
"config" ,
"user.email" ,
"[email protected] " ],
cwd = git_dir ,
check = True
72
+ )
73
+ subprocess .run (["git" , "config" , "user.name" , "Test User" ], cwd = git_dir , check = True )
74
+
75
+ # Create a test file and commit
76
+ test_file = git_dir / "test_file.py"
77
+ test_file .write_text ("# Test file\n print('hello')\n " )
78
+ subprocess .run (["git" , "add" , "." ], cwd = git_dir , check = True )
79
+ subprocess .run (["git" , "commit" , "-m" , "Initial commit" ], cwd = git_dir , check = True )
80
+
81
+ # Add multiple remotes
82
+ subprocess .run (
83
+ [
"git" ,
"remote" ,
"add" ,
"upstream" ,
"[email protected] :upstream/test-repo.git" ],
84
+ cwd = git_dir ,
85
+ check = True ,
86
+ )
87
+ subprocess .run (
88
+ [
"git" ,
"remote" ,
"add" ,
"origin" ,
"[email protected] :test-user/test-repo.git" ],
89
+ cwd = git_dir ,
90
+ check = True ,
91
+ )
92
+
93
+ return git_dir
94
+
95
+
96
+ @pytest .fixture
97
+ def git_repo_with_https_remote (temp_dir ):
98
+ """Create a git repository with HTTPS remote for testing."""
99
+ git_dir = temp_dir / "test_repo_https"
100
+ git_dir .mkdir ()
101
+
102
+ # Initialize git repo
103
+ subprocess .run (["git" , "init" ], cwd = git_dir , check = True , capture_output = True )
104
+ subprocess .run (
105
+ [
"git" ,
"config" ,
"user.email" ,
"[email protected] " ],
cwd = git_dir ,
check = True
106
+ )
107
+ subprocess .run (["git" , "config" , "user.name" , "Test User" ], cwd = git_dir , check = True )
108
+
109
+ # Create a test file and commit
110
+ test_file = git_dir / "test_file.py"
111
+ test_file .write_text ("# Test file\n print('hello')\n " )
112
+ subprocess .run (["git" , "add" , "." ], cwd = git_dir , check = True )
113
+ subprocess .run (["git" , "commit" , "-m" , "Initial commit" ], cwd = git_dir , check = True )
114
+
115
+ # Add HTTPS remote
116
+ subprocess .run (
117
+ [
118
+ "git" ,
119
+ "remote" ,
120
+ "add" ,
121
+ "origin" ,
122
+ "https://github.com/test-user/test-repo.git" ,
123
+ ],
124
+ cwd = git_dir ,
125
+ check = True ,
126
+ )
127
+
128
+ return git_dir
129
+
130
+
30
131
# Test error handling
31
132
def test_git_operations_with_no_commits (temp_dir ):
32
133
"""Test git operations on repo with no commits."""
@@ -68,3 +169,72 @@ def test_git_repo_with_no_remotes(temp_dir):
68
169
# Should raise an exception when trying to get repo info
69
170
with pytest .raises (AssertionError ):
70
171
get_github_repo_info (git_dir )
172
+
173
+
174
+ # Test git-related functions
175
+ def test_parse_git_output_ssh_format ():
176
+ """Test parsing git remote output in SSH format."""
177
+ git_line = "origin [email protected] :test-user/test-repo.git (fetch)"
178
+ result = parse_remote_git_output (git_line )
179
+ assert result == "test-user/test-repo"
180
+
181
+
182
+ def test_parse_git_output_https_format ():
183
+ """Test parsing git remote output in HTTPS format."""
184
+ git_line = "origin https://github.com/test-user/test-repo.git (fetch)"
185
+ result = parse_remote_git_output (git_line )
186
+ assert result == "test-user/test-repo"
187
+
188
+
189
+ def test_parse_git_output_ssh_format_without_git_suffix ():
190
+ """Test parsing git remote output in SSH format without .git suffix."""
191
+ git_line = "origin [email protected] :test-user/test-repo (fetch)"
192
+ result = parse_remote_git_output (git_line )
193
+ assert result == "test-user/test-repo"
194
+
195
+
196
+ def test_parse_git_output_invalid_format ():
197
+ """Test parsing invalid git remote output."""
198
+ git_line = "invalid"
199
+ result = parse_remote_git_output (git_line )
200
+ assert result == ""
201
+
202
+
203
+ def test_parse_git_output_empty_string ():
204
+ """Test parsing empty git remote output."""
205
+ git_line = ""
206
+ result = parse_remote_git_output (git_line )
207
+ assert result == ""
208
+
209
+
210
+ def test_get_github_repo_info_ssh_remote (git_repo ):
211
+ """Test getting GitHub repository information with SSH remote."""
212
+ result = get_github_repo_info (git_repo )
213
+ assert result == "test-user/test-repo"
214
+
215
+
216
+ def test_get_github_repo_info_https_remote (git_repo_with_https_remote ):
217
+ """Test getting GitHub repository information with HTTPS remote."""
218
+ result = get_github_repo_info (git_repo_with_https_remote )
219
+ assert result == "test-user/test-repo"
220
+
221
+
222
+ def test_get_github_repo_info_multiple_remotes (git_repo_multiple_remotes ):
223
+ """Test GitHub repo info retrieval with multiple remotes (origin preferred)."""
224
+ result = get_github_repo_info (git_repo_multiple_remotes )
225
+ assert result == "test-user/test-repo"
226
+
227
+
228
+ def test_get_current_git_hash (git_repo ):
229
+ """Test getting current git hash."""
230
+ result = get_current_git_hash (git_repo )
231
+
232
+ # Verify it's a valid git hash (40 hex characters)
233
+ assert len (result ) == 40
234
+ assert all (c in "0123456789abcdef" for c in result )
235
+
236
+
237
+ def test_get_current_git_hash_invalid_repo (temp_dir ):
238
+ """Test getting git hash from invalid repository."""
239
+ with pytest .raises (Exception ):
240
+ get_current_git_hash (temp_dir )
0 commit comments