Skip to content

Commit 5531393

Browse files
Followup PR to integrate nits of #207 (#212)
* Followup PR to integrate nits of #207
1 parent 0d7f37b commit 5531393

File tree

3 files changed

+176
-141
lines changed

3 files changed

+176
-141
lines changed

src/extensions/score_source_code_linker/tests/test_codelink.py

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -124,75 +124,6 @@ def git_repo(temp_dir):
124124
return git_dir
125125

126126

127-
@pytest.fixture
128-
def git_repo_with_https_remote(temp_dir):
129-
"""Create a git repository with HTTPS remote for testing."""
130-
git_dir = temp_dir / "test_repo_https"
131-
git_dir.mkdir()
132-
133-
# Initialize git repo
134-
subprocess.run(["git", "init"], cwd=git_dir, check=True, capture_output=True)
135-
subprocess.run(
136-
["git", "config", "user.email", "[email protected]"], cwd=git_dir, check=True
137-
)
138-
subprocess.run(["git", "config", "user.name", "Test User"], cwd=git_dir, check=True)
139-
140-
# Create a test file and commit
141-
test_file = git_dir / "test_file.py"
142-
test_file.write_text("# Test file\nprint('hello')\n")
143-
subprocess.run(["git", "add", "."], cwd=git_dir, check=True)
144-
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=git_dir, check=True)
145-
146-
# Add HTTPS remote
147-
subprocess.run(
148-
[
149-
"git",
150-
"remote",
151-
"add",
152-
"origin",
153-
"https://github.com/test-user/test-repo.git",
154-
],
155-
cwd=git_dir,
156-
check=True,
157-
)
158-
159-
return git_dir
160-
161-
162-
@pytest.fixture
163-
def git_repo_multiple_remotes(temp_dir):
164-
"""Create a git repository with multiple remotes for testing."""
165-
git_dir = temp_dir / "test_repo_multiple"
166-
git_dir.mkdir()
167-
168-
# Initialize git repo
169-
subprocess.run(["git", "init"], cwd=git_dir, check=True, capture_output=True)
170-
subprocess.run(
171-
["git", "config", "user.email", "[email protected]"], cwd=git_dir, check=True
172-
)
173-
subprocess.run(["git", "config", "user.name", "Test User"], cwd=git_dir, check=True)
174-
175-
# Create a test file and commit
176-
test_file = git_dir / "test_file.py"
177-
test_file.write_text("# Test file\nprint('hello')\n")
178-
subprocess.run(["git", "add", "."], cwd=git_dir, check=True)
179-
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=git_dir, check=True)
180-
181-
# Add multiple remotes
182-
subprocess.run(
183-
["git", "remote", "add", "upstream", "[email protected]:upstream/test-repo.git"],
184-
cwd=git_dir,
185-
check=True,
186-
)
187-
subprocess.run(
188-
["git", "remote", "add", "origin", "[email protected]:test-user/test-repo.git"],
189-
cwd=git_dir,
190-
check=True,
191-
)
192-
193-
return git_dir
194-
195-
196127
@pytest.fixture
197128
def sample_needlinks():
198129
"""Create sample NeedLink objects for testing."""
@@ -354,75 +285,6 @@ def test_group_by_need_empty_list():
354285
assert len(result) == 0
355286

356287

