git-bug/repository/git_testing.go

81 lines
1.5 KiB
Go
Raw Normal View History

package repository
2018-11-21 20:56:12 +03:00
import (
"io/ioutil"
"log"
"os"
"testing"
2018-11-21 20:56:12 +03:00
)
// This is intended for testing only
func CreateTestRepo(bare bool) *GitRepo {
2018-11-21 20:56:12 +03:00
dir, err := ioutil.TempDir("", "")
if err != nil {
log.Fatal(err)
}
// fmt.Println("Creating repo:", dir)
var creator func(string) (*GitRepo, error)
2018-11-21 20:56:12 +03:00
if bare {
creator = InitBareGitRepo
2018-11-21 20:56:12 +03:00
} else {
creator = InitGitRepo
2018-11-21 20:56:12 +03:00
}
repo, err := creator(dir)
if err != nil {
log.Fatal(err)
}
if err := repo.StoreConfig("user.name", "testuser"); err != nil {
log.Fatal("failed to set user.name for test repository: ", err)
}
if err := repo.StoreConfig("user.email", "testuser@example.com"); err != nil {
log.Fatal("failed to set user.email for test repository: ", err)
}
return repo
}
func CleanupTestRepos(t testing.TB, repos ...Repo) {
var firstErr error
for _, repo := range repos {
path := repo.GetPath()
// fmt.Println("Cleaning repo:", path)
err := os.RemoveAll(path)
if err != nil {
log.Println(err)
if firstErr == nil {
firstErr = err
}
}
}
if firstErr != nil {
t.Fatal(firstErr)
}
}
2018-11-21 20:56:12 +03:00
func SetupReposAndRemote(t testing.TB) (repoA, repoB, remote *GitRepo) {
repoA = CreateTestRepo(false)
repoB = CreateTestRepo(false)
remote = CreateTestRepo(true)
2018-11-21 20:56:12 +03:00
remoteAddr := "file://" + remote.GetPath()
2018-11-21 20:56:12 +03:00
err := repoA.AddRemote("origin", remoteAddr)
if err != nil {
t.Fatal(err)
}
err = repoB.AddRemote("origin", remoteAddr)
if err != nil {
t.Fatal(err)
}
return repoA, repoB, remote
}