Files
simplegit/gitcmd/blob_test.go
T
2026-07-15 11:06:48 -04:00

85 lines
1.8 KiB
Go

package gitcmd
import (
"strings"
"testing"
)
func TestGetBlobText(t *testing.T) {
f := newFixture(t)
b, err := f.repo.GetBlob("HEAD", "README.md")
if err != nil {
t.Fatal(err)
}
if b.Ref != "HEAD" || b.Path != "README.md" {
t.Errorf("Ref/Path = %q/%q", b.Ref, b.Path)
}
if b.Content != readmeV2 {
t.Errorf("Content = %q, want %q", b.Content, readmeV2)
}
if b.Encoding != "text" {
t.Errorf("Encoding = %q, want text", b.Encoding)
}
if b.IsBinary {
t.Error("IsBinary = true, want false")
}
if b.Size != int64(len(readmeV2)) {
t.Errorf("Size = %d, want %d", b.Size, len(readmeV2))
}
if len(b.SHA) != 40 {
t.Errorf("SHA len = %d, want 40", len(b.SHA))
}
}
func TestGetBlobBinary(t *testing.T) {
f := newFixture(t)
b, err := f.repo.GetBlob("HEAD", "binary.bin")
if err != nil {
t.Fatal(err)
}
if !b.IsBinary {
t.Error("IsBinary = false, want true (contains NUL)")
}
if b.Encoding != "base64" {
t.Errorf("Encoding = %q, want base64", b.Encoding)
}
if b.Size != int64(len(binaryContent)) {
t.Errorf("Size = %d, want %d", b.Size, len(binaryContent))
}
if b.Content != string(binaryContent) {
t.Errorf("binary Content mismatch")
}
}
func TestGetBlobMissingPath(t *testing.T) {
f := newFixture(t)
_, err := f.repo.GetBlob("HEAD", "does/not/exist.txt")
if err == nil {
t.Fatal("expected error for missing path")
}
}
func TestGetRawContent(t *testing.T) {
f := newFixture(t)
raw, err := f.repo.GetRawContent("HEAD", "README.md")
if err != nil {
t.Fatal(err)
}
if raw != readmeV2 {
t.Errorf("raw README = %q, want %q", raw, readmeV2)
}
binRaw, err := f.repo.GetRawContent("HEAD", "binary.bin")
if err != nil {
t.Fatal(err)
}
if binRaw != string(binaryContent) {
t.Errorf("binary raw mismatch: %q", binRaw)
}
if !strings.ContainsRune(binRaw, 0) {
t.Error("binary raw should contain a NUL byte")
}
}