git2go/index_test.go

53 lines
1.0 KiB
Go
Raw Normal View History

2013-03-06 06:29:56 -06:00
package git
import (
"os"
"runtime"
"testing"
"io/ioutil"
)
2013-03-06 12:18:41 -06:00
func createTestRepo(t *testing.T) *Repository {
2013-03-06 06:29:56 -06:00
// figure out where we can create the test repo
path, err := ioutil.TempDir("", "git2go")
checkFatal(t, err)
2013-03-06 12:06:00 -06:00
repo, err := InitRepository(path, false)
2013-03-06 06:29:56 -06:00
checkFatal(t, err)
tmpfile := "README"
err = ioutil.WriteFile(path + "/" + tmpfile, []byte("foo\n"), 0644)
checkFatal(t, err)
2013-03-06 12:18:41 -06:00
return repo
}
func TestCreateRepoAndStage(t *testing.T) {
repo := createTestRepo(t)
2013-03-06 13:10:48 -06:00
defer os.RemoveAll(repo.Workdir())
2013-03-06 06:29:56 -06:00
idx, err := repo.Index()
checkFatal(t, err)
2013-03-06 12:18:41 -06:00
err = idx.AddByPath("README")
2013-03-06 06:29:56 -06:00
checkFatal(t, err)
treeId, err := idx.WriteTree()
checkFatal(t, err)
if treeId.String() != "b7119b11e8ef7a1a5a34d3ac87f5b075228ac81e" {
t.Fatalf("%v", treeId.String())
}
}
func checkFatal(t *testing.T, err error) {
if err == nil {
return
}
// The failure happens at wherever we were called, not here
_, file, line, ok := runtime.Caller(1)
if !ok {
t.Fatal()
}
2013-03-06 12:18:41 -06:00
t.Fatalf("Fail at %v:%v; %v", file, line, err)
2013-03-06 06:29:56 -06:00
}