110 lines
2.6 KiB
Go
110 lines
2.6 KiB
Go
package gitcmd
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetTreeRoot(t *testing.T) {
|
|
f := newFixture(t)
|
|
tree, err := f.repo.GetTree("HEAD", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tree.Ref != "HEAD" || tree.Path != "" {
|
|
t.Errorf("Ref/Path = %q/%q", tree.Ref, tree.Path)
|
|
}
|
|
|
|
byName := map[string]TreeEntry{}
|
|
for _, e := range tree.Entries {
|
|
byName[e.Name] = e
|
|
}
|
|
|
|
wantNames := []string{"README.md", "src", "binary.bin"}
|
|
for _, n := range wantNames {
|
|
if _, ok := byName[n]; !ok {
|
|
t.Errorf("missing entry %q in %v", n, byName)
|
|
}
|
|
}
|
|
if _, ok := byName["deleted.txt"]; ok {
|
|
t.Error("deleted.txt should not be present at HEAD")
|
|
}
|
|
|
|
readme := byName["README.md"]
|
|
if readme.Type != EntryBlob {
|
|
t.Errorf("README.md Type = %q, want blob", readme.Type)
|
|
}
|
|
if readme.Mode != "100644" {
|
|
t.Errorf("README.md Mode = %q, want 100644", readme.Mode)
|
|
}
|
|
if readme.Size != int64(len(readmeV2)) {
|
|
t.Errorf("README.md Size = %d, want %d", readme.Size, len(readmeV2))
|
|
}
|
|
if readme.Path != "README.md" {
|
|
t.Errorf("README.md Path = %q, want README.md (root entry path == name)", readme.Path)
|
|
}
|
|
|
|
src := byName["src"]
|
|
if src.Type != EntryTree {
|
|
t.Errorf("src Type = %q, want tree", src.Type)
|
|
}
|
|
if src.Mode != "040000" {
|
|
t.Errorf("src Mode = %q, want 040000", src.Mode)
|
|
}
|
|
if src.Size != 0 {
|
|
t.Errorf("src Size = %d, want 0 (directory)", src.Size)
|
|
}
|
|
|
|
bin := byName["binary.bin"]
|
|
if bin.Type != EntryBlob {
|
|
t.Errorf("binary.bin Type = %q, want blob", bin.Type)
|
|
}
|
|
if bin.Size != int64(len(binaryContent)) {
|
|
t.Errorf("binary.bin Size = %d, want %d", bin.Size, len(binaryContent))
|
|
}
|
|
}
|
|
|
|
func TestGetTreeSubdir(t *testing.T) {
|
|
f := newFixture(t)
|
|
tree, err := f.repo.GetTree("HEAD", "src")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tree.Path != "src" {
|
|
t.Errorf("Path = %q, want src", tree.Path)
|
|
}
|
|
if len(tree.Entries) != 1 {
|
|
t.Fatalf("entries = %d, want 1", len(tree.Entries))
|
|
}
|
|
e := tree.Entries[0]
|
|
if e.Name != "main.txt" {
|
|
t.Errorf("Name = %q, want main.txt", e.Name)
|
|
}
|
|
if e.Path != "src/main.txt" {
|
|
t.Errorf("Path = %q, want src/main.txt", e.Path)
|
|
}
|
|
if e.Type != EntryBlob {
|
|
t.Errorf("Type = %q, want blob", e.Type)
|
|
}
|
|
if e.Size != int64(len(mainTxt)) {
|
|
t.Errorf("Size = %d, want %d", e.Size, len(mainTxt))
|
|
}
|
|
}
|
|
|
|
func TestGetTreeEmptyRepo(t *testing.T) {
|
|
dir := t.TempDir()
|
|
bare := dir + "/empty.git"
|
|
gitRun(t, "", nil, "init", "-q", "--bare", bare)
|
|
r, err := OpenRepository(context.Background(), bare)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tree, err := r.GetTree("HEAD", "")
|
|
if err != nil {
|
|
t.Fatalf("GetTree on empty repo: %v", err)
|
|
}
|
|
if len(tree.Entries) != 0 {
|
|
t.Errorf("empty repo entries = %d, want 0", len(tree.Entries))
|
|
}
|
|
}
|