-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformat-commit-msg.bats
More file actions
390 lines (331 loc) · 12.7 KB
/
Copy pathformat-commit-msg.bats
File metadata and controls
390 lines (331 loc) · 12.7 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env bats
formatter="$BATS_TEST_DIRNAME/../git/.config/git/hooks/format-commit-msg.sh"
setup() {
tmp_msg=$(mktemp)
}
teardown() {
rm -f "$tmp_msg"
}
# Subject: required {{{
@test "empty subject fails" {
printf '\n' > "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 1 ]
[[ "$output" =~ "[commit-msg] subject is required" ]]
}
# }}}
# Subject: auto-squash bypass {{{
@test "fixup commit exits 0 and message is unchanged" {
printf 'fixup! Original subject\n' > "$tmp_msg"
original=$(cat "$tmp_msg")
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(cat "$tmp_msg")" = "$original" ]
}
@test "squash commit exits 0 and message is unchanged" {
printf 'squash! Original subject\n' > "$tmp_msg"
original=$(cat "$tmp_msg")
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(cat "$tmp_msg")" = "$original" ]
}
@test "amend commit exits 0 and message is unchanged" {
printf 'amend! Original subject\n' > "$tmp_msg"
original=$(cat "$tmp_msg")
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(cat "$tmp_msg")" = "$original" ]
}
@test "fixup commit with body exits 0 and message is unchanged" {
printf 'fixup! Original subject\n\nBody text.\n' > "$tmp_msg"
original=$(cat "$tmp_msg")
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(cat "$tmp_msg")" = "$original" ]
}
# }}}
# Subject: length {{{
@test "subject at exactly 72 characters passes" {
printf '%072d\n' 0 > "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
}
@test "subject at 73 characters fails" {
printf '%073d\n' 0 > "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 1 ]
[[ "$output" =~ "[commit-msg] subject exceeds 72 characters" ]]
}
@test "subject too long leaves file unmodified" {
printf '%073d\n' 0 > "$tmp_msg"
original=$(cat "$tmp_msg")
run "$formatter" "$tmp_msg"
[ "$status" -eq 1 ]
[ "$(cat "$tmp_msg")" = "$original" ]
}
# }}}
# Subject: capitalization {{{
@test "uppercase subject without scope passes" {
printf 'Fix the bug\n' > "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
}
@test "lowercase subject without scope fails" {
printf 'fix the bug\n' > "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 1 ]
[[ "$output" =~ "[commit-msg] subject must begin with a capital letter" ]]
}
@test "scoped subject with uppercase text passes" {
printf 'scope: Fix the bug\n' > "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
}
@test "scoped subject with lowercase text fails" {
printf 'scope: fix the bug\n' > "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 1 ]
[[ "$output" =~ "[commit-msg] subject must begin with a capital letter" ]]
}
# }}}
# Subject: blank line separator {{{
@test "body without blank line after subject fails" {
printf 'scope: Fix the bug\nBody without blank line.\n' > "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 1 ]
[[ "$output" =~ "[commit-msg] missing blank line after subject" ]]
}
@test "git comments directly after subject pass and blank separator is inserted" {
printf 'scope: Fix the bug\n# Git comment.\n# Another comment.\n' > "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '1p' "$tmp_msg")" = "scope: Fix the bug" ]
[ -z "$(sed -n '2p' "$tmp_msg")" ]
[ "$(sed -n '3p' "$tmp_msg")" = "# Git comment." ]
}
# }}}
# Body: pass-through {{{
@test "subject only passes and file is unchanged" {
printf 'scope: Fix the bug\n' > "$tmp_msg"
original=$(cat "$tmp_msg")
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(cat "$tmp_msg")" = "$original" ]
}
@test "subject and blank line passes and file is unchanged" {
printf 'scope: Fix the bug\n\n' > "$tmp_msg"
original=$(cat "$tmp_msg")
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(cat "$tmp_msg")" = "$original" ]
}
@test "already wrapped body is unchanged" {
printf 'scope: Fix the bug\n\nShort body.\n' > "$tmp_msg"
original=$(cat "$tmp_msg")
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(cat "$tmp_msg")" = "$original" ]
}
# }}}
# Body: paragraph wrapping {{{
@test "body is wrapped" {
# "seventy-two" is the last word that fits within 72 chars (65 total);
# adding "characters" would push it to 76.
printf 'scope: Fix the bug\n\n%s\n' \
'This is a very long body line that definitely exceeds seventy-two characters and needs wrapping.' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
first_body_line=$(sed -n '3p' "$tmp_msg")
[ "$first_body_line" = "This is a very long body line that definitely exceeds seventy-two" ]
}
@test "short body lines are reflowed into one" {
# Two lines that together fit within 72 chars should be joined into one.
printf 'scope: Fix the bug\n\n%s\n%s\n' \
'This is the first part of the sentence' \
'and this is the second part.' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
first_body_line=$(sed -n '3p' "$tmp_msg")
[ "$first_body_line" = "This is the first part of the sentence and this is the second part." ]
[ -z "$(sed -n '4p' "$tmp_msg")" ]
}
@test "body paragraph uses greedy wrapping" {
# fmt optimal-fit breaks after "invariants," leaving "and" on the next
# line (67+67 chars). Greedy wrapping fits "and" on line 1 (71+71 chars).
printf 'scope: Fix the bug\n\n%s\n' \
'Introduce AGENTS.md to define project-specific context, invariants, and directory structure. This assists AI coding agents by directing them to the primary network specification document for VLAN/IPAM and STP configurations.' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "Introduce AGENTS.md to define project-specific context, invariants, and" ]
[ "$(sed -n '4p' "$tmp_msg")" = "directory structure. This assists AI coding agents by directing them to" ]
[ "$(sed -n '5p' "$tmp_msg")" = "the primary network specification document for VLAN/IPAM and STP" ]
[ "$(sed -n '6p' "$tmp_msg")" = "configurations." ]
}
@test "blank line between body paragraphs is preserved" {
printf 'scope: Fix the bug\n\n%s\n\n%s\n' \
'First paragraph.' \
'Second paragraph.' \
> "$tmp_msg"
original=$(cat "$tmp_msg")
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(cat "$tmp_msg")" = "$original" ]
}
# }}}
# Body: list items {{{
@test "list items are not joined into a single paragraph" {
printf 'scope: Fix the bug\n\n%s\n%s\n' \
'- First item' \
'- Second item' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "- First item" ]
[ "$(sed -n '4p' "$tmp_msg")" = "- Second item" ]
}
@test "asterisk list marker is recognized and items are not joined" {
printf 'scope: Fix the bug\n\n%s\n%s\n' \
'* First item' \
'* Second item' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "* First item" ]
[ "$(sed -n '4p' "$tmp_msg")" = "* Second item" ]
}
@test "plus list marker is recognized and items are not joined" {
printf 'scope: Fix the bug\n\n%s\n%s\n' \
'+ First item' \
'+ Second item' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "+ First item" ]
[ "$(sed -n '4p' "$tmp_msg")" = "+ Second item" ]
}
@test "short list items are unchanged" {
printf 'scope: Fix the bug\n\n%s\n' \
'- Short item' \
> "$tmp_msg"
original=$(cat "$tmp_msg")
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(cat "$tmp_msg")" = "$original" ]
}
@test "long list item wraps with hanging indent" {
# "seventy-two" is the last word fitting in 70 chars (text width for "- ")
printf 'scope: Fix the bug\n\n%s\n' \
'- This is a very long list item that definitely exceeds seventy-two characters and needs wrapping.' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "- This is a very long list item that definitely exceeds seventy-two" ]
[ "$(sed -n '4p' "$tmp_msg")" = " characters and needs wrapping." ]
}
@test "long asterisk list item wraps with hanging indent" {
printf 'scope: Fix the bug\n\n%s\n' \
'* This is a very long list item that definitely exceeds seventy-two characters and needs wrapping.' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "* This is a very long list item that definitely exceeds seventy-two" ]
[ "$(sed -n '4p' "$tmp_msg")" = " characters and needs wrapping." ]
}
@test "long plus list item wraps with hanging indent" {
printf 'scope: Fix the bug\n\n%s\n' \
'+ This is a very long list item that definitely exceeds seventy-two characters and needs wrapping.' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "+ This is a very long list item that definitely exceeds seventy-two" ]
[ "$(sed -n '4p' "$tmp_msg")" = " characters and needs wrapping." ]
}
@test "numbered list item wraps with correct indent" {
# "seventy-two" is the last word fitting in 69 chars (text width for "1. ")
printf 'scope: Fix the bug\n\n%s\n' \
'1. This is a very long list item that definitely exceeds seventy-two characters and needs wrapping.' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "1. This is a very long list item that definitely exceeds seventy-two" ]
[ "$(sed -n '4p' "$tmp_msg")" = " characters and needs wrapping." ]
}
@test "multi-digit numbered list item wraps with correct indent" {
printf 'scope: Fix the bug\n\n%s\n' \
'10. This is a very long list item that definitely exceeds seventy-two characters and needs wrapping.' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "10. This is a very long list item that definitely exceeds seventy-two" ]
[ "$(sed -n '4p' "$tmp_msg")" = " characters and needs wrapping." ]
}
@test "list item with continuation lines is re-wrapped" {
printf 'scope: Fix the bug\n\n%s\n%s\n' \
'- This is a very long list item that definitely exceeds seventy-two' \
' characters and needs wrapping.' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "- This is a very long list item that definitely exceeds seventy-two" ]
[ "$(sed -n '4p' "$tmp_msg")" = " characters and needs wrapping." ]
}
@test "list item continuation that fits is reflowed onto one line" {
printf 'scope: Fix the bug\n\n%s\n%s\n' \
'- Short item' \
' continuation' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "- Short item continuation" ]
[ "$(wc -l < "$tmp_msg")" -eq 3 ]
}
@test "blank line between list items is preserved" {
printf 'scope: Fix the bug\n\n%s\n\n%s\n' \
'- First item' \
'- Second item' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "- First item" ]
[ -z "$(sed -n '4p' "$tmp_msg")" ]
[ "$(sed -n '5p' "$tmp_msg")" = "- Second item" ]
}
@test "list item followed by paragraph without blank line" {
printf 'scope: Fix the bug\n\n%s\n%s\n' \
'- List item' \
'Paragraph text.' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "- List item" ]
[ "$(sed -n '4p' "$tmp_msg")" = "Paragraph text." ]
}
# }}}
# Body: mixed content {{{
@test "mixed paragraph and list are formatted independently" {
printf 'scope: Fix the bug\n\n%s\n\n%s\n%s\n' \
'This paragraph introduces the changes made below.' \
'- First item' \
'- Second item' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
[ "$(sed -n '3p' "$tmp_msg")" = "This paragraph introduces the changes made below." ]
[ -z "$(sed -n '4p' "$tmp_msg")" ]
[ "$(sed -n '5p' "$tmp_msg")" = "- First item" ]
[ "$(sed -n '6p' "$tmp_msg")" = "- Second item" ]
}
# }}}
# Body: git comments {{{
@test "trailing git comment block is preserved verbatim" {
printf 'scope: Fix the bug\n\nBody text.\n# ------------------------ >8 ------------------------\n# Comment line.\n' \
> "$tmp_msg"
run "$formatter" "$tmp_msg"
[ "$status" -eq 0 ]
grep -q '^# Comment line\.$' "$tmp_msg"
grep -q '^# ---' "$tmp_msg"
}
# }}}