object_test.go 4.27 KB
Newer Older
Vicent Marti's avatar
Vicent Marti committed
1 2 3
package git

import (
4
	"strings"
Vicent Marti's avatar
Vicent Marti committed
5 6 7 8
	"testing"
)

func TestObjectPoymorphism(t *testing.T) {
9
	t.Parallel()
Vicent Marti's avatar
Vicent Marti committed
10
	repo := createTestRepo(t)
11 12
	defer cleanupTestRepo(t, repo)

Vicent Marti's avatar
Vicent Marti committed
13 14
	commitId, treeId := seedTestRepo(t, repo)

15
	var obj *Object
Vicent Marti's avatar
Vicent Marti committed
16 17 18 19

	commit, err := repo.LookupCommit(commitId)
	checkFatal(t, err)

20
	obj = &commit.Object
21
	if obj.Type() != ObjectCommit {
Vicent Marti's avatar
Vicent Marti committed
22 23 24
		t.Fatalf("Wrong object type, expected commit, have %v", obj.Type())
	}

25 26 27 28
	commitTree, err := commit.Tree()
	checkFatal(t, err)
	commitTree.EntryCount()

Vicent Marti's avatar
Vicent Marti committed
29 30 31
	tree, err := repo.LookupTree(treeId)
	checkFatal(t, err)

32
	obj = &tree.Object
33
	if obj.Type() != ObjectTree {
Vicent Marti's avatar
Vicent Marti committed
34 35 36
		t.Fatalf("Wrong object type, expected tree, have %v", obj.Type())
	}

37 38
	tree2, err := obj.AsTree()
	if err != nil {
Vicent Marti's avatar
Vicent Marti committed
39 40 41
		t.Fatalf("Converting back to *Tree is not ok")
	}

42 43
	entry := tree2.EntryByName("README")
	if entry == nil {
Vicent Marti's avatar
Vicent Marti committed
44 45 46
		t.Fatalf("Tree did not have expected \"README\" entry")
	}

47 48 49 50
	if entry.Filemode != FilemodeBlob {
		t.Fatal("Wrong filemode for \"README\"")
	}

51 52
	_, err = obj.AsCommit()
	if err == nil {
Vicent Marti's avatar
Vicent Marti committed
53 54 55 56 57 58
		t.Fatalf("*Tree is somehow the same as *Commit")
	}

	obj, err = repo.Lookup(tree.Id())
	checkFatal(t, err)

59 60
	_, err = obj.AsTree()
	if err != nil {
Vicent Marti's avatar
Vicent Marti committed
61 62 63
		t.Fatalf("Lookup creates the wrong type")
	}

64
	if obj.Type() != ObjectTree {
Vicent Marti's avatar
Vicent Marti committed
65 66 67 68 69
		t.Fatalf("Type() doesn't agree with dynamic type")
	}

	obj, err = repo.RevparseSingle("HEAD")
	checkFatal(t, err)
70
	if obj.Type() != ObjectCommit || obj.Id().String() != commit.Id().String() {
Vicent Marti's avatar
Vicent Marti committed
71 72 73 74 75
		t.Fatalf("Failed to parse the right revision")
	}

	obj, err = repo.RevparseSingle("HEAD^{tree}")
	checkFatal(t, err)
76
	if obj.Type() != ObjectTree || obj.Id().String() != tree.Id().String() {
Vicent Marti's avatar
Vicent Marti committed
77 78 79
		t.Fatalf("Failed to parse the right revision")
	}
}
80 81 82 83 84 85 86 87 88 89 90 91 92

func checkOwner(t *testing.T, repo *Repository, obj Object) {
	owner := obj.Owner()
	if owner == nil {
		t.Fatal("bad owner")
	}

	if owner.ptr != repo.ptr {
		t.Fatalf("bad owner, got %v expected %v\n", owner.ptr, repo.ptr)
	}
}

func TestObjectOwner(t *testing.T) {
93
	t.Parallel()
94
	repo := createTestRepo(t)
95 96
	defer cleanupTestRepo(t, repo)

97 98 99 100 101 102 103 104
	commitId, treeId := seedTestRepo(t, repo)

	commit, err := repo.LookupCommit(commitId)
	checkFatal(t, err)

	tree, err := repo.LookupTree(treeId)
	checkFatal(t, err)

105 106
	checkOwner(t, repo, commit.Object)
	checkOwner(t, repo, tree.Object)
107
}
108

109 110
func checkShortId(t *testing.T, Id, shortId string) {
	if len(shortId) < 7 || len(shortId) >= len(Id) {
111
		t.Fatalf("bad shortId lenght %d", len(shortId))
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	}

	if !strings.HasPrefix(Id, shortId) {
		t.Fatalf("bad shortId, should be prefix of %s, but is %s\n", Id, shortId)
	}
}

func TestObjectShortId(t *testing.T) {
	t.Parallel()
	repo := createTestRepo(t)
	defer cleanupTestRepo(t, repo)

	commitId, _ := seedTestRepo(t, repo)

	commit, err := repo.LookupCommit(commitId)
	checkFatal(t, err)

	shortId, err := commit.ShortId()
	checkFatal(t, err)

	checkShortId(t, commitId.String(), shortId)
}

135
func TestObjectPeel(t *testing.T) {
136
	t.Parallel()
137 138 139 140 141
	repo := createTestRepo(t)
	defer cleanupTestRepo(t, repo)

	commitID, treeID := seedTestRepo(t, repo)

142
	var obj *Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

	commit, err := repo.LookupCommit(commitID)
	checkFatal(t, err)

	obj, err = commit.Peel(ObjectAny)
	checkFatal(t, err)

	if obj.Type() != ObjectTree {
		t.Fatalf("Wrong object type when peeling a commit, expected tree, have %v", obj.Type())
	}

	obj, err = commit.Peel(ObjectTag)

	if !IsErrorCode(err, ErrInvalidSpec) {
		t.Fatalf("Wrong error when peeling a commit to a tag, expected ErrInvalidSpec, have %v", err)
	}

	tree, err := repo.LookupTree(treeID)
	checkFatal(t, err)

	obj, err = tree.Peel(ObjectAny)

	if !IsErrorCode(err, ErrInvalidSpec) {
		t.Fatalf("Wrong error when peeling a tree, expected ErrInvalidSpec, have %v", err)
	}

	entry := tree.EntryByName("README")

	blob, err := repo.LookupBlob(entry.Id)
	checkFatal(t, err)

	obj, err = blob.Peel(ObjectAny)

	if !IsErrorCode(err, ErrInvalidSpec) {
		t.Fatalf("Wrong error when peeling a blob, expected ErrInvalidSpec, have %v", err)
	}

	tagID := createTestTag(t, repo, commit)

	tag, err := repo.LookupTag(tagID)
	checkFatal(t, err)

	obj, err = tag.Peel(ObjectAny)
	checkFatal(t, err)

	if obj.Type() != ObjectCommit {
		t.Fatalf("Wrong object type when peeling a tag, expected commit, have %v", obj.Type())
	}

	// TODO: Should test a tag that annotates a different object than a commit
	// but it's impossible at the moment to tag such an object.
}