Something went wrong. Try again.
A project to fine-tune a model specifically for technical writing.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697"""Tests for Git operations (bare repo, blob listing/reading)."""
from __future__ import annotations
from pathlib import Path
import pytest
from docwriter.git import ( GitError, blob_sha256, list_blobs, read_blob, read_blob_by_path, read_blobs, resolve_ref,)
class TestBareRepoOperations: def test_list_blobs(self, mit_repo: tuple[Path, str]) -> None: bare, commit = mit_repo blobs = list_blobs(bare, commit) paths = {b.path for b in blobs} assert "LICENSE" in paths assert "docs/guide/getting-started.md" in paths assert "docs/reference/api.md" in paths assert "docs/changelog.md" in paths assert "src/mylib/__init__.py" in paths assert all(b.size > 0 for b in blobs)
def test_list_blobs_with_base_path(self, mit_repo: tuple[Path, str]) -> None: bare, commit = mit_repo blobs = list_blobs(bare, commit, base_path="docs") paths = {b.path for b in blobs} assert all(p.startswith("docs/") for p in paths) assert "docs/guide/getting-started.md" in paths assert "LICENSE" not in paths
def test_read_blob_by_hash(self, mit_repo: tuple[Path, str]) -> None: bare, commit = mit_repo blobs = list_blobs(bare, commit) license_blob = next(b for b in blobs if b.path == "LICENSE") content = read_blob(bare, license_blob.blob_hash) assert b"MIT License" in content
def test_read_blob_by_path(self, mit_repo: tuple[Path, str]) -> None: bare, commit = mit_repo content = read_blob_by_path(bare, commit, "LICENSE") assert content is not None assert b"MIT License" in content
def test_read_blobs_batch_deduplicates_requests(self, mit_repo: tuple[Path, str]) -> None: bare, commit = mit_repo blobs = list_blobs(bare, commit) selected = blobs[:3] contents = read_blobs( bare, [selected[0].blob_hash, selected[1].blob_hash, selected[0].blob_hash], ) assert set(contents) == {selected[0].blob_hash, selected[1].blob_hash} assert all(contents[blob_hash] for blob_hash in contents)
def test_read_blobs_empty(self, mit_repo: tuple[Path, str]) -> None: bare, _ = mit_repo assert read_blobs(bare, []) == {}
def test_read_blob_by_path_missing(self, mit_repo: tuple[Path, str]) -> None: bare, commit = mit_repo content = read_blob_by_path(bare, commit, "nonexistent/file.txt") assert content is None
def test_resolve_ref(self, mit_repo: tuple[Path, str]) -> None: bare, commit = mit_repo resolved = resolve_ref(bare, "main") assert resolved == commit assert len(resolved) == 40 # full SHA-1 hex
def test_blob_sha256(self) -> None: h = blob_sha256(b"hello") assert h == "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
def test_list_blobs_only_regular_files(self, mit_repo: tuple[Path, str]) -> None: """Symlinks and directories should not appear as blobs.""" bare, commit = mit_repo blobs = list_blobs(bare, commit) # All entries should be regular files (not trees or symlinks) assert len(blobs) > 0 for b in blobs: # Should have a valid blob hash (40 hex chars) assert len(b.blob_hash) == 40 assert b.size >= 0
def test_git_error_on_invalid_repo(self, tmp_path: Path) -> None: with pytest.raises(GitError): list_blobs(tmp_path / "nonexistent", "HEAD")