gogit_testing.go

 1package repository
 2
 3import (
 4	"io/ioutil"
 5	"log"
 6
 7	"github.com/99designs/keyring"
 8)
 9
10// This is intended for testing only
11
12func CreateGoGitTestRepo(bare bool) TestedRepo {
13	dir, err := ioutil.TempDir("", "")
14	if err != nil {
15		log.Fatal(err)
16	}
17
18	var creator func(string) (*GoGitRepo, error)
19
20	if bare {
21		creator = InitBareGoGitRepo
22	} else {
23		creator = InitGoGitRepo
24	}
25
26	repo, err := creator(dir)
27	if err != nil {
28		log.Fatal(err)
29	}
30
31	config := repo.LocalConfig()
32	if err := config.StoreString("user.name", "testuser"); err != nil {
33		log.Fatal("failed to set user.name for test repository: ", err)
34	}
35	if err := config.StoreString("user.email", "testuser@example.com"); err != nil {
36		log.Fatal("failed to set user.email for test repository: ", err)
37	}
38
39	// make sure we use a mock keyring for testing to not interact with the global system
40	return &replaceKeyring{
41		TestedRepo: repo,
42		keyring:    keyring.NewArrayKeyring(nil),
43	}
44}
45
46func SetupGoGitReposAndRemote() (repoA, repoB, remote TestedRepo) {
47	repoA = CreateGoGitTestRepo(false)
48	repoB = CreateGoGitTestRepo(false)
49	remote = CreateGoGitTestRepo(true)
50
51	err := repoA.AddRemote("origin", remote.GetLocalRemote())
52	if err != nil {
53		log.Fatal(err)
54	}
55
56	err = repoB.AddRemote("origin", remote.GetLocalRemote())
57	if err != nil {
58		log.Fatal(err)
59	}
60
61	return repoA, repoB, remote
62}