1package repository
2
3import (
4 "strings"
5 "io/ioutil"
6 "log"
7 "os"
8 "testing"
9)
10
11// This is intended for testing only
12
13func CreateTestRepo(bare bool) *GitRepo {
14 dir, err := ioutil.TempDir("", "")
15 if err != nil {
16 log.Fatal(err)
17 }
18
19 // fmt.Println("Creating repo:", dir)
20
21 var creator func(string) (*GitRepo, error)
22
23 if bare {
24 creator = InitBareGitRepo
25 } else {
26 creator = InitGitRepo
27 }
28
29 repo, err := creator(dir)
30 if err != nil {
31 log.Fatal(err)
32 }
33
34 if err := repo.StoreConfig("user.name", "testuser"); err != nil {
35 log.Fatal("failed to set user.name for test repository: ", err)
36 }
37 if err := repo.StoreConfig("user.email", "testuser@example.com"); err != nil {
38 log.Fatal("failed to set user.email for test repository: ", err)
39 }
40
41 return repo
42}
43
44func CleanupTestRepos(t testing.TB, repos ...Repo) {
45 var firstErr error
46 for _, repo := range repos {
47 path := repo.GetPath()
48 if (strings.HasSuffix(path,"/.git")) {
49 // non bare repository, remove complete repos not
50 // only git meta data.
51 path=strings.TrimSuffix(path,"/.git");
52 // Testing non bare repo should also check path is
53 // only .git (i.e. ./.git), but doing so, we should
54 // try to remove the current directory and hav some
55 // trouble. In the present case, this case should not
56 // occure.
57 // TODO consider warning or error when path == ".git"
58 }
59 // fmt.Println("Cleaning repo:", path)
60 err := os.RemoveAll(path)
61 if err != nil {
62 log.Println(err)
63 if firstErr == nil {
64 firstErr = err
65 }
66 }
67 }
68
69 if firstErr != nil {
70 t.Fatal(firstErr)
71 }
72}
73
74func SetupReposAndRemote(t testing.TB) (repoA, repoB, remote *GitRepo) {
75 repoA = CreateTestRepo(false)
76 repoB = CreateTestRepo(false)
77 remote = CreateTestRepo(true)
78
79 remoteAddr := "file://" + remote.GetPath()
80
81 err := repoA.AddRemote("origin", remoteAddr)
82 if err != nil {
83 t.Fatal(err)
84 }
85
86 err = repoB.AddRemote("origin", remoteAddr)
87 if err != nil {
88 t.Fatal(err)
89 }
90
91 return repoA, repoB, remote
92}