gogit_test.go

 1package repository
 2
 3import (
 4	"path"
 5	"path/filepath"
 6	"testing"
 7
 8	"github.com/stretchr/testify/assert"
 9	"github.com/stretchr/testify/require"
10)
11
12func TestNewGoGitRepo(t *testing.T) {
13	// Plain
14	plainRepo := CreateGoGitTestRepo(t, false)
15	plainRoot := RepoDir(t, plainRepo)
16	require.NoError(t, plainRepo.Close())
17	plainGitDir := filepath.Join(plainRoot, ".git")
18
19	// Bare
20	bareRepo := CreateGoGitTestRepo(t, true)
21	bareRoot := RepoDir(t, bareRepo)
22	require.NoError(t, bareRepo.Close())
23	bareGitDir := bareRoot
24
25	tests := []struct {
26		inPath  string
27		outPath string
28		err     bool
29	}{
30		// errors
31		{"/", "", true},
32		// parent dir of a repo
33		{filepath.Dir(plainRoot), "", true},
34
35		// Plain repo
36		{plainRoot, plainGitDir, false},
37		{plainGitDir, plainGitDir, false},
38		{path.Join(plainGitDir, "objects"), plainGitDir, false},
39
40		// Bare repo
41		{bareRoot, bareGitDir, false},
42		{bareGitDir, bareGitDir, false},
43		{path.Join(bareGitDir, "objects"), bareGitDir, false},
44	}
45
46	for i, tc := range tests {
47		r, err := OpenGoGitRepo(tc.inPath, namespace, nil)
48
49		if tc.err {
50			require.Error(t, err, i)
51		} else {
52			require.NoError(t, err, i)
53			assert.Equal(t, filepath.ToSlash(tc.outPath), filepath.ToSlash(r.path), i)
54			require.NoError(t, r.Close())
55		}
56	}
57}
58
59func TestGoGitRepo(t *testing.T) {
60	RepoTest(t, CreateGoGitTestRepo)
61}
62
63func TestGoGitRepo_Indexes(t *testing.T) {
64	repo := CreateGoGitTestRepo(t, false)
65	plainRoot := RepoDir(t, repo)
66
67	// Can create indices
68	indexA, err := repo.GetBleveIndex("a")
69	require.NoError(t, err)
70	require.NotZero(t, indexA)
71	require.FileExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a", "index_meta.json"))
72	require.FileExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a", "store"))
73
74	indexB, err := repo.GetBleveIndex("b")
75	require.NoError(t, err)
76	require.NotZero(t, indexB)
77	require.DirExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "b"))
78
79	// Can get an existing index
80	indexA, err = repo.GetBleveIndex("a")
81	require.NoError(t, err)
82	require.NotZero(t, indexA)
83
84	// Can delete an index
85	err = repo.ClearBleveIndex("a")
86	require.NoError(t, err)
87	require.NoDirExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a"))
88}