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	"testing"
  6
  7	"github.com/stretchr/testify/assert"
  8)
  9
 10func TestCommit(t *testing.T) {
 11	ctx := context.Background()
 12	c, err := testrepo.CatFileCommit(ctx, testrepoMarks[22].String())
 13	if err != nil {
 14		t.Fatal(err)
 15	}
 16	t.Run("ID", func(t *testing.T) {
 17		assert.Equal(t, testrepoMarks[22].String(), c.ID.String())
 18	})
 19
 20	t.Run("Summary", func(t *testing.T) {
 21		assert.Equal(t, "Merge pull request #35 from githubtraining/travis-yml-docker", c.Summary())
 22	})
 23}
 24
 25func TestCommit_Parent(t *testing.T) {
 26	ctx := context.Background()
 27	c, err := testrepo.CatFileCommit(ctx, testrepoMarks[22].String())
 28	if err != nil {
 29		t.Fatal(err)
 30	}
 31
 32	t.Run("ParentsCount", func(t *testing.T) {
 33		assert.Equal(t, 2, c.ParentsCount())
 34	})
 35
 36	t.Run("Parent", func(t *testing.T) {
 37		t.Run("no such parent", func(t *testing.T) {
 38			_, err := c.Parent(ctx, c.ParentsCount()+1)
 39			assert.Equal(t, ErrParentNotExist, err)
 40		})
 41
 42		tests := []struct {
 43			n           int
 44			expParentID string
 45		}{
 46			{
 47				n:           0,
 48				expParentID: testrepoMarks[20].String(),
 49			},
 50			{
 51				n:           1,
 52				expParentID: testrepoMarks[21].String(),
 53			},
 54		}
 55		for _, test := range tests {
 56			t.Run("", func(t *testing.T) {
 57				p, err := c.Parent(ctx, test.n)
 58				if err != nil {
 59					t.Fatal(err)
 60				}
 61				assert.Equal(t, test.expParentID, p.ID.String())
 62			})
 63		}
 64	})
 65}
 66
 67func TestCommit_CommitByPath(t *testing.T) {
 68	ctx := context.Background()
 69	tests := []struct {
 70		id          string
 71		opt         CommitByRevisionOptions
 72		expCommitID string
 73	}{
 74		{
 75			id: testrepoMarks[12].String(),
 76			opt: CommitByRevisionOptions{
 77				Path: "", // No path gets back to the commit itself
 78			},
 79			expCommitID: testrepoMarks[12].String(),
 80		},
 81		{
 82			id: testrepoMarks[12].String(),
 83			opt: CommitByRevisionOptions{
 84				Path: "resources/labels.properties",
 85			},
 86			expCommitID: testrepoMarks[1].String(),
 87		},
 88	}
 89	for _, test := range tests {
 90		t.Run("", func(t *testing.T) {
 91			c, err := testrepo.CatFileCommit(ctx, test.id)
 92			if err != nil {
 93				t.Fatal(err)
 94			}
 95
 96			cc, err := c.CommitByPath(ctx, test.opt)
 97			if err != nil {
 98				t.Fatal(err)
 99			}
100
101			assert.Equal(t, test.expCommitID, cc.ID.String())
102		})
103	}
104}
105
106// commitsToIDs returns a list of IDs for given commits.
107func commitsToIDs(commits []*Commit) []string {
108	ids := make([]string, len(commits))
109	for i := range commits {
110		ids[i] = commits[i].ID.String()
111	}
112	return ids
113}
114
115func TestCommit_CommitsByPage(t *testing.T) {
116	ctx := context.Background()
117	// There are at most 5 commits can be used for pagination before this commit.
118	c, err := testrepo.CatFileCommit(ctx, testrepoMarks[5].String())
119	if err != nil {
120		t.Fatal(err)
121	}
122
123	tests := []struct {
124		page         int
125		size         int
126		opt          CommitsByPageOptions
127		expCommitIDs []string
128	}{
129		{
130			page: 0,
131			size: 2,
132			expCommitIDs: []string{
133				testrepoMarks[5].String(),
134				testrepoMarks[4].String(),
135			},
136		},
137		{
138			page: 1,
139			size: 2,
140			expCommitIDs: []string{
141				testrepoMarks[5].String(),
142				testrepoMarks[4].String(),
143			},
144		},
145		{
146			page: 2,
147			size: 2,
148			expCommitIDs: []string{
149				testrepoMarks[3].String(),
150				testrepoMarks[2].String(),
151			},
152		},
153		{
154			page: 3,
155			size: 2,
156			expCommitIDs: []string{
157				testrepoMarks[1].String(),
158			},
159		},
160		{
161			page:         4,
162			size:         2,
163			expCommitIDs: []string{},
164		},
165
166		{
167			page: 2,
168			size: 2,
169			opt: CommitsByPageOptions{
170				Path: "src",
171			},
172			expCommitIDs: []string{
173				testrepoMarks[1].String(),
174			},
175		},
176	}
177	for _, test := range tests {
178		t.Run("", func(t *testing.T) {
179			commits, err := c.CommitsByPage(ctx, test.page, test.size, test.opt)
180			if err != nil {
181				t.Fatal(err)
182			}
183
184			assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))
185		})
186	}
187}
188
189func TestCommit_SearchCommits(t *testing.T) {
190	ctx := context.Background()
191	tests := []struct {
192		id           string
193		pattern      string
194		opt          SearchCommitsOptions
195		expCommitIDs []string
196	}{
197		{
198			id:      testrepoMarks[12].String(),
199			pattern: "",
200			expCommitIDs: []string{
201				testrepoMarks[12].String(),
202				testrepoMarks[11].String(),
203				testrepoMarks[10].String(),
204				testrepoMarks[9].String(),
205				testrepoMarks[8].String(),
206				testrepoMarks[7].String(),
207				testrepoMarks[6].String(),
208				testrepoMarks[5].String(),
209				testrepoMarks[4].String(),
210				testrepoMarks[3].String(),
211				testrepoMarks[2].String(),
212				testrepoMarks[1].String(),
213			},
214		},
215		{
216			id:      testrepoMarks[12].String(),
217			pattern: "",
218			opt: SearchCommitsOptions{
219				MaxCount: 3,
220			},
221			expCommitIDs: []string{
222				testrepoMarks[12].String(),
223				testrepoMarks[11].String(),
224				testrepoMarks[10].String(),
225			},
226		},
227
228		{
229			id:      testrepoMarks[12].String(),
230			pattern: "feature",
231			expCommitIDs: []string{
232				testrepoMarks[12].String(),
233				testrepoMarks[10].String(),
234			},
235		},
236		{
237			id:      testrepoMarks[12].String(),
238			pattern: "feature",
239			opt: SearchCommitsOptions{
240				MaxCount: 1,
241			},
242			expCommitIDs: []string{
243				testrepoMarks[12].String(),
244			},
245		},
246
247		{
248			id:      testrepoMarks[12].String(),
249			pattern: "add.*",
250			opt: SearchCommitsOptions{
251				Path: "src",
252			},
253			expCommitIDs: []string{
254				testrepoMarks[10].String(),
255				testrepoMarks[9].String(),
256				testrepoMarks[6].String(),
257				testrepoMarks[2].String(),
258				testrepoMarks[1].String(),
259			},
260		},
261		{
262			id:      testrepoMarks[12].String(),
263			pattern: "add.*",
264			opt: SearchCommitsOptions{
265				MaxCount: 2,
266				Path:     "src",
267			},
268			expCommitIDs: []string{
269				testrepoMarks[10].String(),
270				testrepoMarks[9].String(),
271			},
272		},
273	}
274	for _, test := range tests {
275		t.Run("", func(t *testing.T) {
276			c, err := testrepo.CatFileCommit(ctx, test.id)
277			if err != nil {
278				t.Fatal(err)
279			}
280
281			commits, err := c.SearchCommits(ctx, test.pattern, test.opt)
282			if err != nil {
283				t.Fatal(err)
284			}
285
286			assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))
287		})
288	}
289}
290
291func TestCommit_ShowNameStatus(t *testing.T) {
292	ctx := context.Background()
293	tests := []struct {
294		id        string
295		opt       ShowNameStatusOptions
296		expStatus *NameStatus
297	}{
298		{
299			id: testrepoMarks[1].String(),
300			expStatus: &NameStatus{
301				Added: []string{
302					"README.txt",
303					"resources/labels.properties",
304					"src/Main.groovy",
305				},
306			},
307		},
308		{
309			id: testrepoMarks[2].String(),
310			expStatus: &NameStatus{
311				Modified: []string{
312					"src/Main.groovy",
313				},
314			},
315		},
316		{
317			id: testrepoMarks[3].String(),
318			expStatus: &NameStatus{
319				Added: []string{
320					"src/Square.groovy",
321				},
322				Modified: []string{
323					"src/Main.groovy",
324				},
325			},
326		},
327		{
328			id: testrepoMarks[27].String(),
329			expStatus: &NameStatus{
330				Removed: []string{
331					"fix.txt",
332				},
333			},
334		},
335	}
336	for _, test := range tests {
337		t.Run("", func(t *testing.T) {
338			c, err := testrepo.CatFileCommit(ctx, test.id)
339			if err != nil {
340				t.Fatal(err)
341			}
342
343			status, err := c.ShowNameStatus(ctx, test.opt)
344			if err != nil {
345				t.Fatal(err)
346			}
347
348			assert.Equal(t, test.expStatus, status)
349		})
350	}
351}
352
353func TestCommit_CommitsCount(t *testing.T) {
354	ctx := context.Background()
355	tests := []struct {
356		id       string
357		opt      RevListCountOptions
358		expCount int64
359	}{
360		{
361			id:       testrepoMarks[1].String(),
362			expCount: 1,
363		},
364		{
365			id:       testrepoMarks[5].String(),
366			expCount: 5,
367		},
368		{
369			id:       testrepoMarks[27].String(),
370			expCount: 27,
371		},
372
373		{
374			id: testrepoMarks[21].String(),
375			opt: RevListCountOptions{
376				Path: "README.txt",
377			},
378			expCount: 3,
379		},
380		{
381			id: testrepoMarks[21].String(),
382			opt: RevListCountOptions{
383				Path: "resources",
384			},
385			expCount: 1,
386		},
387	}
388	for _, test := range tests {
389		t.Run("", func(t *testing.T) {
390			c, err := testrepo.CatFileCommit(ctx, test.id)
391			if err != nil {
392				t.Fatal(err)
393			}
394
395			count, err := c.CommitsCount(ctx, test.opt)
396			if err != nil {
397				t.Fatal(err)
398			}
399
400			assert.Equal(t, test.expCount, count)
401		})
402	}
403}
404
405func TestCommit_FilesChangedAfter(t *testing.T) {
406	ctx := context.Background()
407	tests := []struct {
408		id       string
409		after    string
410		opt      DiffNameOnlyOptions
411		expFiles []string
412	}{
413		{
414			id:       testrepoMarks[27].String(),
415			after:    testrepoMarks[26].String(),
416			expFiles: []string{"fix.txt"},
417		},
418		{
419			id:       testrepoMarks[27].String(),
420			after:    testrepoMarks[24].String(),
421			expFiles: []string{"fix.txt", "pom.xml", "src/test/java/com/github/AppTest.java"},
422		},
423
424		{
425			id:    testrepoMarks[27].String(),
426			after: testrepoMarks[24].String(),
427			opt: DiffNameOnlyOptions{
428				Path: "src",
429			},
430			expFiles: []string{"src/test/java/com/github/AppTest.java"},
431		},
432		{
433			id:    testrepoMarks[27].String(),
434			after: testrepoMarks[24].String(),
435			opt: DiffNameOnlyOptions{
436				Path: "resources",
437			},
438			expFiles: []string{},
439		},
440	}
441	for _, test := range tests {
442		t.Run("", func(t *testing.T) {
443			c, err := testrepo.CatFileCommit(ctx, test.id)
444			if err != nil {
445				t.Fatal(err)
446			}
447
448			files, err := c.FilesChangedAfter(ctx, test.after, test.opt)
449			if err != nil {
450				t.Fatal(err)
451			}
452
453			assert.Equal(t, test.expFiles, files)
454		})
455	}
456}
457
458func TestCommit_CommitsAfter(t *testing.T) {
459	ctx := context.Background()
460	tests := []struct {
461		id           string
462		after        string
463		opt          RevListOptions
464		expCommitIDs []string
465	}{
466		{
467			id:    testrepoMarks[27].String(),
468			after: testrepoMarks[24].String(),
469			expCommitIDs: []string{
470				testrepoMarks[27].String(),
471				testrepoMarks[26].String(),
472				testrepoMarks[25].String(),
473			},
474		},
475		{
476			id:    testrepoMarks[27].String(),
477			after: testrepoMarks[24].String(),
478			opt: RevListOptions{
479				Path: "src",
480			},
481			expCommitIDs: []string{
482				testrepoMarks[25].String(),
483			},
484		},
485	}
486	for _, test := range tests {
487		t.Run("", func(t *testing.T) {
488			c, err := testrepo.CatFileCommit(ctx, test.id)
489			if err != nil {
490				t.Fatal(err)
491			}
492
493			commits, err := c.CommitsAfter(ctx, test.after, test.opt)
494			if err != nil {
495				t.Fatal(err)
496			}
497
498			assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))
499		})
500	}
501}
502
503func TestCommit_Ancestors(t *testing.T) {
504	ctx := context.Background()
505	tests := []struct {
506		id           string
507		opt          LogOptions
508		expCommitIDs []string
509	}{
510		{
511			id: testrepoMarks[12].String(),
512			opt: LogOptions{
513				MaxCount: 3,
514			},
515			expCommitIDs: []string{
516				testrepoMarks[11].String(),
517				testrepoMarks[10].String(),
518				testrepoMarks[9].String(),
519			},
520		},
521		{
522			id:           testrepoMarks[1].String(),
523			expCommitIDs: []string{},
524		},
525	}
526	for _, test := range tests {
527		t.Run("", func(t *testing.T) {
528			c, err := testrepo.CatFileCommit(ctx, test.id)
529			if err != nil {
530				t.Fatal(err)
531			}
532
533			commits, err := c.Ancestors(ctx, test.opt)
534			if err != nil {
535				t.Fatal(err)
536			}
537
538			assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))
539		})
540	}
541}
542
543func TestCommit_IsImageFile(t *testing.T) {
544	ctx := context.Background()
545
546	t.Run("not a blob", func(t *testing.T) {
547		c, err := testrepo.CatFileCommit(ctx, testrepoMarks[29].String())
548		if err != nil {
549			t.Fatal(err)
550		}
551
552		isImage, err := c.IsImageFile(ctx, "gogs/docs-api")
553		if err != nil {
554			t.Fatal(err)
555		}
556		assert.False(t, isImage)
557	})
558
559	tests := []struct {
560		id     string
561		name   string
562		expVal bool
563	}{
564		{
565			id:     testrepoMarks[28].String(),
566			name:   "README.txt",
567			expVal: false,
568		},
569		{
570			id:     testrepoMarks[28].String(),
571			name:   "img/sourcegraph.png",
572			expVal: true,
573		},
574	}
575	for _, test := range tests {
576		t.Run("", func(t *testing.T) {
577			c, err := testrepo.CatFileCommit(ctx, test.id)
578			if err != nil {
579				t.Fatal(err)
580			}
581
582			isImage, err := c.IsImageFile(ctx, test.name)
583			if err != nil {
584				t.Fatal(err)
585			}
586
587			assert.Equal(t, test.expVal, isImage)
588		})
589	}
590}
591
592func TestCommit_IsImageFileByIndex(t *testing.T) {
593	ctx := context.Background()
594
595	t.Run("not a blob", func(t *testing.T) {
596		c, err := testrepo.CatFileCommit(ctx, testrepoMarks[29].String())
597		if err != nil {
598			t.Fatal(err)
599		}
600
601		isImage, err := c.IsImageFileByIndex(ctx, testrepoMarks[40].String()) // "gogs"
602		if err != nil {
603			t.Fatal(err)
604		}
605		assert.False(t, isImage)
606	})
607
608	tests := []struct {
609		id     string
610		index  string
611		expVal bool
612	}{
613		{
614			id:     testrepoMarks[28].String(),
615			index:  testrepoMarks[39].String(), // "README.txt"
616			expVal: false,
617		},
618		{
619			id:     testrepoMarks[28].String(),
620			index:  testrepoMarks[49].String(), // "img/sourcegraph.png"
621			expVal: true,
622		},
623	}
624	for _, test := range tests {
625		t.Run("", func(t *testing.T) {
626			c, err := testrepo.CatFileCommit(ctx, test.id)
627			if err != nil {
628				t.Fatal(err)
629			}
630
631			isImage, err := c.IsImageFileByIndex(ctx, test.index)
632			if err != nil {
633				t.Fatal(err)
634			}
635
636			assert.Equal(t, test.expVal, isImage)
637		})
638	}
639}