all repos

mugit @ 8e0f8085e2aefa34ff3e5d695c1a0bed4a976eda

🐮 git server that your cow will love

mugit/internal/git/testutil_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
test: add missing tests (#5)..., 2 months ago
1
package git
2
3
import (
4
	"os"
5
	"path/filepath"
6
	"testing"
7
	"time"
8
9
	"github.com/go-git/go-git/v5"
10
	"github.com/go-git/go-git/v5/plumbing"
11
	"github.com/go-git/go-git/v5/plumbing/object"
12
	"olexsmir.xyz/x/is"
13
)
14
15
type testRepo struct {
16
	tb   testing.TB
17
	r    *git.Repository
18
	path string
19
}
20
21
func newTestRepo(tb testing.TB) *testRepo {
22
	tb.Helper()
23
	path := tb.TempDir()
24
25
	// creates non-bare repo, bare repos require to keep track of HEAD manually
26
	r, err := git.PlainInit(path, false)
27
	is.Err(tb, err, nil)
28
29
	cfg, err := r.Config()
30
	is.Err(tb, err, nil)
31
32
	cfg.User.Name = "Test User"
33
	cfg.User.Email = "test@test.local"
34
	is.Err(tb, r.SetConfig(cfg), nil)
35
36
	return &testRepo{tb: tb, path: path, r: r}
37
}
38
39
func (t *testRepo) commitFileAt(name, content, msg string, when time.Time) plumbing.Hash {
40
	t.tb.Helper()
41
42
	filePath := filepath.Join(t.path, name)
43
	is.Err(t.tb, os.MkdirAll(filepath.Dir(filePath), 0o755), nil)
44
	is.Err(t.tb, os.WriteFile(filePath, []byte(content), 0o644), nil)
45
46
	wt, err := t.r.Worktree()
47
	is.Err(t.tb, err, nil)
48
49
	_, err = wt.Add(name)
50
	is.Err(t.tb, err, nil)
51
52
	hash, err := wt.Commit(msg, &git.CommitOptions{
53
		Author: &object.Signature{
54
			Name:  "Test User",
55
			Email: "test@test.local",
56
			When:  when,
57
		},
58
	})
59
	is.Err(t.tb, err, nil)
60
	return hash
61
}
62
63
func (t *testRepo) commitFile(name, content, msg string) plumbing.Hash {
64
	t.tb.Helper()
65
	return t.commitFileAt(name, content, msg, time.Now())
66
}
67
68
func (t *testRepo) deleteFile(name, msg string) plumbing.Hash {
69
	t.tb.Helper()
70
71
	wt, err := t.r.Worktree()
72
	is.Err(t.tb, err, nil)
73
74
	_, err = wt.Remove(name)
75
	is.Err(t.tb, err, nil)
76
77
	hash, err := wt.Commit(msg, &git.CommitOptions{
78
		Author: &object.Signature{
79
			Name:  "Test User",
80
			Email: "test@test.local",
81
			When:  time.Now(),
82
		},
83
	})
84
	is.Err(t.tb, err, nil)
85
	return hash
86
}
87
88
func (t *testRepo) createBranch(name string, hash plumbing.Hash) {
89
	t.tb.Helper()
90
	ref := plumbing.NewHashReference(plumbing.NewBranchReferenceName(name), hash)
91
	is.Err(t.tb, t.r.Storer.SetReference(ref), nil)
92
}
93
94
func (t *testRepo) createTag(name string, hash plumbing.Hash) {
95
	t.tb.Helper()
96
	ref := plumbing.NewHashReference(plumbing.NewTagReferenceName(name), hash)
97
	is.Err(t.tb, t.r.Storer.SetReference(ref), nil)
98
}
99
100
func (t *testRepo) createAnnotatedTag(name, msg string, hash plumbing.Hash, when time.Time) {
101
	t.tb.Helper()
102
	_, err := t.r.CreateTag(name, hash, &git.CreateTagOptions{
103
		Tagger: &object.Signature{
104
			Name:  "Test User",
105
			Email: "test@test.local",
106
			When:  when,
107
		},
108
		Message: msg,
109
	})
110
	is.Err(t.tb, err, nil)
111
}
112
113
func (t *testRepo) open(ref ...string) *Repo {
114
	t.tb.Helper()
115
	re := ""
116
	if len(ref) == 1 {
117
		re = ref[0]
118
	}
119
	r, err := Open(t.path, re)
120
	is.Err(t.tb, err, nil)
121
	return r
122
}