-
Notifications
You must be signed in to change notification settings - Fork 3
/
range_diff_test.go
278 lines (263 loc) · 6.4 KB
/
range_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
package git
import (
"fmt"
"strings"
"testing"
"github.com/picosh/git-pr/fixtures"
)
func bail(err error) {
if err != nil {
panic(bail)
}
}
func cmp(afile, bfile string) string {
a, err := fixtures.Fixtures.Open(afile)
bail(err)
b, err := fixtures.Fixtures.Open(bfile)
bail(err)
aPatches, err := ParsePatchset(a)
bail(err)
bPatches, err := ParsePatchset(b)
bail(err)
actual := RangeDiff(aPatches, bPatches)
return RangeDiffToStr(actual)
}
func fail(expected, actual string) string {
return fmt.Sprintf("expected:[\n%s] actual:[\n%s]", expected, actual)
}
// https://git.kernel.org/tree/t/t3206-range-diff.sh?id=d19b6cd2dd72dc811f19df4b32c7ed223256c3ee
// simple A..B A..C (unmodified)
/*
1: $(test_oid t1) = 1: $(test_oid u1) s/5/A/
2: $(test_oid t2) = 2: $(test_oid u2) s/4/A/
3: $(test_oid t3) = 3: $(test_oid u3) s/11/B/
4: $(test_oid t4) = 4: $(test_oid u4) s/12/B/
*/
func TestRangeDiffUnmodified(t *testing.T) {
actual := cmp("a_b.patch", "a_c.patch")
expected := "1: 33c682a = 1: 1668484 chore: add torch and create random tensor\n"
if expected != actual {
t.Fatal(fail(expected, actual))
}
}
// trivial reordering
/*
1: $(test_oid t1) = 1: $(test_oid r1) s/5/A/
3: $(test_oid t3) = 2: $(test_oid r2) s/11/B/
4: $(test_oid t4) = 3: $(test_oid r3) s/12/B/
2: $(test_oid t2) = 4: $(test_oid r4) s/4/A/
*/
func TestRangeDiffTrivialReordering(t *testing.T) {
actual := cmp("a_b_reorder.patch", "a_c_reorder.patch")
expected := `2: 22dde12 = 1: 7dbb94c docs: readme
1: 33c682a = 2: ad17587 chore: add torch and create random tensor
`
if expected != actual {
t.Fatal(fail(expected, actual))
}
}
// removed commit
/*
1: $(test_oid t1) = 1: $(test_oid d1) s/5/A/
2: $(test_oid t2) < -: $(test_oid __) s/4/A/
3: $(test_oid t3) = 2: $(test_oid d2) s/11/B/
4: $(test_oid t4) = 3: $(test_oid d3) s/12/B/
*/
func TestRangeDiffRemovedCommit(t *testing.T) {
actual := cmp("a_b_reorder.patch", "a_c_rm_commit.patch")
expected := `1: 33c682a < -: ------- chore: add torch and create random tensor
2: 22dde12 = 1: 7dbb94c docs: readme
`
if expected != actual {
t.Fatal(fail(expected, actual))
}
}
// added commit
/*
1: $(test_oid t1) = 1: $(test_oid a1) s/5/A/
2: $(test_oid t2) = 2: $(test_oid a2) s/4/A/
-: $(test_oid __) > 3: $(test_oid a3) s/6/A/
3: $(test_oid t3) = 4: $(test_oid a4) s/11/B/
4: $(test_oid t4) = 5: $(test_oid a5) s/12/B/
*/
func TestRangeDiffAddedCommit(t *testing.T) {
actual := cmp("a_b_reorder.patch", "a_c_added_commit.patch")
expected := `1: 33c682a = 1: 33c682a chore: add torch and create random tensor
2: 22dde12 = 2: 22dde12 docs: readme
-: ------- > 3: b248060 chore: make tensor 6x6
`
if expected != actual {
t.Fatal(fail(expected, actual))
}
}
// changed commit
/*
1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
3: $(test_oid t3) ! 3: $(test_oid c3) s/11/B/
@@ file: A
9
10
-11
-+B
++BB
12
13
14
4: $(test_oid t4) ! 4: $(test_oid c4) s/12/B/
@@ file
@@ file: A
9
10
- B
+ BB
-12
+B
13
*/
func TestRangeDiffChangedCommit(t *testing.T) {
actual := cmp("a_b_reorder.patch", "a_c_changed_commit.patch")
// os.WriteFile("fixtures/expected_commit_changed.txt", []byte(actual), 0644)
fp, err := fixtures.Fixtures.ReadFile("expected_commit_changed.txt")
if err != nil {
t.Fatal("file not found")
}
expected := string(fp)
if strings.TrimSpace(expected) != strings.TrimSpace(actual) {
t.Fatal(fail(expected, actual))
}
}
// renamed file
/*
1: $(test_oid t1) = 1: $(test_oid n1) s/5/A/
2: $(test_oid t2) ! 2: $(test_oid n2) s/4/A/
@@ Metadata
ZAuthor: Thomas Rast <trast@inf.ethz.ch>
Z
Z ## Commit message ##
- s/4/A/
+ s/4/A/ + rename file
Z
- ## file ##
+ ## file => renamed-file ##
Z@@
Z 1
Z 2
3: $(test_oid t3) ! 3: $(test_oid n3) s/11/B/
@@ Metadata
Z ## Commit message ##
Z s/11/B/
Z
- ## file ##
-@@ file: A
+ ## renamed-file ##
+@@ renamed-file: A
Z 8
Z 9
Z 10
4: $(test_oid t4) ! 4: $(test_oid n4) s/12/B/
@@ Metadata
Z ## Commit message ##
Z s/12/B/
Z
- ## file ##
-@@ file: A
+ ## renamed-file ##
+@@ renamed-file: A
Z 9
Z 10
Z B
*/
// func TestRangeDiffRenamedFile(t *testing.T) {}
// file with mode only change
/*
1: $(test_oid t2) ! 1: $(test_oid o1) s/4/A/
@@ Metadata
ZAuthor: Thomas Rast <trast@inf.ethz.ch>
Z
Z ## Commit message ##
- s/4/A/
+ s/4/A/ + add other-file
Z
Z ## file ##
Z@@
@@ file
Z A
Z 6
Z 7
+
+ ## other-file (new) ##
2: $(test_oid t3) ! 2: $(test_oid o2) s/11/B/
@@ Metadata
ZAuthor: Thomas Rast <trast@inf.ethz.ch>
Z
Z ## Commit message ##
- s/11/B/
+ s/11/B/ + mode change other-file
Z
Z ## file ##
Z@@ file: A
@@ file: A
Z 12
Z 13
Z 14
+
+ ## other-file (mode change 100644 => 100755) ##
3: $(test_oid t4) = 3: $(test_oid o3) s/12/B/
*/
// func TestRangeDiffFileWithModeOnlyChange(t *testing.T) {}
// file added and later removed
/*
1: $(test_oid t1) = 1: $(test_oid s1) s/5/A/
2: $(test_oid t2) ! 2: $(test_oid s2) s/4/A/
@@ Metadata
ZAuthor: Thomas Rast <trast@inf.ethz.ch>
Z
Z ## Commit message ##
- s/4/A/
+ s/4/A/ + new-file
Z
Z ## file ##
Z@@
@@ file
Z A
Z 6
Z 7
+
+ ## new-file (new) ##
3: $(test_oid t3) ! 3: $(test_oid s3) s/11/B/
@@ Metadata
ZAuthor: Thomas Rast <trast@inf.ethz.ch>
Z
Z ## Commit message ##
- s/11/B/
+ s/11/B/ + remove file
Z
Z ## file ##
Z@@ file: A
@@ file: A
Z 12
Z 13
Z 14
+
+ ## new-file (deleted) ##
4: $(test_oid t4) = 4: $(test_oid s4) s/12/B/
*/
// func TestRangeDiffFileAddedThenRemoved(t *testing.T) {}
// changed message
/*
1: $(test_oid t1) = 1: $(test_oid m1) s/5/A/
2: $(test_oid t2) ! 2: $(test_oid m2) s/4/A/
@@ Metadata
Z ## Commit message ##
Z s/4/A/
Z
+ Also a silly comment here!
+
Z ## file ##
Z@@
Z 1
3: $(test_oid t3) = 3: $(test_oid m3) s/11/B/
4: $(test_oid t4) = 4: $(test_oid m4) s/12/B/
*/
// func TestRangeDiffChangedMessage(t *testing.T) {}