Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 0 additions & 138 deletions src/extensions/score_source_code_linker/tests/test_codelink.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,75 +124,6 @@ def git_repo(temp_dir):
return git_dir


@pytest.fixture
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing these, as they have been added to 'test_helper_lib'

def git_repo_with_https_remote(temp_dir):
"""Create a git repository with HTTPS remote for testing."""
git_dir = temp_dir / "test_repo_https"
git_dir.mkdir()

# Initialize git repo
subprocess.run(["git", "init"], cwd=git_dir, check=True, capture_output=True)
subprocess.run(
["git", "config", "user.email", "test@example.com"], cwd=git_dir, check=True
)
subprocess.run(["git", "config", "user.name", "Test User"], cwd=git_dir, check=True)

# Create a test file and commit
test_file = git_dir / "test_file.py"
test_file.write_text("# Test file\nprint('hello')\n")
subprocess.run(["git", "add", "."], cwd=git_dir, check=True)
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=git_dir, check=True)

# Add HTTPS remote
subprocess.run(
[
"git",
"remote",
"add",
"origin",
"https://github.com/test-user/test-repo.git",
],
cwd=git_dir,
check=True,
)

return git_dir


@pytest.fixture
def git_repo_multiple_remotes(temp_dir):
"""Create a git repository with multiple remotes for testing."""
git_dir = temp_dir / "test_repo_multiple"
git_dir.mkdir()

# Initialize git repo
subprocess.run(["git", "init"], cwd=git_dir, check=True, capture_output=True)
subprocess.run(
["git", "config", "user.email", "test@example.com"], cwd=git_dir, check=True
)
subprocess.run(["git", "config", "user.name", "Test User"], cwd=git_dir, check=True)

# Create a test file and commit
test_file = git_dir / "test_file.py"
test_file.write_text("# Test file\nprint('hello')\n")
subprocess.run(["git", "add", "."], cwd=git_dir, check=True)
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=git_dir, check=True)

# Add multiple remotes
subprocess.run(
["git", "remote", "add", "upstream", "git@github.com:upstream/test-repo.git"],
cwd=git_dir,
check=True,
)
subprocess.run(
["git", "remote", "add", "origin", "git@github.com:test-user/test-repo.git"],
cwd=git_dir,
check=True,
)

return git_dir


@pytest.fixture
def sample_needlinks():
"""Create sample NeedLink objects for testing."""
Expand Down Expand Up @@ -354,75 +285,6 @@ def test_group_by_need_empty_list():
assert len(result) == 0


# Test git-related functions
def test_parse_git_output_ssh_format():
"""Test parsing git remote output in SSH format."""
git_line = "origin git@github.com:test-user/test-repo.git (fetch)"
result = parse_remote_git_output(git_line)
assert result == "test-user/test-repo"


def test_parse_git_output_https_format():
"""Test parsing git remote output in HTTPS format."""
git_line = "origin https://github.com/test-user/test-repo.git (fetch)"
result = parse_remote_git_output(git_line)
assert result == "test-user/test-repo"


def test_parse_git_output_ssh_format_without_git_suffix():
"""Test parsing git remote output in SSH format without .git suffix."""
git_line = "origin git@github.com:test-user/test-repo (fetch)"
result = parse_remote_git_output(git_line)
assert result == "test-user/test-repo"


def test_parse_git_output_invalid_format():
"""Test parsing invalid git remote output."""
git_line = "invalid"
result = parse_remote_git_output(git_line)
assert result == ""


def test_parse_git_output_empty_string():
"""Test parsing empty git remote output."""
git_line = ""
result = parse_remote_git_output(git_line)
assert result == ""


def test_get_github_repo_info_ssh_remote(git_repo):
"""Test getting GitHub repository information with SSH remote."""
result = get_github_repo_info(git_repo)
assert result == "test-user/test-repo"


