gogit_test.go

  1package repository
  2
  3import (
  4	"os"
  5	"path"
  6	"path/filepath"
  7	"testing"
  8
  9	"github.com/stretchr/testify/assert"
 10	"github.com/stretchr/testify/require"
 11)
 12
 13func TestNewGoGitRepo(t *testing.T) {
 14	// Plain
 15	plainRepo := CreateGoGitTestRepo(t, false)
 16	plainRoot := goGitRepoDir(t, plainRepo)
 17	require.NoError(t, plainRepo.Close())
 18	plainGitDir := filepath.Join(plainRoot, ".git")
 19
 20	// Bare
 21	bareRepo := CreateGoGitTestRepo(t, true)
 22	bareRoot := goGitRepoDir(t, bareRepo)
 23	require.NoError(t, bareRepo.Close())
 24	bareGitDir := bareRoot
 25
 26	tests := []struct {
 27		inPath  string
 28		outPath string
 29		err     bool
 30	}{
 31		// errors
 32		{"/", "", true},
 33		// parent dir of a repo
 34		{filepath.Dir(plainRoot), "", true},
 35
 36		// Plain repo
 37		{plainRoot, plainGitDir, false},
 38		{plainGitDir, plainGitDir, false},
 39		{path.Join(plainGitDir, "objects"), plainGitDir, false},
 40
 41		// Bare repo
 42		{bareRoot, bareGitDir, false},
 43		{bareGitDir, bareGitDir, false},
 44		{path.Join(bareGitDir, "objects"), bareGitDir, false},
 45	}
 46
 47	for i, tc := range tests {
 48		r, err := OpenGoGitRepo(tc.inPath, namespace, nil)
 49
 50		if tc.err {
 51			require.Error(t, err, i)
 52		} else {
 53			require.NoError(t, err, i)
 54			assert.Equal(t, filepath.ToSlash(tc.outPath), filepath.ToSlash(r.path), i)
 55			require.NoError(t, r.Close())
 56		}
 57	}
 58}
 59
 60func TestGoGitRepo(t *testing.T) {
 61	RepoTest(t, CreateGoGitTestRepo)
 62}
 63
 64func TestGoGitRepo_Indexes(t *testing.T) {
 65	repo := CreateGoGitTestRepo(t, false)
 66	plainRoot := goGitRepoDir(t, repo)
 67
 68	// Can create indices
 69	indexA, err := repo.GetIndex("a")
 70	require.NoError(t, err)
 71	require.NotZero(t, indexA)
 72	require.FileExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a", "index_meta.json"))
 73	require.FileExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a", "store"))
 74
 75	indexB, err := repo.GetIndex("b")
 76	require.NoError(t, err)
 77	require.NotZero(t, indexB)
 78	require.DirExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "b"))
 79
 80	// Can get an existing index
 81	indexA, err = repo.GetIndex("a")
 82	require.NoError(t, err)
 83	require.NotZero(t, indexA)
 84}
 85
 86func TestGoGit_DetectsSubmodules(t *testing.T) {
 87	expectedPath := "../foo/bar"
 88	submoduleData := "gitdir: " + expectedPath
 89	d := t.TempDir()
 90	if f, err := os.Create(filepath.Join(d, ".git")); err != nil {
 91		t.Fatal("could not create necessary temp file:", err)
 92	} else {
 93		t.Log(f.Name())
 94		if _, err := f.Write([]byte(submoduleData)); err != nil {
 95			t.Fatal("could not write necessary data to temp file:", err)
 96		}
 97		_ = f.Close()
 98	}
 99	result, err := detectGitPath(d)
100	assert.Empty(t, err)
101	assert.Equal(t, expectedPath, result)
102}