repo_testing.go

 1package repository
 2
 3import (
 4	"log"
 5	"os"
 6	"strings"
 7	"testing"
 8
 9	"github.com/stretchr/testify/assert"
10	"github.com/stretchr/testify/require"
11)
12
13func CleanupTestRepos(repos ...Repo) {
14	var firstErr error
15	for _, repo := range repos {
16		path := repo.GetPath()
17		if strings.HasSuffix(path, "/.git") {
18			// for a normal repository (not --bare), we want to remove everything
19			// including the parent directory where files are checked out
20			path = strings.TrimSuffix(path, "/.git")
21
22			// Testing non-bare repo should also check path is
23			// only .git (i.e. ./.git), but doing so, we should
24			// try to remove the current directory and hav some
25			// trouble. In the present case, this case should not
26			// occur.
27			// TODO consider warning or error when path == ".git"
28		}
29		// fmt.Println("Cleaning repo:", path)
30		err := os.RemoveAll(path)
31		if err != nil {
32			log.Println(err)
33			if firstErr == nil {
34				firstErr = err
35			}
36		}
37	}
38
39	if firstErr != nil {
40		log.Fatal(firstErr)
41	}
42}
43
44type RepoCreator func(bare bool) TestedRepo
45type RepoCleaner func(repos ...Repo)
46
47// Test suite for a Repo implementation
48func RepoTest(t *testing.T, creator RepoCreator, cleaner RepoCleaner) {
49	t.Run("Read/Write data", func(t *testing.T) {
50		repo := creator(false)
51		defer cleaner(repo)
52
53		data := []byte("hello")
54
55		h, err := repo.StoreData(data)
56		require.NoError(t, err)
57		assert.NotEmpty(t, h)
58
59		read, err := repo.ReadData(h)
60		require.NoError(t, err)
61		assert.Equal(t, data, read)
62	})
63
64	t.Run("Local config", func(t *testing.T) {
65		repo := creator(false)
66		defer cleaner(repo)
67
68		testConfig(t, repo.LocalConfig())
69	})
70}