summaryrefslogtreecommitdiff
path: root/t/unit-tests/u-trailer.c
blob: 3d60ea1603dbdadfaba0a437ff136d8b9140713f (plain)
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
#define DISABLE_SIGN_COMPARE_WARNINGS

#include "unit-test.h"
#include "trailer.h"

struct contents {
	const char *raw;
	const char *key;
	const char *val;
};

static void t_trailer_iterator(const char *msg, size_t num_expected,
			       struct contents *contents)
{
	struct trailer_iterator iter;
	size_t i = 0;

	trailer_iterator_init(&iter, msg);
	while (trailer_iterator_advance(&iter)) {
		if (num_expected) {
			cl_assert_equal_s(iter.raw, contents[i].raw);
			cl_assert_equal_s(iter.key.buf, contents[i].key);
			cl_assert_equal_s(iter.val.buf, contents[i].val);
		}
		i++;
	}
	trailer_iterator_release(&iter);

	cl_assert_equal_i(i, num_expected);
}

void test_trailer__empty_input(void)
{
	struct contents expected_contents[] = { 0 };
	t_trailer_iterator("", 0, expected_contents);
}

void test_trailer__no_newline_start(void)
{
	struct contents expected_contents[] = { 0 };

	t_trailer_iterator("Fixes: x\n"
			   "Acked-by: x\n"
			   "Reviewed-by: x\n",
			   0,
			   expected_contents);
}

void test_trailer__newline_start(void)
{
	struct contents expected_contents[] = {
		{
			.raw = "Fixes: x\n",
			.key = "Fixes",
			.val = "x",
		},
		{
			.raw = "Acked-by: x\n",
			.key = "Acked-by",
			.val = "x",
		},
		{
			.raw = "Reviewed-by: x\n",
			.key = "Reviewed-by",
			.val = "x",
		},
		{
			0
		},
	};

	t_trailer_iterator("\n"
			   "Fixes: x\n"
			   "Acked-by: x\n"
			   "Reviewed-by: x\n",
			   3,
			   expected_contents);
}

void test_trailer__no_body_text(void)
{
	struct contents expected_contents[] = {

		{
			.raw = "Fixes: x\n",
			.key = "Fixes",
			.val = "x",
		},
		{
			.raw = "Acked-by: x\n",
			.key = "Acked-by",
			.val = "x",
		},
		{
			.raw = "Reviewed-by: x\n",
			.key = "Reviewed-by",
			.val = "x",
		},
		{
			0
		},
	};

	t_trailer_iterator("subject: foo bar\n"
			   "\n"
			   "Fixes: x\n"
			   "Acked-by: x\n"
			   "Reviewed-by: x\n",
			   3,
			   expected_contents);
}

void test_trailer__body_text_no_divider(void)
{
	struct contents expected_contents[] = {
		{
			.raw = "Fixes: x\n",
			.key = "Fixes",
			.val = "x",
		},
		{
			.raw = "Acked-by: x\n",
			.key = "Acked-by",
			.val = "x",
		},
		{
			.raw = "Reviewed-by: x\n",
			.key = "Reviewed-by",
			.val = "x",
		},
		{
			.raw = "Signed-off-by: x\n",
			.key = "Signed-off-by",
			.val = "x",
		},
		{
			0
		},
	};

	t_trailer_iterator("my subject\n"
			   "\n"
			   "my body which is long\n"
			   "and contains some special\n"
			   "chars like : = ? !\n"
			   "hello\n"
			   "\n"
			   "Fixes: x\n"
			   "Acked-by: x\n"
			   "Reviewed-by: x\n"
			   "Signed-off-by: x\n",
			   4,
			   expected_contents);
}

