all repos

mugit @ b8831a7

🐮 git server that your cow will love

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

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