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	"bytes"
  5	"context"
  6	"errors"
  7	"fmt"
  8	"strings"
  9	"testing"
 10
 11	"github.com/stretchr/testify/assert"
 12)
 13
 14func TestRepository_Diff(t *testing.T) {
 15	ctx := context.Background()
 16	tests := []struct {
 17		rev          string
 18		maxFiles     int
 19		maxFileLines int
 20		maxLineChars int
 21		opt          DiffOptions
 22		expDiff      *Diff
 23	}{
 24		{
 25			rev: testrepoMarks[27].String(),
 26			expDiff: &Diff{
 27				Files: []*DiffFile{
 28					{
 29						Name:         "fix.txt",
 30						Type:         DiffFileDelete,
 31						Index:        testEmptyShaID,
 32						OldIndex:     testrepoMarks[54].String(),
 33						Sections:     nil,
 34						numAdditions: 0,
 35						numDeletions: 0,
 36						oldName:      "fix.txt",
 37						isBinary:     false,
 38						isSubmodule:  false,
 39						isIncomplete: false,
 40						mode:         0100644,
 41						oldMode:      0100644,
 42					},
 43				},
 44				totalAdditions: 0,
 45				totalDeletions: 0,
 46				isIncomplete:   false,
 47			},
 48		},
 49		{
 50			rev: testrepoMarks[1].String(),
 51			expDiff: &Diff{
 52				Files: []*DiffFile{
 53					{
 54						Name:     "README.txt",
 55						Type:     DiffFileAdd,
 56						Index:    testrepoMarks[51].String(),
 57						OldIndex: testEmptyShaID,
 58						Sections: []*DiffSection{
 59							{
 60								Lines: []*DiffLine{
 61									{
 62										Type:    DiffLineSection,
 63										Content: "@@ -0,0 +1,11 @@",
 64									},
 65									{
 66										Type:      DiffLineAdd,
 67										Content:   "+This is a sample project students can use during Matthew's Git class.",
 68										LeftLine:  0,
 69										RightLine: 1,
 70									},
 71									{
 72										Type:      DiffLineAdd,
 73										Content:   "+",
 74										LeftLine:  0,
 75										RightLine: 2,
 76									},
 77									{
 78										Type:      DiffLineAdd,
 79										Content:   "+We can have a bit of fun with this repo, knowing that we can always reset it to a known good state.  We can apply labels, and branch, then add new code and merge it in to the master branch.",
 80										LeftLine:  0,
 81										RightLine: 3,
 82									},
 83									{
 84										Type:      DiffLineAdd,
 85										Content:   "+",
 86										LeftLine:  0,
 87										RightLine: 4,
 88									},
 89									{
 90										Type:      DiffLineAdd,
 91										Content:   "+As a quick reminder, this came from one of three locations in either SSH, Git, or HTTPS format:",
 92										LeftLine:  0,
 93										RightLine: 5,
 94									},
 95									{
 96										Type:      DiffLineAdd,
 97										Content:   "+",
 98										LeftLine:  0,
 99										RightLine: 6,
100									},
101									{
102										Type:      DiffLineAdd,
103										Content:   "+* git@github.com:matthewmccullough/hellogitworld.git",
104										LeftLine:  0,
105										RightLine: 7,
106									},
107									{
108										Type:      DiffLineAdd,
109										Content:   "+* git://github.com/matthewmccullough/hellogitworld.git",
110										LeftLine:  0,
111										RightLine: 8,
112									},
113									{
114										Type:      DiffLineAdd,
115										Content:   "+* https://matthewmccullough@github.com/matthewmccullough/hellogitworld.git",
116										LeftLine:  0,
117										RightLine: 9,
118									},
119									{
120										Type:      DiffLineAdd,
121										Content:   "+",
122										LeftLine:  0,
123										RightLine: 10,
124									},
125									{
126										Type:      DiffLineAdd,
127										Content:   "+We can, as an example effort, even modify this README and change it as if it were source code for the purposes of the class.",
128										LeftLine:  0,
129										RightLine: 11,
130									},
131								},
132								numAdditions: 11,
133							},
134						},
135						numAdditions: 11,
136						numDeletions: 0,
137						oldName:      "README.txt",
138						isBinary:     false,
139						isSubmodule:  false,
140						isIncomplete: false,
141						mode:         0100644,
142						oldMode:      0100644,
143					},
144					{
145						Name:     "resources/labels.properties",
146						Type:     DiffFileAdd,
147						Index:    testrepoMarks[52].String(),
148						OldIndex: testEmptyShaID,
149						Sections: []*DiffSection{
150							{
151								Lines: []*DiffLine{
152									{
153										Type:    DiffLineSection,
154										Content: "@@ -0,0 +1,4 @@",
155									},
156									{
157										Type:      DiffLineAdd,
158										Content:   "+app.title=Our App",
159										LeftLine:  0,
160										RightLine: 1,
161									},
162									{
163										Type:      DiffLineAdd,
164										Content:   "+app.welcome=Welcome to the application",
165										LeftLine:  0,
166										RightLine: 2,
167									},
168									{
169										Type:      DiffLineAdd,
170										Content:   "+app.goodbye=We hope you enjoyed using our application",
171										LeftLine:  0,
172										RightLine: 3,
173									},
174									{
175										Type:      DiffLineAdd,
176										Content:   "+cli.usage=This application doesn't use a command line interface",
177										LeftLine:  0,
178										RightLine: 4,
179									},
180								},
181								numAdditions: 4,
182							},
183						},
184						numAdditions: 4,
185						numDeletions: 0,
186						oldName:      "resources/labels.properties",
187						isBinary:     false,
188						isSubmodule:  false,
189						isIncomplete: false,
190						mode:         0100644,
191						oldMode:      0100644,
192					},
193					{
194						Name:     "src/Main.groovy",
195						Type:     DiffFileAdd,
196						Index:    testrepoMarks[53].String(),
197						OldIndex: testEmptyShaID,
198						Sections: []*DiffSection{
199							{
200								Lines: []*DiffLine{
201									{
202										Type:    DiffLineSection,
203										Content: "@@ -0,0 +1,6 @@",
204									},
205									{
206										Type:      DiffLineAdd,
207										Content:   "+def name = \"Matthew\"",
208										LeftLine:  0,
209										RightLine: 1,
210									},
211									{
212										Type:      DiffLineAdd,
213										Content:   "+",
214										LeftLine:  0,
215										RightLine: 2,
216									},
217									{
218										Type:      DiffLineAdd,
219										Content:   "+println \"Hello ${name}\"",
220										LeftLine:  0,
221										RightLine: 3,
222									},
223									{
224										Type:      DiffLineAdd,
225										Content:   "+",
226										LeftLine:  0,
227										RightLine: 4,
228									},
229									{
230										Type:      DiffLineAdd,
231										Content:   "+int programmingPoints = 10",
232										LeftLine:  0,
233										RightLine: 5,
234									},
235									{
236										Type:      DiffLineAdd,
237										Content:   "+println \"${name} has at least ${programmingPoints} programming points.\"",
238										LeftLine:  0,
239										RightLine: 6,
240									},
241								},
242								numAdditions: 6,
243							},
244						},
245						numAdditions: 6,
246						numDeletions: 0,
247						oldName:      "src/Main.groovy",
248						isBinary:     false,
249						isSubmodule:  false,
250						isIncomplete: false,
251						mode:         0100644,
252						oldMode:      0100644,
253					},
254				},
255				totalAdditions: 21,
256				totalDeletions: 0,
257				isIncomplete:   false,
258			},
259		},
260		{
261			rev: testrepoMarks[27].String(),
262			opt: DiffOptions{
263				Base: testrepoMarks[26].String(),
264			},
265			expDiff: &Diff{
266				Files: []*DiffFile{
267					{
268						Name:         "fix.txt",
269						Type:         DiffFileDelete,
270						Index:        testEmptyShaID,
271						OldIndex:     testrepoMarks[54].String(),
272						Sections:     nil,
273						numAdditions: 0,
274						numDeletions: 0,
275						oldName:      "fix.txt",
276						isBinary:     false,
277						isSubmodule:  false,
278						isIncomplete: false,
279						mode:         0100644,
280						oldMode:      0100644,
281					},
282				},
283				totalAdditions: 0,
284				totalDeletions: 0,
285				isIncomplete:   false,
286			},
287		},
288	}
289	for _, test := range tests {
290		t.Run("", func(t *testing.T) {
291			diff, err := testrepo.Diff(ctx, test.rev, test.maxFiles, test.maxFileLines, test.maxLineChars, test.opt)
292			if err != nil {
293				t.Fatal(err)
294			}
295
296			assert.Equal(t, test.expDiff, diff)
297		})
298	}
299}
300
301func TestRepository_RawDiff(t *testing.T) {
302	ctx := context.Background()
303
304	t.Run("invalid revision", func(t *testing.T) {
305		err := testrepo.RawDiff(ctx, "bad_revision", "bad_diff_type", nil)
306		assert.Equal(t, ErrRevisionNotExist, err)
307	})
308
309	t.Run("invalid diffType", func(t *testing.T) {
310		err := testrepo.RawDiff(ctx, testrepoMarks[27].String(), "bad_diff_type", nil)
311		assert.Equal(t, errors.New("invalid diffType: bad_diff_type"), err)
312	})
313
314	tests := []struct {
315		rev       string
316		diffType  RawDiffFormat
317		opt       RawDiffOptions
318		expOutput string
319	}{
320		{
321			rev:      testrepoMarks[27].String(),
322			diffType: RawDiffNormal,
323			expOutput: fmt.Sprintf(`diff --git a/fix.txt b/fix.txt
324deleted file mode 100644
325index %s..%s
326`, testrepoMarks[54].String(), testEmptyShaID),
327		},
328		{
329			rev:      testrepoMarks[27].String(),
330			diffType: RawDiffPatch,
331			expOutput: fmt.Sprintf(`Date: Sun, 9 Feb 2020 17:22:24 +0800
332Subject: [PATCH] Delete fix.txt
333
334---
335 fix.txt | 0
336 1 file changed, 0 insertions(+), 0 deletions(-)
337 delete mode 100644 fix.txt
338
339diff --git a/fix.txt b/fix.txt
340deleted file mode 100644
341index %s..%s
342`, testrepoMarks[54].String(), testEmptyShaID),
343		},
344		{
345			rev:      testrepoMarks[1].String(),
346			diffType: RawDiffNormal,
347			expOutput: fmt.Sprintf(`commit %[1]s
348Author: Matthew McCullough <matthewm@ambientideas.com>
349Date:   Mon Nov 24 21:22:01 2008 -0700
350
351    Addition of the README and basic Groovy source samples.
352    
353    - Addition of the README.txt file explaining what this repository is all about.
354    - Addition of Groovy sample source.
355    - Addition of sample resource Properties file.
356
357diff --git a/README.txt b/README.txt
358new file mode 100644
359index %[2]s..%[3]s
360--- /dev/null
361+++ b/README.txt
362@@ -0,0 +1,11 @@
363+This is a sample project students can use during Matthew's Git class.
364+
365+We can have a bit of fun with this repo, knowing that we can always reset it to a known good state.  We can apply labels, and branch, then add new code and merge it in to the master branch.
366+
367+As a quick reminder, this came from one of three locations in either SSH, Git, or HTTPS format:
368+
369+* git@github.com:matthewmccullough/hellogitworld.git
370+* git://github.com/matthewmccullough/hellogitworld.git
371+* https://matthewmccullough@github.com/matthewmccullough/hellogitworld.git
372+
373+We can, as an example effort, even modify this README and change it as if it were source code for the purposes of the class.
374\ No newline at end of file
375diff --git a/resources/labels.properties b/resources/labels.properties
376new file mode 100644
377index %[2]s..%[4]s
378--- /dev/null
379+++ b/resources/labels.properties
380@@ -0,0 +1,4 @@
381+app.title=Our App
382+app.welcome=Welcome to the application
383+app.goodbye=We hope you enjoyed using our application
384+cli.usage=This application doesn't use a command line interface
385diff --git a/src/Main.groovy b/src/Main.groovy
386new file mode 100644
387index %[2]s..%[5]s
388--- /dev/null
389+++ b/src/Main.groovy
390@@ -0,0 +1,6 @@
391+def name = "Matthew"
392+
393+println "Hello ${name}"
394+
395+int programmingPoints = 10
396+println "${name} has at least ${programmingPoints} programming points."
397\ No newline at end of file
398`, testrepoMarks[1].String(), testEmptyShaID, testrepoMarks[51].String(), testrepoMarks[52].String(), testrepoMarks[53].String()),
399		},
400		{
401			rev:      testrepoMarks[1].String(),
402			diffType: RawDiffPatch,
403			expOutput: fmt.Sprintf(`Date: Mon, 24 Nov 2008 21:22:01 -0700
404Subject: [PATCH] Addition of the README and basic Groovy source samples.
405
406- Addition of the README.txt file explaining what this repository is all about.
407- Addition of Groovy sample source.
408- Addition of sample resource Properties file.
409---
410 README.txt                  | 11 +++++++++++
411 resources/labels.properties |  4 ++++
412 src/Main.groovy             |  6 ++++++
413 3 files changed, 21 insertions(+)
414 create mode 100644 README.txt
415 create mode 100644 resources/labels.properties
416 create mode 100644 src/Main.groovy
417
418diff --git a/README.txt b/README.txt
419new file mode 100644
420index %[2]s..%[3]s
421--- /dev/null
422+++ b/README.txt
423@@ -0,0 +1,11 @@
424+This is a sample project students can use during Matthew's Git class.
425+
426+We can have a bit of fun with this repo, knowing that we can always reset it to a known good state.  We can apply labels, and branch, then add new code and merge it in to the master branch.
427+
428+As a quick reminder, this came from one of three locations in either SSH, Git, or HTTPS format:
429+
430+* git@github.com:matthewmccullough/hellogitworld.git
431+* git://github.com/matthewmccullough/hellogitworld.git
432+* https://matthewmccullough@github.com/matthewmccullough/hellogitworld.git
433+
434+We can, as an example effort, even modify this README and change it as if it were source code for the purposes of the class.
435\ No newline at end of file
436diff --git a/resources/labels.properties b/resources/labels.properties
437new file mode 100644
438index %[2]s..%[4]s
439--- /dev/null
440+++ b/resources/labels.properties
441@@ -0,0 +1,4 @@
442+app.title=Our App
443+app.welcome=Welcome to the application
444+app.goodbye=We hope you enjoyed using our application
445+cli.usage=This application doesn't use a command line interface
446diff --git a/src/Main.groovy b/src/Main.groovy
447new file mode 100644
448index %[2]s..%[5]s
449--- /dev/null
450+++ b/src/Main.groovy
451@@ -0,0 +1,6 @@
452+def name = "Matthew"
453+
454+println "Hello ${name}"
455+
456+int programmingPoints = 10
457+println "${name} has at least ${programmingPoints} programming points."
458\ No newline at end of file
459`, testrepoMarks[1].String(), testEmptyShaID, testrepoMarks[51].String(), testrepoMarks[52].String(), testrepoMarks[53].String()),
460		},
461	}
462	for _, test := range tests {
463		t.Run("", func(t *testing.T) {
464			buf := new(bytes.Buffer)
465			err := testrepo.RawDiff(ctx, test.rev, test.diffType, buf, test.opt)
466			if err != nil {
467				t.Fatal(err)
468			}
469			output := buf.String()
470
471			// Only check the content after "Date:" line, which is deterministic.
472			i := strings.Index(output, "Date:")
473			if i > 0 && test.diffType == RawDiffPatch {
474				output = output[i:]
475			}
476
477			assert.Equal(t, test.expOutput, output)
478		})
479	}
480}
481
482func TestRepository_DiffBinary(t *testing.T) {
483	ctx := context.Background()
484	tests := []struct {
485		base      string
486		head      string
487		opt       DiffBinaryOptions
488		expOutput string
489	}{
490		{
491			base: testrepoMarks[28].String(),
492			head: testrepoMarks[29].String(),
493			expOutput: fmt.Sprintf(`diff --git a/.gitmodules b/.gitmodules
494new file mode 100644
495index %[1]s..%[2]s
496--- /dev/null
497+++ b/.gitmodules
498@@ -0,0 +1,3 @@
499+[submodule "gogs/docs-api"]
500+	path = gogs/docs-api
501+	url = https://github.com/gogs/docs-api.git
502diff --git a/gogs/docs-api b/gogs/docs-api
503new file mode 160000
504index %[1]s..%[3]s
505--- /dev/null
506+++ b/gogs/docs-api
507@@ -0,0 +1 @@
508+Subproject commit %[3]s
509`, testEmptyShaID, testrepoMarks[37].String(), submoduleSHA.String()),
510		},
511	}
512	for _, test := range tests {
513		t.Run("", func(t *testing.T) {
514			p, err := testrepo.DiffBinary(ctx, test.base, test.head, test.opt)
515			if err != nil {
516				t.Fatal(err)
517			}
518
519			assert.Equal(t, test.expOutput, string(p))
520		})
521	}
522}