t/unit-tests: convert trailer test to use clar

Adapt trailer test file to use clar testing framework by using clar assertions where necessary. Split test into individual test functions for clarity and maintainability. Each test case now has its own function, making it easier to isolate failures and improve test readability. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Seyi Kuforiji committed Mar 4, 2025 at 12:33 UTC bc9342771b7438ddf785960cd418ab8c6a7aebef
4 files changed +322 -319
Makefile
+1 -1
@@ -1361,6 +1361,7 @@ CLAR_TEST_SUITES += u-reftable-tree
1361 CLAR_TEST_SUITES += u-strbuf
1362 CLAR_TEST_SUITES += u-strcmp-offset
1363 CLAR_TEST_SUITES += u-strvec
1364 +CLAR_TEST_SUITES += u-trailer
1365 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
1366 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
1367 CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
@@ -1377,7 +1378,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader
1378 UNIT_TEST_PROGRAMS += t-reftable-readwrite
1379 UNIT_TEST_PROGRAMS += t-reftable-record
1380 UNIT_TEST_PROGRAMS += t-reftable-stack
1380 -UNIT_TEST_PROGRAMS += t-trailer
1381 UNIT_TEST_PROGRAMS += t-urlmatch-normalization
1382 UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
1383 UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
t/meson.build
+1 -1
@@ -9,6 +9,7 @@ clar_test_suites = [
9 'unit-tests/u-strbuf.c',
10 'unit-tests/u-strcmp-offset.c',
11 'unit-tests/u-strvec.c',
12 + 'unit-tests/u-trailer.c',
13 ]
14
15 clar_sources = [
@@ -59,7 +60,6 @@ unit_test_programs = [
60 'unit-tests/t-reftable-readwrite.c',
61 'unit-tests/t-reftable-record.c',
62 'unit-tests/t-reftable-stack.c',
62 - 'unit-tests/t-trailer.c',
63 'unit-tests/t-urlmatch-normalization.c',
64 ]
65
t/unit-tests/t-trailer.c deleted
-317
@@ -1,317 +0,0 @@
1 -#define DISABLE_SIGN_COMPARE_WARNINGS
2 -
3 -#include "test-lib.h"
4 -#include "trailer.h"
5 -
6 -struct contents {
7 - const char *raw;
8 - const char *key;
9 - const char *val;
10 -};
11 -
12 -static void t_trailer_iterator(const char *msg, size_t num_expected,
13 - struct contents *contents)
14 -{
15 - struct trailer_iterator iter;
16 - size_t i = 0;
17 -
18 - trailer_iterator_init(&iter, msg);
19 - while (trailer_iterator_advance(&iter)) {
20 - if (num_expected) {
21 - check_str(iter.raw, contents[i].raw);
22 - check_str(iter.key.buf, contents[i].key);
23 - check_str(iter.val.buf, contents[i].val);
24 - }
25 - i++;
26 - }
27 - trailer_iterator_release(&iter);
28 -
29 - check_uint(i, ==, num_expected);
30 -}
31 -
32 -static void run_t_trailer_iterator(void)
33 -{
34 -
35 - static struct test_cases {
36 - const char *name;
37 - const char *msg;
38 - size_t num_expected;
39 - struct contents contents[10];
40 - } tc[] = {
41 - {
42 - "empty input",
43 - "",
44 - 0,
45 - {{0}},
46 - },
47 - {
48 - "no newline at beginning",
49 - "Fixes: x\n"
50 - "Acked-by: x\n"
51 - "Reviewed-by: x\n",
52 - 0,
53 - {{0}},
54 - },
55 - {
56 - "newline at beginning",
57 - "\n"
58 - "Fixes: x\n"
59 - "Acked-by: x\n"
60 - "Reviewed-by: x\n",
61 - 3,
62 - {
63 - {
64 - .raw = "Fixes: x\n",
65 - .key = "Fixes",
66 - .val = "x",
67 - },
68 - {
69 - .raw = "Acked-by: x\n",
70 - .key = "Acked-by",
71 - .val = "x",
72 - },
73 - {
74 - .raw = "Reviewed-by: x\n",
75 - .key = "Reviewed-by",
76 - .val = "x",
77 - },
78 - {
79 - 0
80 - },
81 - },
82 - },
83 - {
84 - "without body text",
85 - "subject: foo bar\n"
86 - "\n"
87 - "Fixes: x\n"
88 - "Acked-by: x\n"
89 - "Reviewed-by: x\n",
90 - 3,
91 - {
92 - {
93 - .raw = "Fixes: x\n",
94 - .key = "Fixes",
95 - .val = "x",
96 - },
97 - {
98 - .raw = "Acked-by: x\n",
99 - .key = "Acked-by",
100 - .val = "x",
101 - },
102 - {
103 - .raw = "Reviewed-by: x\n",
104 - .key = "Reviewed-by",
105 - .val = "x",
106 - },
107 - {
108 - 0
109 - },
110 - },
111 - },
112 - {
113 - "with body text, without divider",
114 - "my subject\n"
115 - "\n"
116 - "my body which is long\n"
117 - "and contains some special\n"
118 - "chars like : = ? !\n"
119 - "hello\n"
120 - "\n"
121 - "Fixes: x\n"
122 - "Acked-by: x\n"
123 - "Reviewed-by: x\n"
124 - "Signed-off-by: x\n",
125 - 4,
126 - {
127 - {
128 - .raw = "Fixes: x\n",
129 - .key = "Fixes",
130 - .val = "x",
131 - },
132 - {
133 - .raw = "Acked-by: x\n",
134 - .key = "Acked-by",
135 - .val = "x",
136 - },
137 - {
138 - .raw = "Reviewed-by: x\n",
139 - .key = "Reviewed-by",
140 - .val = "x",
141 - },
142 - {
143 - .raw = "Signed-off-by: x\n",
144 - .key = "Signed-off-by",
145 - .val = "x",
146 - },
147 - {
148 - 0
149 - },
150 - },
151 - },
152 - {
153 - "with body text, without divider (second trailer block)",
154 - "my subject\n"
155 - "\n"
156 - "my body which is long\n"
157 - "and contains some special\n"
158 - "chars like : = ? !\n"
159 - "hello\n"
160 - "\n"
161 - "Fixes: x\n"
162 - "Acked-by: x\n"
163 - "Reviewed-by: x\n"
164 - "Signed-off-by: x\n"
165 - "\n"
166 - /*
167 - * Because this is the last trailer block, it takes
168 - * precedence over the first one encountered above.
169 - */
170 - "Helped-by: x\n"
171 - "Signed-off-by: x\n",
172 - 2,
173 - {
174 - {
175 - .raw = "Helped-by: x\n",
176 - .key = "Helped-by",
177 - .val = "x",
178 - },
179 - {
180 - .raw = "Signed-off-by: x\n",
181 - .key = "Signed-off-by",
182 - .val = "x",
183 - },
184 - {
185 - 0
186 - },
187 - },
188 - },
189 - {
190 - "with body text, with divider",
191 - "my subject\n"
192 - "\n"
193 - "my body which is long\n"
194 - "and contains some special\n"
195 - "chars like : = ? !\n"
196 - "hello\n"
197 - "\n"
198 - "---\n"
199 - "\n"
200 - /*
201 - * This trailer still counts because the iterator
202 - * always ignores the divider.
203 - */
204 - "Signed-off-by: x\n",
205 - 1,
206 - {
207 - {
208 - .raw = "Signed-off-by: x\n",
209 - .key = "Signed-off-by",
210 - .val = "x",
211 - },
212 - {
213 - 0
214 - },
215 - },
216 - },
217 - {
218 - "with non-trailer lines in trailer block",
219 - "subject: foo bar\n"
220 - "\n"
221 - /*
222 - * Even though this trailer block has a non-trailer line
223 - * in it, it's still a valid trailer block because it's
224 - * at least 25% trailers and is Git-generated (see
225 - * git_generated_prefixes[] in trailer.c).
226 - */
227 - "not a trailer line\n"
228 - "not a trailer line\n"
229 - "not a trailer line\n"
230 - "Signed-off-by: x\n",
231 - /*
232 - * Even though there is only really 1 real "trailer"
233 - * (Signed-off-by), we still have 4 trailer objects
234 - * because we still want to iterate through the entire
235 - * block.
236 - */
237 - 4,
238 - {
239 - {
240 - .raw = "not a trailer line\n",
241 - .key = "not a trailer line",
242 - .val = "",
243 - },
244 - {
245 - .raw = "not a trailer line\n",
246 - .key = "not a trailer line",
247 - .val = "",
248 - },
249 - {
250 - .raw = "not a trailer line\n",
251 - .key = "not a trailer line",
252 - .val = "",
253 - },
254 - {
255 - .raw = "Signed-off-by: x\n",
256 - .key = "Signed-off-by",
257 - .val = "x",
258 - },
259 - {
260 - 0
261 - },
262 - },
263 - },
264 - {
265 - "with non-trailer lines (one too many) in trailer block",
266 - "subject: foo bar\n"
267 - "\n"
268 - /*
269 - * This block has only 20% trailers, so it's below the
270 - * 25% threshold.
271 - */
272 - "not a trailer line\n"
273 - "not a trailer line\n"
274 - "not a trailer line\n"
275 - "not a trailer line\n"
276 - "Signed-off-by: x\n",
277 - 0,
278 - {{0}},
279 - },
280 - {
281 - "with non-trailer lines (only 1) in trailer block, but no Git-generated trailers",
282 - "subject: foo bar\n"
283 - "\n"
284 - /*
285 - * This block has only 1 non-trailer out of 10 (IOW, 90%
286 - * trailers) but is not considered a trailer block
287 - * because the 25% threshold only applies to cases where
288 - * there was a Git-generated trailer.
289 - */
290 - "Reviewed-by: x\n"
291 - "Reviewed-by: x\n"
292 - "Reviewed-by: x\n"
293 - "Helped-by: x\n"
294 - "Helped-by: x\n"
295 - "Helped-by: x\n"
296 - "Acked-by: x\n"
297 - "Acked-by: x\n"
298 - "Acked-by: x\n"
299 - "not a trailer line\n",
300 - 0,
301 - {{0}},
302 - },
303 - };
304 -
305 - for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) {
306 - TEST(t_trailer_iterator(tc[i].msg,
307 - tc[i].num_expected,
308 - tc[i].contents),
309 - "%s", tc[i].name);
310 - }
311 -}
312 -
313 -int cmd_main(int argc UNUSED, const char **argv UNUSED)
314 -{
315 - run_t_trailer_iterator();
316 - return test_done();
317 -}
t/unit-tests/u-trailer.c new
+320
@@ -0,0 +1,320 @@
1 +#define DISABLE_SIGN_COMPARE_WARNINGS
2 +
3 +#include "unit-test.h"
4 +#include "trailer.h"
5 +
6 +struct contents {
7 + const char *raw;
8 + const char *key;
9 + const char *val;
10 +};
11 +
12 +static void t_trailer_iterator(const char *msg, size_t num_expected,
13 + struct contents *contents)
14 +{
15 + struct trailer_iterator iter;
16 + size_t i = 0;
17 +
18 + trailer_iterator_init(&iter, msg);
19 + while (trailer_iterator_advance(&iter)) {
20 + if (num_expected) {
21 + cl_assert_equal_s(iter.raw, contents[i].raw);
22 + cl_assert_equal_s(iter.key.buf, contents[i].key);
23 + cl_assert_equal_s(iter.val.buf, contents[i].val);
24 + }
25 + i++;
26 + }
27 + trailer_iterator_release(&iter);
28 +
29 + cl_assert_equal_i(i, num_expected);
30 +}
31 +
32 +void test_trailer__empty_input(void)
33 +{
34 + struct contents expected_contents[] = { 0 };
35 + t_trailer_iterator("", 0, expected_contents);
36 +}
37 +
38 +void test_trailer__no_newline_start(void)
39 +{
40 + struct contents expected_contents[] = { 0 };
41 +
42 + t_trailer_iterator("Fixes: x\n"
43 + "Acked-by: x\n"
44 + "Reviewed-by: x\n",
45 + 0,
46 + expected_contents);
47 +}
48 +
49 +void test_trailer__newline_start(void)
50 +{
51 + struct contents expected_contents[] = {
52 + {
53 + .raw = "Fixes: x\n",
54 + .key = "Fixes",
55 + .val = "x",
56 + },
57 + {
58 + .raw = "Acked-by: x\n",
59 + .key = "Acked-by",
60 + .val = "x",
61 + },
62 + {
63 + .raw = "Reviewed-by: x\n",
64 + .key = "Reviewed-by",
65 + .val = "x",
66 + },
67 + {
68 + 0
69 + },
70 + };
71 +
72 + t_trailer_iterator("\n"
73 + "Fixes: x\n"
74 + "Acked-by: x\n"
75 + "Reviewed-by: x\n",
76 + 3,
77 + expected_contents);
78 +}
79 +
80 +void test_trailer__no_body_text(void)
81 +{
82 + struct contents expected_contents[] = {
83 +
84 + {
85 + .raw = "Fixes: x\n",
86 + .key = "Fixes",
87 + .val = "x",
88 + },
89 + {
90 + .raw = "Acked-by: x\n",
91 + .key = "Acked-by",
92 + .val = "x",
93 + },
94 + {
95 + .raw = "Reviewed-by: x\n",
96 + .key = "Reviewed-by",
97 + .val = "x",
98 + },
99 + {
100 + 0
101 + },
102 + };
103 +
104 + t_trailer_iterator("subject: foo bar\n"
105 + "\n"
106 + "Fixes: x\n"
107 + "Acked-by: x\n"
108 + "Reviewed-by: x\n",
109 + 3,
110 + expected_contents);
111 +}
112 +
113 +void test_trailer__body_text_no_divider(void)
114 +{
115 + struct contents expected_contents[] = {
116 + {
117 + .raw = "Fixes: x\n",
118 + .key = "Fixes",
119 + .val = "x",
120 + },
121 + {
122 + .raw = "Acked-by: x\n",
123 + .key = "Acked-by",
124 + .val = "x",
125 + },
126 + {
127 + .raw = "Reviewed-by: x\n",
128 + .key = "Reviewed-by",
129 + .val = "x",
130 + },
131 + {
132 + .raw = "Signed-off-by: x\n",
133 + .key = "Signed-off-by",
134 + .val = "x",
135 + },
136 + {
137 + 0
138 + },
139 + };
140 +
141 + t_trailer_iterator("my subject\n"
142 + "\n"
143 + "my body which is long\n"
144 + "and contains some special\n"
145 + "chars like : = ? !\n"
146 + "hello\n"
147 + "\n"
148 + "Fixes: x\n"
149 + "Acked-by: x\n"
150 + "Reviewed-by: x\n"
151 + "Signed-off-by: x\n",
152 + 4,
153 + expected_contents);
154 +}
155 +
156 +void test_trailer__body_no_divider_2nd_block(void)
157 +{
158 + struct contents expected_contents[] = {
159 + {
160 + .raw = "Helped-by: x\n",
161 + .key = "Helped-by",
162 + .val = "x",
163 + },
164 + {
165 + .raw = "Signed-off-by: x\n",
166 + .key = "Signed-off-by",
167 + .val = "x",
168 + },
169 + {
170 + 0
171 + },
172 + };
173 +
174 + t_trailer_iterator("my subject\n"
175 + "\n"
176 + "my body which is long\n"
177 + "and contains some special\n"
178 + "chars like : = ? !\n"
179 + "hello\n"
180 + "\n"
181 + "Fixes: x\n"
182 + "Acked-by: x\n"
183 + "Reviewed-by: x\n"
184 + "Signed-off-by: x\n"
185 + "\n"
186 + /*
187 + * Because this is the last trailer block, it takes
188 + * precedence over the first one encountered above.
189 + */
190 + "Helped-by: x\n"
191 + "Signed-off-by: x\n",
192 + 2,
193 + expected_contents);
194 +}
195 +
196 +void test_trailer__body_and_divider(void)
197 +{
198 + struct contents expected_contents[] = {
199 + {
200 + .raw = "Signed-off-by: x\n",
201 + .key = "Signed-off-by",
202 + .val = "x",
203 + },
204 + {
205 + 0
206 + },
207 + };
208 +
209 + t_trailer_iterator("my subject\n"
210 + "\n"
211 + "my body which is long\n"
212 + "and contains some special\n"
213 + "chars like : = ? !\n"
214 + "hello\n"
215 + "\n"
216 + "---\n"
217 + "\n"
218 + /*
219 + * This trailer still counts because the iterator
220 + * always ignores the divider.
221 + */
222 + "Signed-off-by: x\n",
223 + 1,
224 + expected_contents);
225 +}
226 +
227 +void test_trailer__non_trailer_in_block(void)
228 +{
229 + struct contents expected_contents[] = {
230 + {
231 + .raw = "not a trailer line\n",
232 + .key = "not a trailer line",
233 + .val = "",
234 + },
235 + {
236 + .raw = "not a trailer line\n",
237 + .key = "not a trailer line",
238 + .val = "",
239 + },
240 + {
241 + .raw = "not a trailer line\n",
242 + .key = "not a trailer line",
243 + .val = "",
244 + },
245 + {
246 + .raw = "Signed-off-by: x\n",
247 + .key = "Signed-off-by",
248 + .val = "x",
249 + },
250 + {
251 + 0
252 + },
253 + };
254 +
255 + t_trailer_iterator("subject: foo bar\n"
256 + "\n"
257 + /*
258 + * Even though this trailer block has a non-trailer line
259 + * in it, it's still a valid trailer block because it's
260 + * at least 25% trailers and is Git-generated (see
261 + * git_generated_prefixes[] in trailer.c).
262 + */
263 + "not a trailer line\n"
264 + "not a trailer line\n"
265 + "not a trailer line\n"
266 + "Signed-off-by: x\n",
267 + /*
268 + * Even though there is only really 1 real "trailer"
269 + * (Signed-off-by), we still have 4 trailer objects
270 + * because we still want to iterate through the entire
271 + * block.
272 + */
273 + 4,
274 + expected_contents);
275 +}
276 +
277 +void test_trailer__too_many_non_trailers(void)
278 +{
279 + struct contents expected_contents[] = { 0 };
280 +
281 + t_trailer_iterator("subject: foo bar\n"
282 + "\n"
283 + /*
284 + * This block has only 20% trailers, so it's below the
285 + * 25% threshold.
286 + */
287 + "not a trailer line\n"
288 + "not a trailer line\n"
289 + "not a trailer line\n"
290 + "not a trailer line\n"
291 + "Signed-off-by: x\n",
292 + 0,
293 + expected_contents);
294 +}
295 +
296 +void test_trailer__one_non_trailer_no_git_trailers(void)
297 +{
298 + struct contents expected_contents[] = { 0 };
299 +
300 + t_trailer_iterator("subject: foo bar\n"
301 + "\n"
302 + /*
303 + * This block has only 1 non-trailer out of 10 (IOW, 90%
304 + * trailers) but is not considered a trailer block
305 + * because the 25% threshold only applies to cases where
306 + * there was a Git-generated trailer.
307 + */
308 + "Reviewed-by: x\n"
309 + "Reviewed-by: x\n"
310 + "Reviewed-by: x\n"
311 + "Helped-by: x\n"
312 + "Helped-by: x\n"
313 + "Helped-by: x\n"
314 + "Acked-by: x\n"
315 + "Acked-by: x\n"
316 + "Acked-by: x\n"
317 + "not a trailer line\n",
318 + 0,
319 + expected_contents);
320 +}