1package patchutil
2
3import (
4 "reflect"
5 "testing"
6)
7
8func TestSplitPatches(t *testing.T) {
9 tests := []struct {
10 name string
11 input string
12 expected []string
13 }{
14 {
15 name: "Empty input",
16 input: "",
17 expected: []string{},
18 },
19 {
20 name: "No valid patches",
21 input: "This is not a patch\nJust some random text",
22 expected: []string{},
23 },
24 {
25 name: "Single patch",
26 input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001
27From: Author <author@example.com>
28Date: Wed, 16 Apr 2025 11:01:00 +0300
29Subject: [PATCH] Example patch
30
31diff --git a/file.txt b/file.txt
32index 123456..789012 100644
33--- a/file.txt
34+++ b/file.txt
35@@ -1 +1 @@
36-old content
37+new content
38--
392.48.1`,
40 expected: []string{
41 `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001
42From: Author <author@example.com>
43Date: Wed, 16 Apr 2025 11:01:00 +0300
44Subject: [PATCH] Example patch
45
46diff --git a/file.txt b/file.txt
47index 123456..789012 100644
48--- a/file.txt
49+++ b/file.txt
50@@ -1 +1 @@
51-old content
52+new content
53--
542.48.1`,
55 },
56 },
57 {
58 name: "Two patches",
59 input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001
60From: Author <author@example.com>
61Date: Wed, 16 Apr 2025 11:01:00 +0300
62Subject: [PATCH 1/2] First patch
63
64diff --git a/file1.txt b/file1.txt
65index 123456..789012 100644
66--- a/file1.txt
67+++ b/file1.txt
68@@ -1 +1 @@
69-old content
70+new content
71--
722.48.1
73From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001
74From: Author <author@example.com>
75Date: Wed, 16 Apr 2025 11:03:11 +0300
76Subject: [PATCH 2/2] Second patch
77
78diff --git a/file2.txt b/file2.txt
79index abcdef..ghijkl 100644
80--- a/file2.txt
81+++ b/file2.txt
82@@ -1 +1 @@
83-foo bar
84+baz qux
85--
862.48.1`,
87 expected: []string{
88 `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001
89From: Author <author@example.com>
90Date: Wed, 16 Apr 2025 11:01:00 +0300
91Subject: [PATCH 1/2] First patch
92
93diff --git a/file1.txt b/file1.txt
94index 123456..789012 100644
95--- a/file1.txt
96+++ b/file1.txt
97@@ -1 +1 @@
98-old content
99+new content
100--
1012.48.1`,
102 `From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001
103From: Author <author@example.com>
104Date: Wed, 16 Apr 2025 11:03:11 +0300
105Subject: [PATCH 2/2] Second patch
106
107diff --git a/file2.txt b/file2.txt
108index abcdef..ghijkl 100644
109--- a/file2.txt
110+++ b/file2.txt
111@@ -1 +1 @@
112-foo bar
113+baz qux
114--
1152.48.1`,
116 },
117 },
118 {
119 name: "Patches with additional text between them",
120 input: `Some text before the patches
121
122From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001
123From: Author <author@example.com>
124Subject: [PATCH] First patch
125
126diff content here
127--
1282.48.1
129
130Some text between patches
131
132From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001
133From: Author <author@example.com>
134Subject: [PATCH] Second patch
135
136more diff content
137--
1382.48.1
139
140Text after patches`,
141 expected: []string{
142 `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001
143From: Author <author@example.com>
144Subject: [PATCH] First patch
145
146diff content here
147--
1482.48.1
149
150Some text between patches`,
151 `From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001
152From: Author <author@example.com>
153Subject: [PATCH] Second patch
154
155more diff content
156--
1572.48.1
158
159Text after patches`,
160 },
161 },
162 {
163 name: "Patches with whitespace padding",
164 input: `
165
166From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001
167From: Author <author@example.com>
168Subject: Patch
169
170content
171--
1722.48.1
173
174
175From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001
176From: Author <author@example.com>
177Subject: Another patch
178
179content
180--
1812.48.1
182 `,
183 expected: []string{
184 `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001
185From: Author <author@example.com>
186Subject: Patch
187
188content
189--
1902.48.1`,
191 `From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001
192From: Author <author@example.com>
193Subject: Another patch
194
195content
196--
1972.48.1`,
198 },
199 },
200 }
201
202 for _, tt := range tests {
203 t.Run(tt.name, func(t *testing.T) {
204 result := splitFormatPatch(tt.input)
205 if !reflect.DeepEqual(result, tt.expected) {
206 t.Errorf("splitPatches() = %v, want %v", result, tt.expected)
207 }
208 })
209 }
210}