git-module

fork of https://git.lin.moe/gogs/git-module

git clone git://git.lin.moe/fork/git-module.git

  1package git
  2
  3import (
  4	"context"
  5	"strconv"
  6	"testing"
  7	"time"
  8
  9	"github.com/stretchr/testify/assert"
 10)
 11
 12func TestRefShortName(t *testing.T) {
 13	tests := []struct {
 14		ref    string
 15		expVal string
 16	}{
 17		{
 18			ref:    "refs/heads/master",
 19			expVal: "master",
 20		},
 21		{
 22			ref:    "refs/tags/v1.0.0",
 23			expVal: "v1.0.0",
 24		},
 25		{
 26			ref:    "refs/pull/98",
 27			expVal: "refs/pull/98",
 28		},
 29	}
 30	for _, test := range tests {
 31		t.Run("", func(t *testing.T) {
 32			assert.Equal(t, test.expVal, RefShortName(test.ref))
 33		})
 34	}
 35}
 36
 37func TestRepository_ShowRefVerify(t *testing.T) {
 38	ctx := context.Background()
 39
 40	t.Run("reference does not exist", func(t *testing.T) {
 41		rev, err := testrepo.ShowRefVerify(ctx, "bad_reference")
 42		assert.NotNil(t, err)
 43		assert.Empty(t, rev)
 44	})
 45
 46	rev, err := testrepo.ShowRefVerify(ctx, "refs/heads/release-1.0")
 47	if err != nil {
 48		t.Fatal(err)
 49	}
 50
 51	assert.Equal(t, testrepoMarks[30].String(), rev)
 52}
 53
 54func TestRepository_BranchCommitID(t *testing.T) {
 55	ctx := context.Background()
 56
 57	t.Run("branch does not exist", func(t *testing.T) {
 58		rev, err := testrepo.BranchCommitID(ctx, "bad_branch")
 59		assert.NotNil(t, err)
 60		assert.Empty(t, rev)
 61	})
 62
 63	rev, err := testrepo.BranchCommitID(ctx, "release-1.0")
 64	if err != nil {
 65		t.Fatal(err)
 66	}
 67
 68	assert.Equal(t, testrepoMarks[30].String(), rev)
 69}
 70
 71func TestRepository_TagCommitID(t *testing.T) {
 72	ctx := context.Background()
 73
 74	t.Run("tag does not exist", func(t *testing.T) {
 75		rev, err := testrepo.TagCommitID(ctx, "bad_tag")
 76		assert.NotNil(t, err)
 77		assert.Empty(t, rev)
 78	})
 79
 80	rev, err := testrepo.TagCommitID(ctx, "v1.0.0")
 81	if err != nil {
 82		t.Fatal(err)
 83	}
 84
 85	assert.Equal(t, testrepoMarks[30].String(), rev)
 86}
 87
 88func TestRepository_HasReference(t *testing.T) {
 89	ctx := context.Background()
 90	tests := []struct {
 91		ref    string
 92		opt    ShowRefVerifyOptions
 93		expVal bool
 94	}{
 95		{
 96			ref:    RefsHeads + "master",
 97			expVal: true,
 98		},
 99		{
100			ref:    RefsTags + "v1.0.0",
101			expVal: true,
102		},
103		{
104			ref:    "master",
105			expVal: false,
106		},
107	}
108	for _, test := range tests {
109		t.Run("", func(t *testing.T) {
110			assert.Equal(t, test.expVal, testrepo.HasReference(ctx, test.ref, test.opt))
111		})
112	}
113}
114
115func TestRepository_HasBranch(t *testing.T) {
116	ctx := context.Background()
117	tests := []struct {
118		ref    string
119		opt    ShowRefVerifyOptions
120		expVal bool
121	}{
122		{
123			ref:    "master",
124			expVal: true,
125		},
126		{
127			ref:    RefsHeads + "master",
128			expVal: false,
129		},
130	}
131	for _, test := range tests {
132		t.Run("", func(t *testing.T) {
133			assert.Equal(t, test.expVal, testrepo.HasBranch(ctx, test.ref, test.opt))
134		})
135	}
136}
137
138func TestRepository_HasTag(t *testing.T) {
139	ctx := context.Background()
140	tests := []struct {
141		ref    string
142		opt    ShowRefVerifyOptions
143		expVal bool
144	}{
145		{
146			ref:    "v1.0.0",
147			expVal: true,
148		},
149		{
150			ref:    RefsTags + "v1.0.0",
151			expVal: false,
152		},
153	}
154	for _, test := range tests {
155		t.Run("", func(t *testing.T) {
156			assert.Equal(t, test.expVal, testrepo.HasTag(ctx, test.ref, test.opt))
157		})
158	}
159}
160
161func TestRepository_SymbolicRef(t *testing.T) {
162	ctx := context.Background()
163	r, cleanup, err := setupTempRepo()
164	if err != nil {
165		t.Fatal(err)
166	}
167	defer cleanup()
168
169	// Get HEAD
170	ref, err := r.SymbolicRef(ctx)
171	if err != nil {
172		t.Fatal(err)
173	}
174	assert.Equal(t, RefsHeads+"master", ref)
175
176	// Set a symbolic reference
177	_, err = r.SymbolicRef(ctx, SymbolicRefOptions{
178		Name: "TEST_REF",
179		Ref:  RefsHeads + "develop",
180	})
181	if err != nil {
182		t.Fatal(err)
183	}
184
185	// Get the symbolic reference we just set
186	ref, err = r.SymbolicRef(ctx, SymbolicRefOptions{
187		Name: "TEST_REF",
188	})
189	if err != nil {
190		t.Fatal(err)
191	}
192	assert.Equal(t, RefsHeads+"develop", ref)
193}
194
195func TestRepository_ShowRef(t *testing.T) {
196	ctx := context.Background()
197	tests := []struct {
198		opt     ShowRefOptions
199		expRefs []*Reference
200	}{
201		{
202			opt: ShowRefOptions{
203				Heads:    true,
204				Patterns: []string{"release-1.0"},
205			},
206			expRefs: []*Reference{
207				{
208					ID:      testrepoMarks[30].String(),
209					Refspec: "refs/heads/release-1.0",
210				},
211			},
212		}, {
213			opt: ShowRefOptions{
214				Tags:     true,
215				Patterns: []string{"v1.0.0"},
216			},
217			expRefs: []*Reference{
218				{
219					ID:      testrepoMarks[30].String(),
220					Refspec: "refs/tags/v1.0.0",
221				},
222			},
223		},
224	}
225	for _, test := range tests {
226		t.Run("", func(t *testing.T) {
227			refs, err := testrepo.ShowRef(ctx, test.opt)
228			if err != nil {
229				t.Fatal(err)
230			}
231
232			assert.Equal(t, test.expRefs, refs)
233		})
234	}
235}
236
237func TestRepository_Branches(t *testing.T) {
238	ctx := context.Background()
239	expBranches := map[string]bool{
240		"master":      true,
241		"develop":     true,
242		"release-1.0": true,
243	}
244	branches, err := testrepo.Branches(ctx)
245	if err != nil {
246		t.Fatal(err)
247	}
248	for _, b := range branches {
249		delete(expBranches, b)
250	}
251
252	if len(expBranches) > 0 {
253		t.Fatalf("expect to be empty but got %v", expBranches)
254	}
255}
256
257func TestRepository_DeleteBranch(t *testing.T) {
258	ctx := context.Background()
259	r, cleanup, err := setupTempRepo()
260	if err != nil {
261		t.Fatal(err)
262	}
263	defer cleanup()
264
265	tests := []struct {
266		opt DeleteBranchOptions
267	}{
268		{
269			opt: DeleteBranchOptions{
270				Force: false,
271			},
272		},
273		{
274			opt: DeleteBranchOptions{
275				Force: true,
276			},
277		},
278	}
279	for _, test := range tests {
280		t.Run("", func(t *testing.T) {
281			branch := strconv.Itoa(int(time.Now().UnixNano()))
282			err := r.Checkout(ctx, branch, CheckoutOptions{
283				BaseBranch: "master",
284			})
285			if err != nil {
286				t.Fatal(err)
287			}
288
289			assert.True(t, r.HasReference(ctx, RefsHeads+branch))
290
291			err = r.Checkout(ctx, "master")
292			if err != nil {
293				t.Fatal(err)
294			}
295
296			err = r.DeleteBranch(ctx, branch, test.opt)
297			if err != nil {
298				t.Fatal(err)
299			}
300
301			assert.False(t, r.HasReference(ctx, RefsHeads+branch))
302		})
303	}
304}