2013-09-11 12:17:45 -05:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2015-03-04 13:40:26 -06:00
|
|
|
"errors"
|
2013-09-11 12:17:45 -05:00
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOdbStream(t *testing.T) {
|
|
|
|
repo := createTestRepo(t)
|
2015-04-24 05:59:29 -05:00
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
|
2013-09-11 12:17:45 -05:00
|
|
|
_, _ = seedTestRepo(t, repo)
|
|
|
|
|
|
|
|
odb, error := repo.Odb()
|
|
|
|
checkFatal(t, error)
|
|
|
|
|
|
|
|
str := "hello, world!"
|
|
|
|
|
2013-09-21 17:05:37 -05:00
|
|
|
stream, error := odb.NewWriteStream(len(str), ObjectBlob)
|
2013-09-11 12:17:45 -05:00
|
|
|
checkFatal(t, error)
|
|
|
|
n, error := io.WriteString(stream, str)
|
|
|
|
checkFatal(t, error)
|
|
|
|
if n != len(str) {
|
|
|
|
t.Fatalf("Bad write length %v != %v", n, len(str))
|
|
|
|
}
|
|
|
|
|
|
|
|
error = stream.Close()
|
|
|
|
checkFatal(t, error)
|
|
|
|
|
2014-03-17 23:04:26 -05:00
|
|
|
expectedId, error := NewOid("30f51a3fba5274d53522d0f19748456974647b4f")
|
2013-09-11 12:17:45 -05:00
|
|
|
checkFatal(t, error)
|
|
|
|
if stream.Id.Cmp(expectedId) != 0 {
|
|
|
|
t.Fatal("Wrong data written")
|
|
|
|
}
|
2014-01-25 15:18:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOdbHash(t *testing.T) {
|
|
|
|
|
2015-03-04 13:40:26 -06:00
|
|
|
repo := createTestRepo(t)
|
2015-04-24 05:59:29 -05:00
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
|
2014-01-25 15:18:43 -06:00
|
|
|
_, _ = seedTestRepo(t, repo)
|
|
|
|
|
|
|
|
odb, error := repo.Odb()
|
|
|
|
checkFatal(t, error)
|
|
|
|
|
|
|
|
str := `tree 115fcae49287c82eb55bb275cbbd4556fbed72b7
|
|
|
|
parent 66e1c476199ebcd3e304659992233132c5a52c6c
|
|
|
|
author John Doe <john@doe.com> 1390682018 +0000
|
|
|
|
committer John Doe <john@doe.com> 1390682018 +0000
|
|
|
|
|
2014-05-06 07:19:34 -05:00
|
|
|
Initial commit.`
|
2014-01-25 15:18:43 -06:00
|
|
|
|
|
|
|
oid, error := odb.Hash([]byte(str), ObjectCommit)
|
|
|
|
checkFatal(t, error)
|
|
|
|
|
|
|
|
coid, error := odb.Write([]byte(str), ObjectCommit)
|
|
|
|
checkFatal(t, error)
|
|
|
|
|
|
|
|
if oid.Cmp(coid) != 0 {
|
|
|
|
t.Fatal("Hash and write Oids are different")
|
|
|
|
}
|
2014-03-17 23:04:26 -05:00
|
|
|
}
|
2014-05-06 07:19:34 -05:00
|
|
|
|
|
|
|
func TestOdbForeach(t *testing.T) {
|
|
|
|
repo := createTestRepo(t)
|
2015-04-24 05:59:29 -05:00
|
|
|
defer cleanupTestRepo(t, repo)
|
|
|
|
|
2014-05-06 07:19:34 -05:00
|
|
|
_, _ = seedTestRepo(t, repo)
|
|
|
|
|
|
|
|
odb, err := repo.Odb()
|
|
|
|
checkFatal(t, err)
|
|
|
|
|
|
|
|
expect := 3
|
|
|
|
count := 0
|
|
|
|
err = odb.ForEach(func(id *Oid) error {
|
|
|
|
count++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
checkFatal(t, err)
|
|
|
|
if count != expect {
|
|
|
|
t.Fatalf("Expected %v objects, got %v")
|
|
|
|
}
|
|
|
|
|
|
|
|
expect = 1
|
|
|
|
count = 0
|
|
|
|
to_return := errors.New("not really an error")
|
|
|
|
err = odb.ForEach(func(id *Oid) error {
|
|
|
|
count++
|
|
|
|
return to_return
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != to_return {
|
|
|
|
t.Fatalf("Odb.ForEach() did not return the expected error, got %v", err)
|
|
|
|
}
|
|
|
|
}
|