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	"errors"
  6	"fmt"
  7	"testing"
  8	"time"
  9
 10	"github.com/stretchr/testify/assert"
 11)
 12
 13func Test_escapePath(t *testing.T) {
 14	tests := []struct {
 15		path    string
 16		expPath string
 17	}{
 18		{
 19			path:    "",
 20			expPath: "",
 21		},
 22		{
 23			path:    "normal",
 24			expPath: "normal",
 25		},
 26		{
 27			path:    ":normal",
 28			expPath: "\\:normal",
 29		},
 30	}
 31	for _, test := range tests {
 32		t.Run("", func(t *testing.T) {
 33			assert.Equal(t, test.expPath, escapePath(test.path))
 34		})
 35	}
 36}
 37
 38func TestRepository_CatFileCommit(t *testing.T) {
 39	ctx := context.Background()
 40
 41	t.Run("invalid revision", func(t *testing.T) {
 42		c, err := testrepo.CatFileCommit(ctx, "bad_revision")
 43		assert.Equal(t, ErrRevisionNotExist, err)
 44		assert.Nil(t, c)
 45	})
 46
 47	c, err := testrepo.CatFileCommit(ctx, testrepoMarks[31].String())
 48	if err != nil {
 49		t.Fatal(err)
 50	}
 51
 52	assert.Equal(t, testrepoMarks[31].String(), c.ID.String())
 53	assert.Equal(t, "Add a symlink\n", c.Message)
 54}
 55
 56func TestRepository_BranchCommit(t *testing.T) {
 57	ctx := context.Background()
 58
 59	t.Run("invalid branch", func(t *testing.T) {
 60		c, err := testrepo.BranchCommit(ctx, "refs/heads/release-1.0")
 61		assert.Equal(t, ErrRevisionNotExist, err)
 62		assert.Nil(t, c)
 63	})
 64
 65	c, err := testrepo.BranchCommit(ctx, "release-1.0")
 66	if err != nil {
 67		t.Fatal(err)
 68	}
 69
 70	assert.Equal(t, testrepoMarks[30].String(), c.ID.String())
 71	assert.Equal(t, "Rename shell script\n", c.Message)
 72}
 73
 74func TestRepository_TagCommit(t *testing.T) {
 75	ctx := context.Background()
 76
 77	t.Run("invalid branch", func(t *testing.T) {
 78		c, err := testrepo.BranchCommit(ctx, "refs/tags/v1.0.0")
 79		assert.Equal(t, ErrRevisionNotExist, err)
 80		assert.Nil(t, c)
 81	})
 82
 83	c, err := testrepo.BranchCommit(ctx, "release-1.0")
 84	if err != nil {
 85		t.Fatal(err)
 86	}
 87
 88	assert.Equal(t, testrepoMarks[30].String(), c.ID.String())
 89	assert.Equal(t, "Rename shell script\n", c.Message)
 90}
 91
 92func TestRepository_Log(t *testing.T) {
 93	ctx := context.Background()
 94	tests := []struct {
 95		rev          string
 96		opt          LogOptions
 97		expCommitIDs []string
 98	}{
 99		{
100			rev: testrepoMarks[30].String(),
101			opt: LogOptions{
102				Since: time.Unix(1581250680, 0),
103			},
104			expCommitIDs: []string{
105				testrepoMarks[30].String(),
106				testrepoMarks[29].String(),
107			},
108		},
109		{
110			rev: testrepoMarks[30].String(),
111			opt: LogOptions{
112				Since: time.Now().AddDate(100, 0, 0),
113			},
114			expCommitIDs: []string{},
115		},
116	}
117	for _, test := range tests {
118		t.Run("", func(t *testing.T) {
119			commits, err := testrepo.Log(ctx, test.rev, test.opt)
120			if err != nil {
121				t.Fatal(err)
122			}
123
124			assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))
125		})
126	}
127}
128
129func TestRepository_CommitByRevision(t *testing.T) {
130	ctx := context.Background()
131
132	t.Run("invalid revision", func(t *testing.T) {
133		c, err := testrepo.CommitByRevision(ctx, "bad_revision")
134		assert.Equal(t, ErrRevisionNotExist, err)
135		assert.Nil(t, c)
136	})
137
138	tests := []struct {
139		rev   string
140		opt   CommitByRevisionOptions
141		expID string
142	}{
143		{
144			rev:   testrepoMarks[29].String()[:7],
145			expID: testrepoMarks[29].String(),
146		},
147	}
148	for _, test := range tests {
149		t.Run("", func(t *testing.T) {
150			c, err := testrepo.CommitByRevision(ctx, test.rev, test.opt)
151			if err != nil {
152				t.Fatal(err)
153			}
154
155			assert.Equal(t, test.expID, c.ID.String())
156		})
157	}
158}
159
160func TestRepository_CommitsSince(t *testing.T) {
161	ctx := context.Background()
162	tests := []struct {
163		rev          string
164		since        time.Time
165		opt          CommitsSinceOptions
166		expCommitIDs []string
167	}{
168		{
169			rev:   testrepoMarks[30].String(),
170			since: time.Unix(1581250680, 0),
171			expCommitIDs: []string{
172				testrepoMarks[30].String(),
173				testrepoMarks[29].String(),
174			},
175		},
176		{
177			rev:          testrepoMarks[30].String(),
178			since:        time.Now().AddDate(100, 0, 0),
179			expCommitIDs: []string{},
180		},
181	}
182	for _, test := range tests {
183		t.Run("", func(t *testing.T) {
184			commits, err := testrepo.CommitsSince(ctx, test.rev, test.since, test.opt)
185			if err != nil {
186				t.Fatal(err)
187			}
188
189			assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))
190		})
191	}
192}
193
194func TestRepository_DiffNameOnly(t *testing.T) {
195	ctx := context.Background()
196	tests := []struct {
197		base     string
198		head     string
199		opt      DiffNameOnlyOptions
200		expFiles []string
201	}{
202		{
203			base:     testrepoMarks[26].String(),
204			head:     testrepoMarks[27].String(),
205			expFiles: []string{"fix.txt"},
206		},
207		{
208			base: testrepoMarks[24].String(),
209			head: testrepoMarks[27].String(),
210			opt: DiffNameOnlyOptions{
211				NeedsMergeBase: true,
212			},
213			expFiles: []string{"fix.txt", "pom.xml", "src/test/java/com/github/AppTest.java"},
214		},
215
216		{
217			base: testrepoMarks[24].String(),
218			head: testrepoMarks[27].String(),
219			opt: DiffNameOnlyOptions{
220				Path: "src",
221			},
222			expFiles: []string{"src/test/java/com/github/AppTest.java"},
223		},
224		{
225			base: testrepoMarks[24].String(),
226			head: testrepoMarks[27].String(),
227			opt: DiffNameOnlyOptions{
228				Path: "resources",
229			},
230			expFiles: []string{},
231		},
232	}
233	for _, test := range tests {
234		t.Run("", func(t *testing.T) {
235			files, err := testrepo.DiffNameOnly(ctx, test.base, test.head, test.opt)
236			if err != nil {
237				t.Fatal(err)
238			}
239
240			assert.Equal(t, test.expFiles, files)
241		})
242	}
243}
244
245func TestRepository_RevListCount(t *testing.T) {
246	ctx := context.Background()
247
248	t.Run("no refspecs", func(t *testing.T) {
249		count, err := testrepo.RevListCount(ctx, []string{})
250		assert.Equal(t, errors.New("must have at least one refspec"), err)
251		assert.Zero(t, count)
252	})
253
254	tests := []struct {
255		refspecs []string
256		opt      RevListCountOptions
257		expCount int64
258	}{
259		{
260			refspecs: []string{testrepoMarks[1].String()},
261			expCount: 1,
262		},
263		{
264			refspecs: []string{testrepoMarks[5].String()},
265			expCount: 5,
266		},
267		{
268			refspecs: []string{testrepoMarks[27].String()},
269			expCount: 27,
270		},
271
272		{
273			refspecs: []string{testrepoMarks[21].String()},
274			opt: RevListCountOptions{
275				Path: "README.txt",
276			},
277			expCount: 3,
278		},
279		{
280			refspecs: []string{testrepoMarks[21].String()},
281			opt: RevListCountOptions{
282				Path: "resources",
283			},
284			expCount: 1,
285		},
286	}
287	for _, test := range tests {
288		t.Run("", func(t *testing.T) {
289			count, err := testrepo.RevListCount(ctx, test.refspecs, test.opt)
290			if err != nil {
291				t.Fatal(err)
292			}
293
294			assert.Equal(t, test.expCount, count)
295		})
296	}
297}
298
299func TestRepository_RevList(t *testing.T) {
300	ctx := context.Background()
301
302	t.Run("no refspecs", func(t *testing.T) {
303		commits, err := testrepo.RevList(ctx, []string{})
304		assert.Equal(t, errors.New("must have at least one refspec"), err)
305		assert.Nil(t, commits)
306	})
307
308	tests := []struct {
309		refspecs     []string
310		opt          RevListOptions
311		expCommitIDs []string
312	}{
313		{
314			refspecs: []string{fmt.Sprintf("%s...%s", testrepoMarks[24].String(), testrepoMarks[27].String())},
315			expCommitIDs: []string{
316				testrepoMarks[27].String(),
317				testrepoMarks[26].String(),
318				testrepoMarks[25].String(),
319			},
320		},
321		{
322			refspecs: []string{fmt.Sprintf("%s...%s", testrepoMarks[24].String(), testrepoMarks[27].String())},
323			opt: RevListOptions{
324				Path: "src",
325			},
326			expCommitIDs: []string{
327				testrepoMarks[25].String(),
328			},
329		},
330	}
331	for _, test := range tests {
332		t.Run("", func(t *testing.T) {
333			commits, err := testrepo.RevList(ctx, test.refspecs, test.opt)
334			if err != nil {
335				t.Fatal(err)
336			}
337
338			assert.Equal(t, test.expCommitIDs, commitsToIDs(commits))
339		})
340	}
341}
342
343func TestRepository_LatestCommitTime(t *testing.T) {
344	ctx := context.Background()
345	tests := []struct {
346		opt     LatestCommitTimeOptions
347		expTime time.Time
348	}{
349		{
350			opt: LatestCommitTimeOptions{
351				Branch: "release-1.0",
352			},
353			expTime: time.Unix(1581256638, 0),
354		},
355	}
356	for _, test := range tests {
357		t.Run("", func(t *testing.T) {
358			got, err := testrepo.LatestCommitTime(ctx, test.opt)
359			if err != nil {
360				t.Fatal(err)
361			}
362
363			assert.Equal(t, test.expTime.Unix(), got.Unix())
364		})
365	}
366}