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