def test_get_github_repo_info_https_remote(git_repo_with_https_remote):
"""Test getting GitHub repository information with HTTPS remote."""
result = get_github_repo_info(git_repo_with_https_remote)
assert result == "test-user/test-repo"


def test_get_github_repo_info_multiple_remotes(git_repo_multiple_remotes):
"""Test GitHub repo info retrieval with multiple remotes (origin preferred)."""
result = get_github_repo_info(git_repo_multiple_remotes)
assert result == "test-user/test-repo"


def test_get_current_git_hash(git_repo):
"""Test getting current git hash."""
result = get_current_git_hash(git_repo)

# Verify it's a valid git hash (40 hex characters)
assert len(result) == 40
assert all(c in "0123456789abcdef" for c in result)


def test_get_current_git_hash_invalid_repo(temp_dir):
"""Test getting git hash from invalid repository."""
with pytest.raises(subprocess.CalledProcessError):
get_current_git_hash(temp_dir)


def test_get_github_link_with_real_repo(git_repo):
"""Test generating GitHub link with real repository."""
# Create a needlink
Expand Down
7 changes: 5 additions & 2 deletions src/helper_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def parse_remote_git_output(str_line: str) -> str:

def get_github_repo_info(git_root_cwd: Path) -> str:
"""
Extract GitHub repository info from git remotes.
Query git for the github remote repository (based on heuristic).

Execution context behavior:
- Works consistently across all contexts when given valid git directory
Expand Down Expand Up @@ -154,5 +154,8 @@ def get_current_git_hash(git_root: Path) -> str:
assert all(c in "0123456789abcdef" for c in decoded_result)
return decoded_result
except Exception as e:
LOGGER.warning(f"Unexpected error: {git_root}", exc_info=e)
LOGGER.warning(
f"Unexpected error while trying to get git_hash. Exceuted in: {git_root}",
exc_info=e,
)
raise
172 changes: 171 additions & 1 deletion src/helper_lib/test_helper_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

import pytest

from src.helper_lib import get_current_git_hash, get_github_repo_info
from src.helper_lib import (
get_current_git_hash,
get_github_repo_info,
parse_remote_git_output,
)


@pytest.fixture
Expand All @@ -27,6 +31,103 @@ def temp_dir():
yield Path(temp_dir)


@pytest.fixture
def git_repo(temp_dir):
"""Create a real git repository for testing."""
git_dir = temp_dir / "test_repo"
git_dir.mkdir()

# Initialize git repo
subprocess.run(["git", "init"], cwd=git_dir, check=True, capture_output=True)
subprocess.run(
["git", "config", "user.email", "test@example.com"], cwd=git_dir, check=True
)
subprocess.run(["git", "config", "user.name", "Test User"], cwd=git_dir, check=True)

# Create a test file and commit
test_file = git_dir / "test_file.py"
test_file.write_text("# Test file\nprint('hello')\n")
subprocess.run(["git", "add", "."], cwd=git_dir, check=True)
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=git_dir, check=True)

# Add a remote
subprocess.run(
["git", "remote", "add", "origin", "git@github.com:test-user/test-repo.git"],
cwd=git_dir,
check=True,
)
return git_dir


@pytest.fixture
def git_repo_multiple_remotes(temp_dir):
"""Create a git repository with multiple remotes for testing."""
git_dir = temp_dir / "test_repo_multiple"
git_dir.mkdir()

# Initialize git repo
subprocess.run(["git", "init"], cwd=git_dir, check=True, capture_output=True)
subprocess.run(
["git", "config", "user.email", "test@example.com"], cwd=git_dir, check=True
)
subprocess.run(["git", "config", "user.name", "Test User"], cwd=git_dir, check=True)

# Create a test file and commit
test_file = git_dir / "test_file.py"
test_file.write_text("# Test file\nprint('hello')\n")
subprocess.run(["git", "add", "."], cwd=git_dir, check=True)
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=git_dir, check=True)

# Add multiple remotes
subprocess.run(
["git", "remote", "add", "upstream", "git@github.com:upstream/test-repo.git"],
cwd=git_dir,
check=True,
)
subprocess.run(
["git", "remote", "add", "origin", "git@github.com:test-user/test-repo.git"],
cwd=git_dir,
check=True,
)

return git_dir


@pytest.fixture
def git_repo_with_https_remote(temp_dir):
"""Create a git repository with HTTPS remote for testing."""
git_dir = temp_dir / "test_repo_https"
git_dir.mkdir()

# Initialize git repo
subprocess.run(["git", "init"], cwd=git_dir, check=True, capture_output=True)
subprocess.run(
["git", "config", "user.email", "test@example.com"], cwd=git_dir, check=True
)
subprocess.run(["git", "config", "user.name", "Test User"], cwd=git_dir, check=True)

# Create a test file and commit
test_file = git_dir / "test_file.py"
test_file.write_text("# Test file\nprint('hello')\n")
subprocess.run(["git", "add", "."], cwd=git_dir, check=True)
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd=git_dir, check=True)

# Add HTTPS remote
subprocess.run(
[
"git",
"remote",
"add",
"origin",
"https://github.com/test-user/test-repo.git",
],
cwd=git_dir,
check=True,
)

return git_dir


# Test error handling
def test_git_operations_with_no_commits(temp_dir):
"""Test git operations on repo with no commits."""
Expand Down Expand Up @@ -68,3 +169,72 @@ def test_git_repo_with_no_remotes(temp_dir):
# Should raise an exception when trying to get repo info
with pytest.raises(AssertionError):
get_github_repo_info(git_dir)


# Test git-related functions
def test_parse_git_output_ssh_format():
"""Test parsing git remote output in SSH format."""
git_line = "origin git@github.com:test-user/test-repo.git (fetch)"
result = parse_remote_git_output(git_line)
assert result == "test-user/test-repo"


def test_parse_git_output_https_format():
"""Test parsing git remote output in HTTPS format."""
git_line = "origin https://github.com/test-user/test-repo.git (fetch)"
result = parse_remote_git_output(git_line)
assert result == "test-user/test-repo"


def test_parse_git_output_ssh_format_without_git_suffix():
"""Test parsing git remote output in SSH format without .git suffix."""
git_line = "origin git@github.com:test-user/test-repo (fetch)"
result = parse_remote_git_output(git_line)
assert result == "test-user/test-repo"


def test_parse_git_output_invalid_format():
"""Test parsing invalid git remote output."""
git_line = "invalid"
result = parse_remote_git_output(git_line)
assert result == ""


def test_parse_git_output_empty_string():
"""Test parsing empty git remote output."""
git_line = ""
result = parse_remote_git_output(git_line)
assert result == ""


def test_get_github_repo_info_ssh_remote(git_repo):
"""Test getting GitHub repository information with SSH remote."""
result = get_github_repo_info(git_repo)
assert result == "test-user/test-repo"


def test_get_github_repo_info_https_remote(git_repo_with_https_remote):
"""Test getting GitHub repository information with HTTPS remote."""
result = get_github_repo_info(git_repo_with_https_remote)
assert result == "test-user/test-repo"


def test_get_github_repo_info_multiple_remotes(git_repo_multiple_remotes):
"""Test GitHub repo info retrieval with multiple remotes (origin preferred)."""
result = get_github_repo_info(git_repo_multiple_remotes)
assert result == "test-user/test-repo"


def test_get_current_git_hash(git_repo):
"""Test getting current git hash."""
result = get_current_git_hash(git_repo)

# Verify it's a valid git hash (40 hex characters)
assert len(result) == 40
assert all(c in "0123456789abcdef" for c in result)


def test_get_current_git_hash_invalid_repo(temp_dir):
"""Test getting git hash from invalid repository."""
with pytest.raises(Exception):
get_current_git_hash(temp_dir)
Loading