git-bug/repository/git_testing.go
Michael Muré 88ad7e606f
repository: remove tie to Bug, improved and reusable testing
- allow the creation of arbitrary Lamport clocks, freeing the way to new entities and removing Bug specific (upper layer) code.
- generalize the memory-only and persisted Lamport clocks behind a common interface
- rework the tests to provide reusable testing code for a Repo, a Clock, a Config, opening a path to add a new Repo implementation more easily
- test previously untested components with those new tests

Note: one problem found during this endeavor is that `identity.Version` also need to store one time + Lamport time for each other Entity (Bug, config, PR ...). This could possibly done without breaking change but it would be much easier to wait for https://github.com/MichaelMure/git-bug-migration to happen.
2020-06-26 19:14:22 +02:00

59 lines
1.1 KiB
Go

package repository
import (
"io/ioutil"
"log"
)
// This is intended for testing only
func CreateTestRepo(bare bool) TestedRepo {
dir, err := ioutil.TempDir("", "")
if err != nil {
log.Fatal(err)
}
var creator func(string) (*GitRepo, error)
if bare {
creator = InitBareGitRepo
} else {
creator = InitGitRepo
}
repo, err := creator(dir)
if err != nil {
log.Fatal(err)
}
config := repo.LocalConfig()
if err := config.StoreString("user.name", "testuser"); err != nil {
log.Fatal("failed to set user.name for test repository: ", err)
}
if err := config.StoreString("user.email", "testuser@example.com"); err != nil {
log.Fatal("failed to set user.email for test repository: ", err)
}
return repo
}
func SetupReposAndRemote() (repoA, repoB, remote TestedRepo) {
repoA = CreateTestRepo(false)
repoB = CreateTestRepo(false)
remote = CreateTestRepo(true)
remoteAddr := "file://" + remote.GetPath()
err := repoA.AddRemote("origin", remoteAddr)
if err != nil {
log.Fatal(err)
}
err = repoB.AddRemote("origin", remoteAddr)
if err != nil {
log.Fatal(err)
}
return repoA, repoB, remote
}