-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathcheckCommitMessages.test.ts
More file actions
511 lines (375 loc) · 18 KB
/
checkCommitMessages.test.ts
File metadata and controls
511 lines (375 loc) · 18 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { exec } from '../../src/proxy/processors/push-action/checkCommitMessages';
import { Action } from '../../src/proxy/actions';
import * as configModule from '../../src/config';
import { CommitData } from '../../src/proxy/processors/types';
vi.mock('../../src/config', async (importOriginal) => {
const actual: any = await importOriginal();
return {
...actual,
getCommitConfig: vi.fn(() => ({})),
};
});
describe('checkCommitMessages', () => {
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
let mockCommitConfig: any;
beforeEach(() => {
// spy on console.log to verify calls
consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
// default mock config
mockCommitConfig = {
message: {
block: {
literals: ['password', 'secret', 'token'],
patterns: ['http://.*', 'https://.*'],
},
},
};
vi.mocked(configModule.getCommitConfig).mockReturnValue(mockCommitConfig);
});
afterEach(() => {
vi.clearAllMocks();
});
describe('isMessageAllowed', () => {
describe('Empty or invalid messages', () => {
it('should block empty string commit messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: '' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
expect(result.steps[0].logs.join('\n')).toContain('No commit message included...');
});
it('should block null commit messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: null as any } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should block undefined commit messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: undefined as any } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should block non-string commit messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 123 as any } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
expect(JSON.stringify(result.steps[0])).toContain(
'A non-string value has been captured for the commit message...',
);
});
it('should block object commit messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: { text: 'fix: bug' } as any } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should block array commit messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: ['fix: bug'] as any } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
});
describe('Blocked literals', () => {
it('should block messages containing blocked literals (exact case)', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Add password to config' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
expect(JSON.stringify(result.steps[0])).toContain(
'Commit message is blocked via configured literals/patterns...',
);
});
it('should block messages containing blocked literals (case insensitive)', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [
{ message: 'Add PASSWORD to config' } as CommitData,
{ message: 'Store Secret key' } as CommitData,
{ message: 'Update TOKEN value' } as CommitData,
];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should block messages with literals in the middle of words', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Update mypassword123' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should block when multiple literals are present', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Add password and secret token' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
});
describe('Blocked patterns', () => {
it('should block messages containing http URLs', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'See http://example.com for details' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should block messages containing https URLs', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Update docs at https://docs.example.com' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should block messages with multiple URLs', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [
{ message: 'See http://example.com and https://other.com' } as CommitData,
];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should handle custom regex patterns', async () => {
mockCommitConfig.message.block.patterns = ['\\d{3}-\\d{2}-\\d{4}'];
vi.mocked(configModule.getCommitConfig).mockReturnValue(mockCommitConfig);
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'SSN: 123-45-6789' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should match patterns case-insensitively', async () => {
mockCommitConfig.message.block.patterns = ['PRIVATE'];
vi.mocked(configModule.getCommitConfig).mockReturnValue(mockCommitConfig);
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'This is private information' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
});
describe('Combined blocking (literals and patterns)', () => {
it('should block when both literals and patterns match', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'password at http://example.com' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should block when only literals match', async () => {
mockCommitConfig.message.block.patterns = [];
vi.mocked(configModule.getCommitConfig).mockReturnValue(mockCommitConfig);
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Add secret key' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should block when only patterns match', async () => {
mockCommitConfig.message.block.literals = [];
vi.mocked(configModule.getCommitConfig).mockReturnValue(mockCommitConfig);
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Visit http://example.com' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
});
describe('Allowed messages', () => {
it('should allow valid commit messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'fix: resolve bug in user authentication' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(false);
expect(JSON.stringify(result.steps[0])).toContain(
'The following commit messages are legal:',
);
});
it('should allow messages with no blocked content', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [
{ message: 'feat: add new feature' } as CommitData,
{ message: 'chore: update dependencies' } as CommitData,
{ message: 'docs: improve documentation' } as CommitData,
];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(false);
});
it('should allow messages when config has empty block lists', async () => {
mockCommitConfig.message.block.literals = [];
mockCommitConfig.message.block.patterns = [];
vi.mocked(configModule.getCommitConfig).mockReturnValue(mockCommitConfig);
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Any message should pass' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(false);
});
});
describe('Multiple commits', () => {
it('should handle multiple valid commits', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [
{ message: 'feat: add feature A' } as CommitData,
{ message: 'fix: resolve issue B' } as CommitData,
{ message: 'chore: update config C' } as CommitData,
];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(false);
});
it('should block when any commit is invalid', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [
{ message: 'feat: add feature A' } as CommitData,
{ message: 'fix: add password to config' } as CommitData,
{ message: 'chore: update config C' } as CommitData,
];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should block when multiple commits are invalid', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [
{ message: 'Add password' } as CommitData,
{ message: 'Store secret' } as CommitData,
{ message: 'feat: valid message' } as CommitData,
];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should deduplicate commit messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [
{ message: 'fix: bug' } as CommitData,
{ message: 'fix: bug' } as CommitData,
];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(false);
});
it('should handle mix of duplicate valid and invalid messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [
{ message: 'fix: bug' } as CommitData,
{ message: 'Add password' } as CommitData,
{ message: 'fix: bug' } as CommitData,
];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
});
describe('Error handling and logging', () => {
it('should set error flag on step when messages are illegal', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Add password' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should log error message to step', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Add password' } as CommitData];
const result = await exec({}, action);
const step = result.steps[0];
// first log is the "push blocked" message
expect(step.logs[1]).toContain(
'The following commit messages are illegal: ["Add password"]',
);
});
it('should set detailed error message', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Add secret' } as CommitData];
const result = await exec({}, action);
const step = result.steps[0];
expect(step.errorMessage).toContain('Your push has been blocked');
expect(step.errorMessage).toContain('Add secret');
});
it('should include all illegal messages in error', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [
{ message: 'Add password' } as CommitData,
{ message: 'Store token' } as CommitData,
];
const result = await exec({}, action);
const step = result.steps[0];
expect(step.errorMessage).toContain('Add password');
expect(step.errorMessage).toContain('Store token');
});
});
describe('Edge cases', () => {
it('should handle action with no commitData', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = undefined;
const result = await exec({}, action);
// should handle gracefully
expect(result.steps).toHaveLength(1);
});
it('should handle action with empty commitData array', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(false);
});
it('should handle whitespace-only messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: ' ' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(false);
});
it('should handle very long commit messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
const longMessage = 'fix: ' + 'a'.repeat(10000);
action.commitData = [{ message: longMessage } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(false);
});
it('should handle special regex characters in literals', async () => {
mockCommitConfig.message.block.literals = ['$pecial', 'char*'];
vi.mocked(configModule.getCommitConfig).mockReturnValue(mockCommitConfig);
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Contains $pecial characters' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(true);
});
it('should handle unicode characters in messages', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'feat: 添加新功能 🎉' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].error).toBe(false);
});
it('should handle malformed regex patterns gracefully', async () => {
mockCommitConfig.message.block.patterns = ['[invalid'];
vi.mocked(configModule.getCommitConfig).mockReturnValue(mockCommitConfig);
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'Any message' } as CommitData];
// test that it doesn't crash
expect(() => exec({}, action)).not.toThrow();
});
});
describe('Function properties', () => {
it('should have displayName property', () => {
expect(exec.displayName).toBe('checkCommitMessages.exec');
});
});
describe('Step management', () => {
it('should create a step named "checkCommitMessages"', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'fix: bug' } as CommitData];
const result = await exec({}, action);
expect(result.steps[0].stepName).toBe('checkCommitMessages');
});
it('should add step to action', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'fix: bug' } as CommitData];
const initialStepCount = action.steps.length;
const result = await exec({}, action);
expect(result.steps.length).toBe(initialStepCount + 1);
});
it('should return the same action object', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'fix: bug' } as CommitData];
const result = await exec({}, action);
expect(result).toBe(action);
});
});
describe('Request parameter', () => {
it('should accept request parameter without using it', async () => {
const action = new Action('test', 'test', 'test', 1, 'test');
action.commitData = [{ message: 'fix: bug' } as CommitData];
const mockRequest = { headers: {}, body: {} };
const result = await exec(mockRequest, action);
expect(result.steps[0].error).toBe(false);
});
});
});
});