1package git23import (4 "bytes"5 "context"6 "errors"7 "fmt"8 "strings"9 "testing"1011 "github.com/stretchr/testify/assert"12)1314func TestRepository_Diff(t *testing.T) {15 ctx := context.Background()16 tests := []struct {17 rev string18 maxFiles int19 maxFileLines int20 maxLineChars int21 opt DiffOptions22 expDiff *Diff23 }{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 }295296 assert.Equal(t, test.expDiff, diff)297 })298 }299}300301func TestRepository_RawDiff(t *testing.T) {302 ctx := context.Background()303304 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 })308309 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 })313314 tests := []struct {315 rev string316 diffType RawDiffFormat317 opt RawDiffOptions318 expOutput string319 }{320 {321 rev: testrepoMarks[27].String(),322 diffType: RawDiffNormal,323 expOutput: fmt.Sprintf(`diff --git a/fix.txt b/fix.txt324deleted file mode 100644325index %s..%s326`, 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 +0800332Subject: [PATCH] Delete fix.txt333334---335 fix.txt | 0336 1 file changed, 0 insertions(+), 0 deletions(-)337 delete mode 100644 fix.txt338339diff --git a/fix.txt b/fix.txt340deleted file mode 100644341index %s..%s342`, testrepoMarks[54].String(), testEmptyShaID),343 },344 {345 rev: testrepoMarks[1].String(),346 diffType: RawDiffNormal,347 expOutput: fmt.Sprintf(`commit %[1]s348Author: Matthew McCullough <matthewm@ambientideas.com>349Date: Mon Nov 24 21:22:01 2008 -0700350351 Addition of the README and basic Groovy source samples.352353 - 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.356357diff --git a/README.txt b/README.txt358new file mode 100644359index %[2]s..%[3]s360--- /dev/null361+++ b/README.txt362@@ -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.git370+* git://github.com/matthewmccullough/hellogitworld.git371+* https://matthewmccullough@github.com/matthewmccullough/hellogitworld.git372+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 file375diff --git a/resources/labels.properties b/resources/labels.properties376new file mode 100644377index %[2]s..%[4]s378--- /dev/null379+++ b/resources/labels.properties380@@ -0,0 +1,4 @@381+app.title=Our App382+app.welcome=Welcome to the application383+app.goodbye=We hope you enjoyed using our application384+cli.usage=This application doesn't use a command line interface385diff --git a/src/Main.groovy b/src/Main.groovy386new file mode 100644387index %[2]s..%[5]s388--- /dev/null389+++ b/src/Main.groovy390@@ -0,0 +1,6 @@391+def name = "Matthew"392+393+println "Hello ${name}"394+395+int programmingPoints = 10396+println "${name} has at least ${programmingPoints} programming points."397\ No newline at end of file398`, 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 -0700404Subject: [PATCH] Addition of the README and basic Groovy source samples.405406- 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.txt415 create mode 100644 resources/labels.properties416 create mode 100644 src/Main.groovy417418diff --git a/README.txt b/README.txt419new file mode 100644420index %[2]s..%[3]s421--- /dev/null422+++ b/README.txt423@@ -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.git431+* git://github.com/matthewmccullough/hellogitworld.git432+* https://matthewmccullough@github.com/matthewmccullough/hellogitworld.git433+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 file436diff --git a/resources/labels.properties b/resources/labels.properties437new file mode 100644438index %[2]s..%[4]s439--- /dev/null440+++ b/resources/labels.properties441@@ -0,0 +1,4 @@442+app.title=Our App443+app.welcome=Welcome to the application444+app.goodbye=We hope you enjoyed using our application445+cli.usage=This application doesn't use a command line interface446diff --git a/src/Main.groovy b/src/Main.groovy447new file mode 100644448index %[2]s..%[5]s449--- /dev/null450+++ b/src/Main.groovy451@@ -0,0 +1,6 @@452+def name = "Matthew"453+454+println "Hello ${name}"455+456+int programmingPoints = 10457+println "${name} has at least ${programmingPoints} programming points."458\ No newline at end of file459`, 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()470471 // 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 }476477 assert.Equal(t, test.expOutput, output)478 })479 }480}481482func TestRepository_DiffBinary(t *testing.T) {483 ctx := context.Background()484 tests := []struct {485 base string486 head string487 opt DiffBinaryOptions488 expOutput string489 }{490 {491 base: testrepoMarks[28].String(),492 head: testrepoMarks[29].String(),493 expOutput: fmt.Sprintf(`diff --git a/.gitmodules b/.gitmodules494new file mode 100644495index %[1]s..%[2]s496--- /dev/null497+++ b/.gitmodules498@@ -0,0 +1,3 @@499+[submodule "gogs/docs-api"]500+ path = gogs/docs-api501+ url = https://github.com/gogs/docs-api.git502diff --git a/gogs/docs-api b/gogs/docs-api503new file mode 160000504index %[1]s..%[3]s505--- /dev/null506+++ b/gogs/docs-api507@@ -0,0 +1 @@508+Subproject commit %[3]s509`, 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 }518519 assert.Equal(t, test.expOutput, string(p))520 })521 }522}