1package repository
2
3import (
4 "io/ioutil"
5 "os"
6 "path"
7 "path/filepath"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12)
13
14func TestNewGoGitRepo(t *testing.T) {
15 // Plain
16 plainRoot, err := ioutil.TempDir("", "")
17 require.NoError(t, err)
18 defer os.RemoveAll(plainRoot)
19
20 _, err = InitGoGitRepo(plainRoot)
21 require.NoError(t, err)
22 plainGitDir := path.Join(plainRoot, ".git")
23
24 // Bare
25 bareRoot, err := ioutil.TempDir("", "")
26 require.NoError(t, err)
27 defer os.RemoveAll(bareRoot)
28
29 _, err = InitBareGoGitRepo(bareRoot)
30 require.NoError(t, err)
31 bareGitDir := bareRoot
32
33 tests := []struct {
34 inPath string
35 outPath string
36 err bool
37 }{
38 // errors
39 {"/", "", true},
40 // parent dir of a repo
41 {filepath.Dir(plainRoot), "", true},
42
43 // Plain repo
44 {plainRoot, plainGitDir, false},
45 {plainGitDir, plainGitDir, false},
46 {path.Join(plainGitDir, "objects"), plainGitDir, false},
47
48 // Bare repo
49 {bareRoot, bareGitDir, false},
50 {bareGitDir, bareGitDir, false},
51 {path.Join(bareGitDir, "objects"), bareGitDir, false},
52 }
53
54 for i, tc := range tests {
55 r, err := NewGoGitRepo(tc.inPath, nil)
56
57 if tc.err {
58 require.Error(t, err, i)
59 } else {
60 require.NoError(t, err, i)
61 assert.Equal(t, tc.outPath, r.GetPath(), i)
62 }
63 }
64}
65
66func TestGoGitRepo(t *testing.T) {
67 RepoTest(t, CreateGoGitTestRepo, CleanupTestRepos)
68}