void test_trailer__body_no_divider_2nd_block(void)
{
	struct contents expected_contents[] = {
			{
				.raw = "Helped-by: x\n",
				.key = "Helped-by",
				.val = "x",
			},
			{
				.raw = "Signed-off-by: x\n",
				.key = "Signed-off-by",
				.val = "x",
			},
			{
				0
			},
	};

	t_trailer_iterator("my subject\n"
			   "\n"
			   "my body which is long\n"
			   "and contains some special\n"
			   "chars like : = ? !\n"
			   "hello\n"
			   "\n"
			   "Fixes: x\n"
			   "Acked-by: x\n"
			   "Reviewed-by: x\n"
			   "Signed-off-by: x\n"
			   "\n"
			   /*
			   * Because this is the last trailer block, it takes
			   * precedence over the first one encountered above.
			   */
			   "Helped-by: x\n"
			   "Signed-off-by: x\n",
			   2,
			   expected_contents);
}

void test_trailer__body_and_divider(void)
{
	struct contents expected_contents[] = {
			{
				.raw = "Signed-off-by: x\n",
				.key = "Signed-off-by",
				.val = "x",
			},
			{
				0
			},
	};

	t_trailer_iterator("my subject\n"
			   "\n"
			   "my body which is long\n"
			   "and contains some special\n"
			   "chars like : = ? !\n"
			   "hello\n"
			   "\n"
			   "---\n"
			   "\n"
			   /*
			   * This trailer still counts because the iterator
			   * always ignores the divider.
			   */
			   "Signed-off-by: x\n",
			   1,
			   expected_contents);
}

void test_trailer__non_trailer_in_block(void)
{
	struct contents expected_contents[] = {
		{
			.raw = "not a trailer line\n",
			.key = "not a trailer line",
			.val = "",
		},
		{
			.raw = "not a trailer line\n",
			.key = "not a trailer line",
			.val = "",
		},
		{
			.raw = "not a trailer line\n",
			.key = "not a trailer line",
			.val = "",
		},
		{
			.raw = "Signed-off-by: x\n",
			.key = "Signed-off-by",
			.val = "x",
		},
		{
			0
		},
	};

	t_trailer_iterator("subject: foo bar\n"
			   "\n"
			   /*
			   * Even though this trailer block has a non-trailer line
			   * in it, it's still a valid trailer block because it's
			   * at least 25% trailers and is Git-generated (see
			   * git_generated_prefixes[] in trailer.c).
			   */
			   "not a trailer line\n"
			   "not a trailer line\n"
			   "not a trailer line\n"
			   "Signed-off-by: x\n",
			   /*
			   * Even though there is only really 1 real "trailer"
			   * (Signed-off-by), we still have 4 trailer objects
			   * because we still want to iterate through the entire
			   * block.
			   */
			   4,
			   expected_contents);
}

void test_trailer__too_many_non_trailers(void)
{
	struct contents expected_contents[] = { 0 };

	t_trailer_iterator("subject: foo bar\n"
			   "\n"
			   /*
			   * This block has only 20% trailers, so it's below the
			   * 25% threshold.
			   */
			   "not a trailer line\n"
			   "not a trailer line\n"
			   "not a trailer line\n"
			   "not a trailer line\n"
			   "Signed-off-by: x\n",
			   0,
			   expected_contents);
}

void test_trailer__one_non_trailer_no_git_trailers(void)
{
	struct contents expected_contents[] = { 0 };

	t_trailer_iterator("subject: foo bar\n"
			   "\n"
			   /*
			   * This block has only 1 non-trailer out of 10 (IOW, 90%
			   * trailers) but is not considered a trailer block
			   * because the 25% threshold only applies to cases where
			   * there was a Git-generated trailer.
			   */
			   "Reviewed-by: x\n"
			   "Reviewed-by: x\n"
			   "Reviewed-by: x\n"
			   "Helped-by: x\n"
			   "Helped-by: x\n"
			   "Helped-by: x\n"
			   "Acked-by: x\n"
			   "Acked-by: x\n"
			   "Acked-by: x\n"
			   "not a trailer line\n",
			   0,
			   expected_contents);
}