1package repository
2
3import (
4 "fmt"
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 plainRepo := CreateGoGitTestRepo(t, false)
17 plainRoot := goGitRepoDir(t, plainRepo)
18 require.NoError(t, plainRepo.Close())
19 plainGitDir := filepath.Join(plainRoot, ".git")
20
21 // Bare
22 bareRepo := CreateGoGitTestRepo(t, true)
23 bareRoot := goGitRepoDir(t, bareRepo)
24 require.NoError(t, bareRepo.Close())
25 bareGitDir := bareRoot
26
27 tests := []struct {
28 inPath string
29 outPath string
30 err bool
31 }{
32 // errors
33 {"/", "", true},
34 // parent dir of a repo
35 {filepath.Dir(plainRoot), "", true},
36
37 // Plain repo
38 {plainRoot, plainGitDir, false},
39 {plainGitDir, plainGitDir, false},
40 {path.Join(plainGitDir, "objects"), plainGitDir, false},
41
42 // Bare repo
43 {bareRoot, bareGitDir, false},
44 {bareGitDir, bareGitDir, false},
45 {path.Join(bareGitDir, "objects"), bareGitDir, false},
46 }
47
48 for i, tc := range tests {
49 r, err := OpenGoGitRepo(tc.inPath, namespace, nil)
50
51 if tc.err {
52 require.Error(t, err, i)
53 } else {
54 require.NoError(t, err, i)
55 assert.Equal(t, filepath.ToSlash(tc.outPath), filepath.ToSlash(r.path), i)
56 require.NoError(t, r.Close())
57 }
58 }
59}
60
61func TestGoGitRepo(t *testing.T) {
62 RepoTest(t, CreateGoGitTestRepo)
63}
64
65func TestGoGitRepo_Indexes(t *testing.T) {
66 repo := CreateGoGitTestRepo(t, false)
67 plainRoot := goGitRepoDir(t, repo)
68
69 // Can create indices
70 indexA, err := repo.GetIndex("a")
71 require.NoError(t, err)
72 require.NotZero(t, indexA)
73 require.FileExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a", "index_meta.json"))
74 require.FileExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a", "store", "root.bolt"))
75
76 indexB, err := repo.GetIndex("b")
77 require.NoError(t, err)
78 require.NotZero(t, indexB)
79 require.DirExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "b"))
80
81 // Can get an existing index
82 indexA, err = repo.GetIndex("a")
83 require.NoError(t, err)
84 require.NotZero(t, indexA)
85}
86
87func TestGoGit_DetectsSubmodules(t *testing.T) {
88 repo := CreateGoGitTestRepo(t, false)
89 expected := filepath.Join(goGitRepoDir(t, repo), "/.git")
90
91 d := t.TempDir()
92 err := os.WriteFile(filepath.Join(d, ".git"), []byte(fmt.Sprintf("gitdir: %s", expected)), 0600)
93 require.NoError(t, err)
94
95 result, err := detectGitPath(d, 0)
96 assert.Empty(t, err)
97 assert.Equal(t, expected, result)
98}