-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvalidatePrTitle.test.js
More file actions
429 lines (371 loc) · 14.4 KB
/
Copy pathvalidatePrTitle.test.js
File metadata and controls
429 lines (371 loc) · 14.4 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import {describe, expect, it} from 'vitest';
import validatePrTitle from './validatePrTitle.js';
it('allows valid PR titles that use the default types', async () => {
const inputs = [
'fix: Fix bug',
'fix!: Fix bug',
'feat: Add feature',
'feat!: Add feature',
'refactor: Internal cleanup'
];
for (let index = 0; index < inputs.length; index++) {
await validatePrTitle(inputs[index]);
}
});
it('throws for PR titles without a type', async () => {
await expect(validatePrTitle('Fix bug')).rejects.toThrow(
'No release type found in pull request title "Fix bug".'
);
});
it('throws for PR titles with only a type', async () => {
await expect(validatePrTitle('fix:')).rejects.toThrow(
'No release type found in pull request title "fix:".'
);
});
it('throws for PR titles without a subject', async () => {
await expect(validatePrTitle('fix: ')).rejects.toThrow(
'No subject found in pull request title "fix: ".'
);
});
it('throws for PR titles with an unknown type', async () => {
await expect(validatePrTitle('foo: Bar')).rejects.toThrow(
'Unknown release type "foo" found in pull request title "foo: Bar".'
);
});
describe('regex types', () => {
const headerPattern = /^([\w-]*)(?:\(([\w$.\-*/ ]*)\))?: (.*)$/;
it('allows a regex matching type', async () => {
await validatePrTitle('JIRA-123: Bar', {
types: ['JIRA-\\d+'],
headerPattern
});
});
it('can be used for dynamic Jira keys', async () => {
const inputs = ['JIRA-123', 'P-123', 'INT-31', 'CONF-0'];
for (let index = 0; index < inputs.length; index++) {
await validatePrTitle(`${inputs[index]}: did the thing`, {
types: ['[A-Z]+-\\d+'],
headerPattern
});
}
});
it('throws for PR titles without a type', async () => {
await expect(
validatePrTitle('Fix JIRA-123 bug', {
types: ['JIRA-\\d+'],
headerPattern
})
).rejects.toThrow(
'No release type found in pull request title "Fix JIRA-123 bug".'
);
});
it('throws for PR titles with only a type', async () => {
await expect(
validatePrTitle('JIRA-123:', {
types: ['JIRA-\\d+'],
headerPattern
})
).rejects.toThrow(
'No release type found in pull request title "JIRA-123:".'
);
});
it('throws for PR titles without a subject', async () => {
await expect(
validatePrTitle('JIRA-123: ', {
types: ['JIRA-\\d+'],
headerPattern
})
).rejects.toThrow('No subject found in pull request title "JIRA-123: ".');
});
it('throws for PR titles that do not match the regex', async () => {
await expect(
validatePrTitle('CONF-123: ', {
types: ['JIRA-\\d+'],
headerPattern
})
).rejects.toThrow('No subject found in pull request title "CONF-123: ".');
});
it('throws when an unknown type is detected for auto-wrapping regex', async () => {
await expect(
validatePrTitle('JIRA-123A: Bar', {
types: ['JIRA-\\d+'],
headerPattern
})
).rejects.toThrow(
'Unknown release type "JIRA-123A" found in pull request title "JIRA-123A: Bar". \n\nAvailable types:\n - JIRA-\\d+'
);
});
it('allows scopes when using a regex for the type', async () => {
await validatePrTitle('JIRA-123(core): Bar', {
types: ['JIRA-\\d+'],
headerPattern
});
});
});
describe('defined scopes', () => {
it('allows a missing scope by default', async () => {
await validatePrTitle('fix: Bar');
});
it('allows all scopes by default', async () => {
await validatePrTitle('fix(core): Bar');
});
it('allows a missing scope when custom scopes are defined', async () => {
await validatePrTitle('fix: Bar', {scopes: ['foo']});
});
it('allows a matching scope', async () => {
await validatePrTitle('fix(core): Bar', {scopes: ['core']});
});
it('allows a regex matching scope', async () => {
await validatePrTitle('fix(CORE): Bar', {scopes: ['[A-Z]+']});
});
it('allows multiple matching scopes', async () => {
await validatePrTitle('fix(core,e2e): Bar', {
scopes: ['core', 'e2e', 'web']
});
});
it('allows multiple regex matching scopes', async () => {
await validatePrTitle('fix(CORE,WEB): Bar', {
scopes: ['[A-Z]+']
});
});
it('throws when an unknown scope is detected within multiple scopes', async () => {
await expect(
validatePrTitle('fix(core,e2e,foo,bar): Bar', {scopes: ['foo', 'core']})
).rejects.toThrow(
'Unknown scopes "e2e,bar" found in pull request title "fix(core,e2e,foo,bar): Bar". Scope must match one of: foo, core.'
);
});
it('throws when an unknown scope is detected within multiple scopes and a regex', async () => {
await expect(
validatePrTitle('fix(CORE,e2e,foo,bar): Bar', {
scopes: ['foo', '[A-Z]+']
})
).rejects.toThrow(
'Unknown scopes "e2e,bar" found in pull request title "fix(CORE,e2e,foo,bar): Bar". Scope must match one of: foo, [A-Z]+.'
);
});
it('throws when an unknown scope is detected', async () => {
await expect(
validatePrTitle('fix(core): Bar', {scopes: ['foo']})
).rejects.toThrow(
'Unknown scope "core" found in pull request title "fix(core): Bar". Scope must match one of: foo.'
);
});
it('throws when an unknown scope is detected for auto-wrapped regex matching', async () => {
await expect(
validatePrTitle('fix(score): Bar', {scopes: ['core']})
).rejects.toThrow(
'Unknown scope "score" found in pull request title "fix(score): Bar". Scope must match one of: core.'
);
});
it('throws when an unknown scope is detected for auto-wrapped regex matching when input is already wrapped', async () => {
await expect(
validatePrTitle('fix(score): Bar', {scopes: ['^[A-Z]+$']})
).rejects.toThrow(
'Unknown scope "score" found in pull request title "fix(score): Bar". Scope must match one of: ^[A-Z]+$.'
);
});
it('throws when an unknown scope is detected for regex matching', async () => {
await expect(
validatePrTitle('fix(core): Bar', {scopes: ['[A-Z]+']})
).rejects.toThrow(
'Unknown scope "core" found in pull request title "fix(core): Bar". Scope must match one of: [A-Z]+.'
);
});
describe('require scope', () => {
describe('scope allowlist defined', () => {
it('passes when a scope is provided', async () => {
await validatePrTitle('fix(core): Bar', {
scopes: ['core'],
requireScope: true
});
});
it('throws when a scope is missing', async () => {
await expect(
validatePrTitle('fix: Bar', {
scopes: ['foo', 'bar'],
requireScope: true
})
).rejects.toThrow(
'No scope found in pull request title "fix: Bar". Scope must match one of: foo, bar.'
);
});
});
describe('disallow scopes', () => {
it('passes when a single scope is provided, but not present in disallowScopes with one item', async () => {
await validatePrTitle('fix(core): Bar', {disallowScopes: ['release']});
});
it('passes when a single scope is provided, but not present in disallowScopes with one regex item', async () => {
await validatePrTitle('fix(core): Bar', {disallowScopes: ['[A-Z]+']});
});
it('passes when multiple scopes are provided, but not present in disallowScopes with one item', async () => {
await validatePrTitle('fix(core,e2e,bar): Bar', {
disallowScopes: ['release']
});
});
it('passes when multiple scopes are provided, but not present in disallowScopes with one regex item', async () => {
await validatePrTitle('fix(core,e2e,bar): Bar', {
disallowScopes: ['[A-Z]+']
});
});
it('passes when a single scope is provided, but not present in disallowScopes with multiple items', async () => {
await validatePrTitle('fix(core): Bar', {
disallowScopes: ['release', 'test', '[A-Z]+']
});
});
it('passes when multiple scopes are provided, but not present in disallowScopes with multiple items', async () => {
await validatePrTitle('fix(core,e2e,bar): Bar', {
disallowScopes: ['release', 'test', '[A-Z]+']
});
});
it('throws when a single scope is provided and it is present in disallowScopes with one item', async () => {
await expect(
validatePrTitle('fix(release): Bar', {disallowScopes: ['release']})
).rejects.toThrow('Disallowed scope was found: release');
});
it('throws when a single scope is provided and it is present in disallowScopes with one regex item', async () => {
await expect(
validatePrTitle('fix(RELEASE): Bar', {disallowScopes: ['[A-Z]+']})
).rejects.toThrow('Disallowed scope was found: RELEASE');
});
it('throws when a single scope is provided and it is present in disallowScopes with multiple item', async () => {
await expect(
validatePrTitle('fix(release): Bar', {
disallowScopes: ['release', 'test']
})
).rejects.toThrow('Disallowed scope was found: release');
});
it('throws when a single scope is provided and it is present in disallowScopes with multiple regex item', async () => {
await expect(
validatePrTitle('fix(RELEASE): Bar', {
disallowScopes: ['[A-Z]+', '^[A-Z].+$']
})
).rejects.toThrow('Disallowed scope was found: RELEASE');
});
it('throws when multiple scopes are provided and one of them is present in disallowScopes with one item ', async () => {
await expect(
validatePrTitle('fix(release,e2e): Bar', {
disallowScopes: ['release']
})
).rejects.toThrow('Disallowed scope was found: release');
});
it('throws when multiple scopes are provided and one of them is present in disallowScopes with one regex item ', async () => {
await expect(
validatePrTitle('fix(RELEASE,e2e): Bar', {
disallowScopes: ['[A-Z]+']
})
).rejects.toThrow('Disallowed scope was found: RELEASE');
});
it('throws when multiple scopes are provided and one of them is present in disallowScopes with multiple items ', async () => {
await expect(
validatePrTitle('fix(release,e2e): Bar', {
disallowScopes: ['release', 'test']
})
).rejects.toThrow('Disallowed scope was found: release');
});
it('throws when multiple scopes are provided and one of them is present in disallowScopes with multiple items and a regex', async () => {
await expect(
validatePrTitle('fix(RELEASE,e2e): Bar', {
disallowScopes: ['[A-Z]+', 'test']
})
).rejects.toThrow('Disallowed scope was found: RELEASE');
});
it('throws when multiple scopes are provided and more than one of them are present in disallowScopes', async () => {
await expect(
validatePrTitle('fix(release,test,CORE): Bar', {
disallowScopes: ['release', 'test', '[A-Z]+']
})
).rejects.toThrow('Disallowed scopes were found: release, test, CORE');
});
});
describe('scope allowlist not defined', () => {
it('passes when a scope is provided', async () => {
await validatePrTitle('fix(core): Bar', {
requireScope: true
});
});
it('throws when a scope is missing', async () => {
await expect(
validatePrTitle('fix: Bar', {
requireScope: true
})
).rejects.toThrow(
// Should make no mention of any available scope
/^No scope found in pull request title "fix: Bar".$/
);
});
});
});
});
describe('custom types', () => {
it('allows PR titles with a supported type', async () => {
const inputs = ['foo: Foobar', 'bar: Foobar', 'baz: Foobar'];
const types = ['foo', 'bar', 'baz'];
for (let index = 0; index < inputs.length; index++) {
await validatePrTitle(inputs[index], {types});
}
});
it('throws for PR titles with an unknown type', async () => {
await expect(
validatePrTitle('fix: Foobar', {types: ['foo', 'bar']})
).rejects.toThrow(
'Unknown release type "fix" found in pull request title "fix: Foobar".'
);
});
});
describe('description validation', () => {
it('does not validate the description by default', async () => {
await validatePrTitle('fix: sK!"§4123');
});
it('can pass the validation when `subjectPatternError` is configured', async () => {
await validatePrTitle('fix: foobar', {
subjectPattern: '^(?![A-Z]).+$',
subjectPatternError:
'The subject found in the pull request title cannot start with an uppercase character.'
});
});
it('uses the `subjectPatternError` if available when the `subjectPattern` does not match', async () => {
const customError =
'The subject found in the pull request title cannot start with an uppercase character.';
await expect(
validatePrTitle('fix: Foobar', {
subjectPattern: '^(?![A-Z]).+$',
subjectPatternError: customError
})
).rejects.toThrow(customError);
});
it('interpolates variables into `subjectPatternError`', async () => {
await expect(
validatePrTitle('fix: Foobar', {
subjectPattern: '^(?![A-Z]).+$',
subjectPatternError:
'The subject "{subject}" found in the pull request title "{title}" cannot start with an uppercase character.'
})
).rejects.toThrow(
'The subject "Foobar" found in the pull request title "fix: Foobar" cannot start with an uppercase character.'
);
});
it('throws for invalid subjects', async () => {
await expect(
validatePrTitle('fix: Foobar', {
subjectPattern: '^(?![A-Z]).+$'
})
).rejects.toThrow(
'The subject "Foobar" found in pull request title "fix: Foobar" doesn\'t match the configured pattern "^(?![A-Z]).+$".'
);
});
it('throws for only partial matches', async () => {
await expect(
validatePrTitle('fix: Foobar', {
subjectPattern: 'Foo'
})
).rejects.toThrow(
'The subject "Foobar" found in pull request title "fix: Foobar" isn\'t an exact match for the configured pattern "Foo". Please provide a subject that matches the whole pattern exactly.'
);
});
it('accepts valid subjects', async () => {
await validatePrTitle('fix: foobar', {
subjectPattern: '^(?![A-Z]).+$'
});
});
});