357-
# Test git-related functions
358-
def test_parse_git_output_ssh_format():
359-
"""Test parsing git remote output in SSH format."""
360-
git_line = "origin [email protected]:test-user/test-repo.git (fetch)"
361-
result = parse_remote_git_output(git_line)
362-
assert result == "test-user/test-repo"
363-
364-
365-
def test_parse_git_output_https_format():
366-
"""Test parsing git remote output in HTTPS format."""
367-
git_line = "origin https://github.com/test-user/test-repo.git (fetch)"
368-
result = parse_remote_git_output(git_line)
369-
assert result == "test-user/test-repo"
370-
371-
372-
def test_parse_git_output_ssh_format_without_git_suffix():
373-
"""Test parsing git remote output in SSH format without .git suffix."""
374-
git_line = "origin [email protected]:test-user/test-repo (fetch)"
375-
result = parse_remote_git_output(git_line)
376-
assert result == "test-user/test-repo"
377-
378-
379-
def test_parse_git_output_invalid_format():
380-
"""Test parsing invalid git remote output."""
381-
git_line = "invalid"
382-
result = parse_remote_git_output(git_line)
383-
assert result == ""
384-
385-
386-
def test_parse_git_output_empty_string():
387-
"""Test parsing empty git remote output."""
388-
git_line = ""
389-
result = parse_remote_git_output(git_line)
390-
assert result == ""
391-
392-
393-
def test_get_github_repo_info_ssh_remote(git_repo):
394-
"""Test getting GitHub repository information with SSH remote."""
395-
result = get_github_repo_info(git_repo)
396-
assert result == "test-user/test-repo"
397-
398-
399-
def test_get_github_repo_info_https_remote(git_repo_with_https_remote):
400-
"""Test getting GitHub repository information with HTTPS remote."""
401-
result = get_github_repo_info(git_repo_with_https_remote)
402-
assert result == "test-user/test-repo"
403-
404-
405-
def test_get_github_repo_info_multiple_remotes(git_repo_multiple_remotes):
406-
"""Test GitHub repo info retrieval with multiple remotes (origin preferred)."""
407-
result = get_github_repo_info(git_repo_multiple_remotes)
408-
assert result == "test-user/test-repo"
409-
410-
411-
def test_get_current_git_hash(git_repo):
412-
"""Test getting current git hash."""
413-
result = get_current_git_hash(git_repo)
414-
415-
# Verify it's a valid git hash (40 hex characters)
416-
assert len(result) == 40
417-
assert all(c in "0123456789abcdef" for c in result)
418-
419-
420-
def test_get_current_git_hash_invalid_repo(temp_dir):
421-
"""Test getting git hash from invalid repository."""
422-
with pytest.raises(subprocess.CalledProcessError):
423-
get_current_git_hash(temp_dir)
424-
425-
426288
def test_get_github_link_with_real_repo(git_repo):
427289
"""Test generating GitHub link with real repository."""
428290
# Create a needlink

src/helper_lib/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def parse_remote_git_output(str_line: str) -> str:
7575

7676
def get_github_repo_info(git_root_cwd: Path) -> str:
7777
"""
78-
Extract GitHub repository info from git remotes.
78+
Query git for the github remote repository (based on heuristic).
7979
8080
Execution context behavior:
8181
- Works consistently across all contexts when given valid git directory
@@ -154,5 +154,8 @@ def get_current_git_hash(git_root: Path) -> str:
154154
assert all(c in "0123456789abcdef" for c in decoded_result)
155155
return decoded_result
156156
except Exception as e:
157-
LOGGER.warning(f"Unexpected error: {git_root}", exc_info=e)
157+
LOGGER.warning(
158+
f"Unexpected error while trying to get git_hash. Exceuted in: {git_root}",
159+
exc_info=e,
160+
)
158161
raise

src/helper_lib/test_helper_lib.py

Lines changed: 171 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
import pytest
1919

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+
)
2125

2226

2327
@pytest.fixture
@@ -27,6 +31,103 @@ def temp_dir():
2731
yield Path(temp_dir)
2832

2933

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\nprint('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\nprint('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\nprint('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+
30131
# Test error handling
31132
def test_git_operations_with_no_commits(temp_dir):
32133
"""Test git operations on repo with no commits."""
@@ -68,3 +169,72 @@ def test_git_repo_with_no_remotes(temp_dir):
68169
# Should raise an exception when trying to get repo info
69170
with pytest.raises(AssertionError):
70171
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

Comments
 (0)