repos / git-pr

a self-hosted git collaboration server
git clone https://github.com/picosh/git-pr.git

Eric Bower  ·  2024-07-23

range_diff_test.go

  1package git
  2
  3import (
  4	"fmt"
  5	"strings"
  6	"testing"
  7
  8	"github.com/picosh/git-pr/fixtures"
  9)
 10
 11func bail(err error) {
 12	if err != nil {
 13		panic(bail)
 14	}
 15}
 16
 17func cmp(afile, bfile string) string {
 18	a, err := fixtures.Fixtures.Open(afile)
 19	bail(err)
 20	b, err := fixtures.Fixtures.Open(bfile)
 21	bail(err)
 22	aPatches, err := ParsePatchset(a)
 23	bail(err)
 24	bPatches, err := ParsePatchset(b)
 25	bail(err)
 26	actual := RangeDiff(aPatches, bPatches)
 27	return RangeDiffToStr(actual)
 28}
 29
 30func fail(expected, actual string) string {
 31	return fmt.Sprintf("expected:[\n%s] actual:[\n%s]", expected, actual)
 32}
 33
 34// https://git.kernel.org/tree/t/t3206-range-diff.sh?id=d19b6cd2dd72dc811f19df4b32c7ed223256c3ee
 35
 36// simple A..B A..C (unmodified)
 37/*
 38	1:  $(test_oid t1) = 1:  $(test_oid u1) s/5/A/
 39	2:  $(test_oid t2) = 2:  $(test_oid u2) s/4/A/
 40	3:  $(test_oid t3) = 3:  $(test_oid u3) s/11/B/
 41	4:  $(test_oid t4) = 4:  $(test_oid u4) s/12/B/
 42*/
 43func TestRangeDiffUnmodified(t *testing.T) {
 44	actual := cmp("a_b.patch", "a_c.patch")
 45	expected := "1:  33c682a = 1:  1668484 chore: add torch and create random tensor\n"
 46	if expected != actual {
 47		t.Fatal(fail(expected, actual))
 48	}
 49}
 50
 51// trivial reordering
 52/*
 53	1:  $(test_oid t1) = 1:  $(test_oid r1) s/5/A/
 54	3:  $(test_oid t3) = 2:  $(test_oid r2) s/11/B/
 55	4:  $(test_oid t4) = 3:  $(test_oid r3) s/12/B/
 56	2:  $(test_oid t2) = 4:  $(test_oid r4) s/4/A/
 57*/
 58func TestRangeDiffTrivialReordering(t *testing.T) {
 59	actual := cmp("a_b_reorder.patch", "a_c_reorder.patch")
 60	expected := `2:  22dde12 = 1:  7dbb94c docs: readme
 611:  33c682a = 2:  ad17587 chore: add torch and create random tensor
 62`
 63	if expected != actual {
 64		t.Fatal(fail(expected, actual))
 65	}
 66}
 67
 68// removed commit
 69/*
 70	1:  $(test_oid t1) = 1:  $(test_oid d1) s/5/A/
 71	2:  $(test_oid t2) < -:  $(test_oid __) s/4/A/
 72	3:  $(test_oid t3) = 2:  $(test_oid d2) s/11/B/
 73	4:  $(test_oid t4) = 3:  $(test_oid d3) s/12/B/
 74*/
 75func TestRangeDiffRemovedCommit(t *testing.T) {
 76	actual := cmp("a_b_reorder.patch", "a_c_rm_commit.patch")
 77	expected := `1:  33c682a < -:  ------- chore: add torch and create random tensor
 782:  22dde12 = 1:  7dbb94c docs: readme
 79`
 80	if expected != actual {
 81		t.Fatal(fail(expected, actual))
 82	}
 83}
 84
 85// added commit
 86/*
 87	1:  $(test_oid t1) = 1:  $(test_oid a1) s/5/A/
 88	2:  $(test_oid t2) = 2:  $(test_oid a2) s/4/A/
 89	-:  $(test_oid __) > 3:  $(test_oid a3) s/6/A/
 90	3:  $(test_oid t3) = 4:  $(test_oid a4) s/11/B/
 91	4:  $(test_oid t4) = 5:  $(test_oid a5) s/12/B/
 92*/
 93func TestRangeDiffAddedCommit(t *testing.T) {
 94	actual := cmp("a_b_reorder.patch", "a_c_added_commit.patch")
 95	expected := `1:  33c682a = 1:  33c682a chore: add torch and create random tensor
 962:  22dde12 = 2:  22dde12 docs: readme
 97-:  ------- > 3:  b248060 chore: make tensor 6x6
 98`
 99	if expected != actual {
100		t.Fatal(fail(expected, actual))
101	}
102}
103
104// changed commit
105/*
106	1:  $(test_oid t1) = 1:  $(test_oid c1) s/5/A/
107	2:  $(test_oid t2) = 2:  $(test_oid c2) s/4/A/
108	3:  $(test_oid t3) ! 3:  $(test_oid c3) s/11/B/
109	    @@ file: A
110	      9
111	      10
112	     -11
113	    -+B
114	    ++BB
115	      12
116	      13
117	      14
118	4:  $(test_oid t4) ! 4:  $(test_oid c4) s/12/B/
119	    @@ file
120	     @@ file: A
121	      9
122	      10
123	    - B
124	    + BB
125	     -12
126	     +B
127	      13
128*/
129func TestRangeDiffChangedCommit(t *testing.T) {
130	actual := cmp("a_b_reorder.patch", "a_c_changed_commit.patch")
131	// os.WriteFile("fixtures/expected_commit_changed.txt", []byte(actual), 0644)
132	fp, err := fixtures.Fixtures.ReadFile("expected_commit_changed.txt")
133	if err != nil {
134		t.Fatal("file not found")
135	}
136	expected := string(fp)
137	if strings.TrimSpace(expected) != strings.TrimSpace(actual) {
138		t.Fatal(fail(expected, actual))
139	}
140}
141
142// renamed file
143/*
144	1:  $(test_oid t1) = 1:  $(test_oid n1) s/5/A/
145	2:  $(test_oid t2) ! 2:  $(test_oid n2) s/4/A/
146	    @@ Metadata
147	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
148	    Z
149	    Z ## Commit message ##
150	    -    s/4/A/
151	    +    s/4/A/ + rename file
152	    Z
153	    - ## file ##
154	    + ## file => renamed-file ##
155	    Z@@
156	    Z 1
157	    Z 2
158	3:  $(test_oid t3) ! 3:  $(test_oid n3) s/11/B/
159	    @@ Metadata
160	    Z ## Commit message ##
161	    Z    s/11/B/
162	    Z
163	    - ## file ##
164	    -@@ file: A
165	    + ## renamed-file ##
166	    +@@ renamed-file: A
167	    Z 8
168	    Z 9
169	    Z 10
170	4:  $(test_oid t4) ! 4:  $(test_oid n4) s/12/B/
171	    @@ Metadata
172	    Z ## Commit message ##
173	    Z    s/12/B/
174	    Z
175	    - ## file ##
176	    -@@ file: A
177	    + ## renamed-file ##
178	    +@@ renamed-file: A
179	    Z 9
180	    Z 10
181	    Z B
182*/
183// func TestRangeDiffRenamedFile(t *testing.T) {}
184
185// file with mode only change
186/*
187	1:  $(test_oid t2) ! 1:  $(test_oid o1) s/4/A/
188	    @@ Metadata
189	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
190	    Z
191	    Z ## Commit message ##
192	    -    s/4/A/
193	    +    s/4/A/ + add other-file
194	    Z
195	    Z ## file ##
196	    Z@@
197	    @@ file
198	    Z A
199	    Z 6
200	    Z 7
201	    +
202	    + ## other-file (new) ##
203	2:  $(test_oid t3) ! 2:  $(test_oid o2) s/11/B/
204	    @@ Metadata
205	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
206	    Z
207	    Z ## Commit message ##
208	    -    s/11/B/
209	    +    s/11/B/ + mode change other-file
210	    Z
211	    Z ## file ##
212	    Z@@ file: A
213	    @@ file: A
214	    Z 12
215	    Z 13
216	    Z 14
217	    +
218	    + ## other-file (mode change 100644 => 100755) ##
219	3:  $(test_oid t4) = 3:  $(test_oid o3) s/12/B/
220*/
221// func TestRangeDiffFileWithModeOnlyChange(t *testing.T) {}
222
223// file added and later removed
224/*
225	1:  $(test_oid t1) = 1:  $(test_oid s1) s/5/A/
226	2:  $(test_oid t2) ! 2:  $(test_oid s2) s/4/A/
227	    @@ Metadata
228	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
229	    Z
230	    Z ## Commit message ##
231	    -    s/4/A/
232	    +    s/4/A/ + new-file
233	    Z
234	    Z ## file ##
235	    Z@@
236	    @@ file
237	    Z A
238	    Z 6
239	    Z 7
240	    +
241	    + ## new-file (new) ##
242	3:  $(test_oid t3) ! 3:  $(test_oid s3) s/11/B/
243	    @@ Metadata
244	    ZAuthor: Thomas Rast <trast@inf.ethz.ch>
245	    Z
246	    Z ## Commit message ##
247	    -    s/11/B/
248	    +    s/11/B/ + remove file
249	    Z
250	    Z ## file ##
251	    Z@@ file: A
252	    @@ file: A
253	    Z 12
254	    Z 13
255	    Z 14
256	    +
257	    + ## new-file (deleted) ##
258	4:  $(test_oid t4) = 4:  $(test_oid s4) s/12/B/
259*/
260// func TestRangeDiffFileAddedThenRemoved(t *testing.T) {}
261
262// changed message
263/*
264	1:  $(test_oid t1) = 1:  $(test_oid m1) s/5/A/
265	2:  $(test_oid t2) ! 2:  $(test_oid m2) s/4/A/
266	    @@ Metadata
267	    Z ## Commit message ##
268	    Z    s/4/A/
269	    Z
270	    +    Also a silly comment here!
271	    +
272	    Z ## file ##
273	    Z@@
274	    Z 1
275	3:  $(test_oid t3) = 3:  $(test_oid m3) s/11/B/
276	4:  $(test_oid t4) = 4:  $(test_oid m4) s/12/B/
277*/
278// func TestRangeDiffChangedMessage(t *testing.T) {}