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	"os"
  6	"path/filepath"
  7	"testing"
  8
  9	"github.com/stretchr/testify/assert"
 10)
 11
 12func TestRepository(t *testing.T) {
 13	path := os.TempDir()
 14	r := &Repository{
 15		path: path,
 16	}
 17
 18	assert.Equal(t, path, r.Path())
 19}
 20
 21func TestInit(t *testing.T) {
 22	ctx := context.Background()
 23	tests := []struct {
 24		opt InitOptions
 25	}{
 26		{
 27			opt: InitOptions{},
 28		},
 29		{
 30			opt: InitOptions{
 31				Bare: true,
 32			},
 33		},
 34	}
 35	for _, test := range tests {
 36		t.Run("", func(t *testing.T) {
 37			// Make sure it does not blow up
 38			path := tempPath()
 39			defer func() {
 40				_ = os.RemoveAll(path)
 41			}()
 42
 43			if err := Init(ctx, path, test.opt); err != nil {
 44				t.Fatal(err)
 45			}
 46		})
 47	}
 48}
 49
 50func TestOpen(t *testing.T) {
 51	_, err := Open(testrepo.Path())
 52	assert.Nil(t, err)
 53
 54	_, err = Open(tempPath())
 55	assert.Equal(t, os.ErrNotExist, err)
 56}
 57
 58func TestClone(t *testing.T) {
 59	ctx := context.Background()
 60	tests := []struct {
 61		opt CloneOptions
 62	}{
 63		{
 64			opt: CloneOptions{},
 65		},
 66		{
 67			opt: CloneOptions{
 68				Mirror: true,
 69				Bare:   true,
 70				Quiet:  true,
 71			},
 72		},
 73		{
 74			opt: CloneOptions{
 75				Branch: "develop",
 76			},
 77		},
 78		{
 79			opt: CloneOptions{
 80				Depth: 1,
 81			},
 82		},
 83		{
 84			opt: CloneOptions{
 85				Branch: "develop",
 86				Depth:  1,
 87			},
 88		},
 89	}
 90	for _, test := range tests {
 91		t.Run("", func(t *testing.T) {
 92			// Make sure it does not blow up
 93			path := tempPath()
 94			defer func() {
 95				_ = os.RemoveAll(path)
 96			}()
 97
 98			if err := Clone(ctx, testrepo.Path(), path, test.opt); err != nil {
 99				t.Fatal(err)
100			}
101		})
102	}
103}
104
105func setupTempRepo() (_ *Repository, cleanup func(), err error) {
106	ctx := context.Background()
107	path := tempPath()
108	cleanup = func() {
109		_ = os.RemoveAll(path)
110	}
111	defer func() {
112		if err != nil {
113			cleanup()
114		}
115	}()
116
117	if err = Clone(ctx, testrepo.Path(), path); err != nil {
118		return nil, cleanup, err
119	}
120
121	r, err := Open(path)
122	if err != nil {
123		return nil, cleanup, err
124	}
125	return r, cleanup, nil
126}
127
128func TestRepository_Fetch(t *testing.T) {
129	ctx := context.Background()
130	r, cleanup, err := setupTempRepo()
131	if err != nil {
132		t.Fatal(err)
133	}
134	defer cleanup()
135
136	tests := []struct {
137		opt FetchOptions
138	}{
139		{
140			opt: FetchOptions{},
141		},
142		{
143			opt: FetchOptions{
144				Prune: true,
145			},
146		},
147	}
148	for _, test := range tests {
149		t.Run("", func(t *testing.T) {
150			// Make sure it does not blow up
151			if err := r.Fetch(ctx, test.opt); err != nil {
152				t.Fatal(err)
153			}
154		})
155	}
156}
157
158func TestRepository_Pull(t *testing.T) {
159	ctx := context.Background()
160	r, cleanup, err := setupTempRepo()
161	if err != nil {
162		t.Fatal(err)
163	}
164	defer cleanup()
165
166	tests := []struct {
167		opt PullOptions
168	}{
169		{
170			opt: PullOptions{},
171		},
172		{
173			opt: PullOptions{
174				Rebase: true,
175			},
176		},
177		{
178			opt: PullOptions{
179				All: true,
180			},
181		},
182		{
183			opt: PullOptions{
184				Remote: "origin",
185				Branch: "master",
186			},
187		},
188	}
189	for _, test := range tests {
190		t.Run("", func(t *testing.T) {
191			// Make sure it does not blow up
192			if err := r.Pull(ctx, test.opt); err != nil {
193				t.Fatal(err)
194			}
195		})
196	}
197}
198
199func TestRepository_Push(t *testing.T) {
200	ctx := context.Background()
201	r, cleanup, err := setupTempRepo()
202	if err != nil {
203		t.Fatal(err)
204	}
205	defer cleanup()
206
207	tests := []struct {
208		remote string
209		branch string
210		opt    PushOptions
211	}{
212		{
213			remote: "origin",
214			branch: "master",
215			opt:    PushOptions{},
216		},
217		{
218			remote: "origin",
219			branch: "master",
220			opt:    PushOptions{SetUpstream: true},
221		},
222	}
223	for _, test := range tests {
224		t.Run("", func(t *testing.T) {
225			// Make sure it does not blow up
226			if err := r.Push(ctx, test.remote, test.branch, test.opt); err != nil {
227				t.Fatal(err)
228			}
229		})
230	}
231}
232
233func TestRepository_Checkout(t *testing.T) {
234	ctx := context.Background()
235	r, cleanup, err := setupTempRepo()
236	if err != nil {
237		t.Fatal(err)
238	}
239	defer cleanup()
240
241	tests := []struct {
242		branch string
243		opt    CheckoutOptions
244	}{
245		{
246			branch: "develop",
247			opt:    CheckoutOptions{},
248		},
249		{
250			branch: "a-new-branch",
251			opt: CheckoutOptions{
252				BaseBranch: "master",
253			},
254		},
255	}
256	for _, test := range tests {
257		t.Run("", func(t *testing.T) {
258			// Make sure it does not blow up
259			if err := r.Checkout(ctx, test.branch, test.opt); err != nil {
260				t.Fatal(err)
261			}
262		})
263	}
264}
265
266func TestRepository_Reset(t *testing.T) {
267	ctx := context.Background()
268	r, cleanup, err := setupTempRepo()
269	if err != nil {
270		t.Fatal(err)
271	}
272	defer cleanup()
273
274	tests := []struct {
275		rev string
276		opt ResetOptions
277	}{
278		{
279			rev: testrepoMarks[27].String(),
280			opt: ResetOptions{
281				Hard: true,
282			},
283		},
284	}
285	for _, test := range tests {
286		t.Run("", func(t *testing.T) {
287			// Make sure it does not blow up
288			if err := r.Reset(ctx, test.rev, test.opt); err != nil {
289				t.Fatal(err)
290			}
291		})
292	}
293}
294
295func TestRepository_Move(t *testing.T) {
296	ctx := context.Background()
297	r, cleanup, err := setupTempRepo()
298	if err != nil {
299		t.Fatal(err)
300	}
301	defer cleanup()
302
303	tests := []struct {
304		src string
305		dst string
306		opt MoveOptions
307	}{
308		{
309			src: "run.sh",
310			dst: "runme.sh",
311			opt: MoveOptions{},
312		},
313	}
314	for _, test := range tests {
315		t.Run("", func(t *testing.T) {
316			// Make sure it does not blow up
317			if err := r.Move(ctx, test.src, test.dst, test.opt); err != nil {
318				t.Fatal(err)
319			}
320		})
321	}
322}
323
324func TestRepository_Add(t *testing.T) {
325	ctx := context.Background()
326	r, cleanup, err := setupTempRepo()
327	if err != nil {
328		t.Fatal(err)
329	}
330	defer cleanup()
331
332	// Generate a file
333	fpath := filepath.Join(r.Path(), "TESTFILE")
334	err = os.WriteFile(fpath, []byte("something"), 0600)
335	if err != nil {
336		t.Fatal(err)
337	}
338
339	// Make sure it does not blow up
340	if err := r.Add(ctx, AddOptions{
341		All:       true,
342		Pathspecs: []string{"TESTFILE"},
343	}); err != nil {
344		t.Fatal(err)
345	}
346}
347
348func TestRepository_Commit(t *testing.T) {
349	ctx := context.Background()
350	r, cleanup, err := setupTempRepo()
351	if err != nil {
352		t.Fatal(err)
353	}
354	defer cleanup()
355
356	committer := &Signature{
357		Name:  "alice",
358		Email: "alice@example.com",
359	}
360	author := &Signature{
361		Name:  "bob",
362		Email: "bob@example.com",
363	}
364	message := "Add a file"
365
366	t.Run("nothing to commit", func(t *testing.T) {
367		if err = r.Commit(ctx, committer, message, CommitOptions{
368			Author: author,
369		}); err != nil {
370			t.Fatal(err)
371		}
372	})
373
374	t.Run("committer is also the author", func(t *testing.T) {
375		// Generate a file and add to index
376		fpath := filepath.Join(r.Path(), "COMMITTER_IS_AUTHOR")
377		err = os.WriteFile(fpath, []byte("something"), 0600)
378		if err != nil {
379			t.Fatal(err)
380		}
381
382		if err := r.Add(ctx, AddOptions{
383			All: true,
384		}); err != nil {
385			t.Fatal(err)
386		}
387
388		// Make sure it does not blow up
389		if err = r.Commit(ctx, committer, message); err != nil {
390			t.Fatal(err)
391		}
392
393		// Verify the result
394		c, err := r.CatFileCommit(ctx, "master")
395		if err != nil {
396			t.Fatal(err)
397		}
398
399		assert.Equal(t, committer.Name, c.Committer.Name)
400		assert.Equal(t, committer.Email, c.Committer.Email)
401		assert.Equal(t, committer.Name, c.Author.Name)
402		assert.Equal(t, committer.Email, c.Author.Email)
403		assert.Equal(t, message+"\n", c.Message)
404	})
405
406	t.Run("committer is not the author", func(t *testing.T) {
407		// Generate a file and add to index
408		fpath := filepath.Join(r.Path(), "COMMITTER_IS_NOT_AUTHOR")
409		err = os.WriteFile(fpath, []byte("something"), 0600)
410		if err != nil {
411			t.Fatal(err)
412		}
413
414		if err := r.Add(ctx, AddOptions{
415			All: true,
416		}); err != nil {
417			t.Fatal(err)
418		}
419
420		// Make sure it does not blow up
421		if err = r.Commit(ctx, committer, message, CommitOptions{Author: author}); err != nil {
422			t.Fatal(err)
423		}
424
425		// Verify the result
426		c, err := r.CatFileCommit(ctx, "master")
427		if err != nil {
428			t.Fatal(err)
429		}
430
431		assert.Equal(t, committer.Name, c.Committer.Name)
432		assert.Equal(t, committer.Email, c.Committer.Email)
433		assert.Equal(t, author.Name, c.Author.Name)
434		assert.Equal(t, author.Email, c.Author.Email)
435		assert.Equal(t, message+"\n", c.Message)
436	})
437}
438
439func TestRepository_RevParse(t *testing.T) {
440	ctx := context.Background()
441	tests := []struct {
442		rev    string
443		expID  string
444		expErr error
445	}{
446		{
447			rev:    testrepoMarks[29].String()[:7],
448			expID:  testrepoMarks[29].String(),
449			expErr: nil,
450		},
451		{
452			rev:    "release-1.0",
453			expID:  testrepoMarks[30].String(),
454			expErr: nil,
455		},
456		{
457			rev:    "RELEASE_1.0",
458			expID:  testrepoMarks[12].String(),
459			expErr: nil,
460		},
461		{
462			rev:    "refs/heads/release-1.0",
463			expID:  testrepoMarks[30].String(),
464			expErr: nil,
465		},
466		{
467			rev:    "refs/tags/RELEASE_1.0",
468			expID:  testrepoMarks[12].String(),
469			expErr: nil,
470		},
471
472		{
473			rev:    "refs/tags/404",
474			expID:  "",
475			expErr: ErrRevisionNotExist,
476		},
477	}
478	for _, test := range tests {
479		t.Run("", func(t *testing.T) {
480			id, err := testrepo.RevParse(ctx, test.rev)
481			assert.Equal(t, test.expErr, err)
482			assert.Equal(t, test.expID, id)
483		})
484	}
485}
486
487func TestRepository_CountObjects(t *testing.T) {
488	ctx := context.Background()
489	// Make sure it does not blow up
490	_, err := testrepo.CountObjects(ctx, CountObjectsOptions{})
491	if err != nil {
492		t.Fatal(err)
493	}
494}
495
496func TestRepository_Fsck(t *testing.T) {
497	ctx := context.Background()
498	// Make sure it does not blow up
499	err := testrepo.Fsck(ctx, FsckOptions{})
500	if err != nil {
501		t.Fatal(err)
502	}
503}
504
505func TestRepository_ObjectFormat(t *testing.T) {
506	ctx := context.Background()
507	obf, err := testrepo.ObjectFormat(ctx)
508	if err != nil {
509		t.Fatal(err)
510	}
511	assert.Equal(t, obf, testrepoObjectFormat)
512}