-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdangerfile-utils.test.js
More file actions
524 lines (396 loc) · 18.7 KB
/
dangerfile-utils.test.js
File metadata and controls
524 lines (396 loc) · 18.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
const { describe, it } = require('node:test');
const assert = require('node:assert');
const { getFlavorConfig, extractPRFlavor, extractLegalBoilerplateSection, FLAVOR_CONFIG } = require('./dangerfile-utils.js');
describe('dangerfile-utils', () => {
describe('getFlavorConfig', () => {
it('should return config for features with isFeature true', () => {
const featConfig = getFlavorConfig('feat');
assert.strictEqual(featConfig.changelog, 'Features');
assert.strictEqual(featConfig.isFeature, true);
const featureConfig = getFlavorConfig('feature');
assert.strictEqual(featureConfig.changelog, 'Features');
assert.strictEqual(featureConfig.isFeature, true);
});
it('should return config for fixes without isFeature', () => {
const fixConfig = getFlavorConfig('fix');
assert.strictEqual(fixConfig.changelog, 'Fixes');
assert.strictEqual(fixConfig.isFeature, undefined);
const bugConfig = getFlavorConfig('bug');
assert.strictEqual(bugConfig.changelog, 'Fixes');
assert.strictEqual(bugConfig.isFeature, undefined);
const bugfixConfig = getFlavorConfig('bugfix');
assert.strictEqual(bugfixConfig.changelog, 'Fixes');
assert.strictEqual(bugfixConfig.isFeature, undefined);
});
it('should return config with undefined changelog for skipped flavors', () => {
const skipFlavors = ['docs', 'doc', 'ci', 'tests', 'test', 'style', 'refactor', 'build', 'chore', 'meta', 'deps', 'dep', 'chore(deps)', 'build(deps)'];
skipFlavors.forEach(flavor => {
const config = getFlavorConfig(flavor);
assert.strictEqual(config.changelog, undefined, `${flavor} should have undefined changelog`);
assert.strictEqual(config.isFeature, undefined, `${flavor} should have undefined isFeature`);
});
});
it('should return default config for unknown flavors', () => {
const unknownConfig = getFlavorConfig('unknown');
assert.strictEqual(unknownConfig.changelog, 'Features');
assert.strictEqual(unknownConfig.isFeature, undefined);
const emptyConfig = getFlavorConfig('');
assert.strictEqual(emptyConfig.changelog, 'Features');
assert.strictEqual(emptyConfig.isFeature, undefined);
});
it('should be case-insensitive and handle whitespace', () => {
const config1 = getFlavorConfig('FEAT');
assert.strictEqual(config1.changelog, 'Features');
const config2 = getFlavorConfig(' fix ');
assert.strictEqual(config2.changelog, 'Fixes');
});
it('should handle all security-related flavors', () => {
const secConfig = getFlavorConfig('sec');
assert.strictEqual(secConfig.changelog, 'Security');
const securityConfig = getFlavorConfig('security');
assert.strictEqual(securityConfig.changelog, 'Security');
});
it('should handle all performance-related flavors', () => {
const perfConfig = getFlavorConfig('perf');
assert.strictEqual(perfConfig.changelog, 'Performance');
const performanceConfig = getFlavorConfig('performance');
assert.strictEqual(performanceConfig.changelog, 'Performance');
});
it('should handle ref flavor (internal changes - no changelog)', () => {
const refConfig = getFlavorConfig('ref');
assert.strictEqual(refConfig.changelog, undefined);
assert.strictEqual(refConfig.isFeature, undefined);
});
it('should handle scoped flavors by stripping scope', () => {
const scopedFeat = getFlavorConfig('feat(core)');
assert.strictEqual(scopedFeat.changelog, 'Features');
assert.strictEqual(scopedFeat.isFeature, true);
const scopedFix = getFlavorConfig('fix(browser)');
assert.strictEqual(scopedFix.changelog, 'Fixes');
assert.strictEqual(scopedFix.isFeature, undefined);
const scopedChore = getFlavorConfig('chore(deps)');
assert.strictEqual(scopedChore.changelog, undefined);
// Test edge cases for scope stripping
const nestedParens = getFlavorConfig('feat(scope(nested))');
assert.strictEqual(nestedParens.changelog, 'Features'); // Should strip at first (
const noCloseParen = getFlavorConfig('feat(scope');
assert.strictEqual(noCloseParen.changelog, 'Features'); // Should still work
const multipleParens = getFlavorConfig('feat(scope1)(scope2)');
assert.strictEqual(multipleParens.changelog, 'Features'); // Should strip at first (
});
it('should handle non-conventional action words', () => {
// Feature-related words
const addConfig = getFlavorConfig('add');
assert.strictEqual(addConfig.changelog, 'Features');
assert.strictEqual(addConfig.isFeature, true);
const implementConfig = getFlavorConfig('implement');
assert.strictEqual(implementConfig.changelog, 'Features');
assert.strictEqual(implementConfig.isFeature, true);
// Fix-related words
const resolveConfig = getFlavorConfig('resolve');
assert.strictEqual(resolveConfig.changelog, 'Fixes');
const correctConfig = getFlavorConfig('correct');
assert.strictEqual(correctConfig.changelog, 'Fixes');
// Internal change words
const updateConfig = getFlavorConfig('update');
assert.strictEqual(updateConfig.changelog, undefined);
const bumpConfig = getFlavorConfig('bump');
assert.strictEqual(bumpConfig.changelog, undefined);
const cleanupConfig = getFlavorConfig('cleanup');
assert.strictEqual(cleanupConfig.changelog, undefined);
const formatConfig = getFlavorConfig('format');
assert.strictEqual(formatConfig.changelog, undefined);
});
});
describe('extractPRFlavor', () => {
it('should extract flavor from PR title with colon', () => {
const flavor = extractPRFlavor('feat: add new feature', null);
assert.strictEqual(flavor, 'feat');
const flavor2 = extractPRFlavor('Fix: resolve bug in authentication', null);
assert.strictEqual(flavor2, 'fix');
const flavor3 = extractPRFlavor('Docs: Update readme', null);
assert.strictEqual(flavor3, 'docs');
});
it('should extract flavor from branch name with slash', () => {
const flavor = extractPRFlavor(null, 'feature/new-api');
assert.strictEqual(flavor, 'feature');
const flavor2 = extractPRFlavor(null, 'ci/update-workflows');
assert.strictEqual(flavor2, 'ci');
const flavor3 = extractPRFlavor(null, 'fix/auth-bug');
assert.strictEqual(flavor3, 'fix');
});
it('should prefer title over branch if both available', () => {
const flavor = extractPRFlavor('feat: add feature', 'ci/update-workflows');
assert.strictEqual(flavor, 'feat');
});
it('should return empty string if no flavor found', () => {
// Empty or whitespace-only strings
const flavor1 = extractPRFlavor('', null);
assert.strictEqual(flavor1, '');
const flavor2 = extractPRFlavor(' ', null);
assert.strictEqual(flavor2, '');
// No branch with slash
const flavor3 = extractPRFlavor(null, 'simple-branch');
assert.strictEqual(flavor3, '');
// All null/undefined
const flavor4 = extractPRFlavor(null, null);
assert.strictEqual(flavor4, '');
});
it('should handle edge cases', () => {
const flavor1 = extractPRFlavor(':', null);
assert.strictEqual(flavor1, '');
const flavor2 = extractPRFlavor(null, '/');
assert.strictEqual(flavor2, '');
const flavor3 = extractPRFlavor('title: with: multiple: colons', null);
assert.strictEqual(flavor3, 'title');
});
it('should validate input parameters and handle non-string types', () => {
// Number inputs
const flavor1 = extractPRFlavor(123, 456);
assert.strictEqual(flavor1, '');
// Object inputs
const flavor2 = extractPRFlavor({ test: 'object' }, ['array']);
assert.strictEqual(flavor2, '');
// Boolean inputs
const flavor3 = extractPRFlavor(true, false);
assert.strictEqual(flavor3, '');
// Mixed valid/invalid inputs
const flavor4 = extractPRFlavor(null, 'valid/branch');
assert.strictEqual(flavor4, 'valid');
const flavor5 = extractPRFlavor('valid: title', 42);
assert.strictEqual(flavor5, 'valid');
});
it('should extract first word from non-conventional PR titles', () => {
// Non-conventional titles starting with action words
const flavor1 = extractPRFlavor('Fix memory leak in authentication', null);
assert.strictEqual(flavor1, 'fix');
const flavor2 = extractPRFlavor('Add support for new API endpoint', null);
assert.strictEqual(flavor2, 'add');
const flavor3 = extractPRFlavor('Update dependencies to latest versions', null);
assert.strictEqual(flavor3, 'update');
const flavor4 = extractPRFlavor('Remove deprecated configuration options', null);
assert.strictEqual(flavor4, 'remove');
const flavor5 = extractPRFlavor('Bump version to 2.0.0', null);
assert.strictEqual(flavor5, 'bump');
// Should still prefer conventional format over first word
const flavor6 = extractPRFlavor('chore: Update dependencies to latest versions', null);
assert.strictEqual(flavor6, 'chore');
// Handle extra whitespace
const flavor7 = extractPRFlavor(' Fix memory leak ', null);
assert.strictEqual(flavor7, 'fix');
});
});
describe('FLAVOR_CONFIG integrity', () => {
it('should have unique labels across all configs', () => {
const allLabels = [];
FLAVOR_CONFIG.forEach(config => {
config.labels.forEach(label => {
assert.ok(!allLabels.includes(label), `Duplicate label found: ${label}`);
allLabels.push(label);
});
});
});
it('should have proper structure for all configs', () => {
FLAVOR_CONFIG.forEach((config, index) => {
assert.ok(Array.isArray(config.labels), `Config ${index} should have labels array`);
assert.ok(config.labels.length > 0, `Config ${index} should have at least one label`);
assert.ok(config.hasOwnProperty('changelog'), `Config ${index} should have changelog property`);
// changelog should be either a string or undefined
if (config.changelog !== undefined) {
assert.strictEqual(typeof config.changelog, 'string', `Config ${index} changelog should be string or undefined`);
}
// isFeature should be true or undefined (not false)
if (config.hasOwnProperty('isFeature')) {
assert.strictEqual(config.isFeature, true, `Config ${index} isFeature should be true or undefined`);
}
});
});
it('should have only Features configs with isFeature true', () => {
FLAVOR_CONFIG.forEach(config => {
if (config.isFeature === true) {
assert.strictEqual(config.changelog, 'Features', 'Only Features configs should have isFeature true');
}
});
});
});
describe('extractLegalBoilerplateSection', () => {
it('should extract legal boilerplate section with ### header', () => {
const template = `
# Pull Request Template
## Description
Please describe your changes
### Legal Boilerplate
Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.
## Checklist
- [ ] Tests added
`;
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes('### Legal Boilerplate'), 'Should include the header');
assert.ok(result.includes('Functional Software, Inc.'), 'Should include the legal text');
assert.ok(!result.includes('## Checklist'), 'Should not include the next section');
});
it('should extract legal boilerplate section with ## header', () => {
const template = `
# Pull Request Template
## Legal Boilerplate
This is a legal notice.
## Other Section
More content
`;
const result = extractLegalBoilerplateSection(template);
assert.strictEqual(result.trim(), '## Legal Boilerplate\n\nThis is a legal notice.');
});
it('should extract legal boilerplate section with different heading levels', () => {
const testCases = [
{ header: '# Legal Boilerplate', text: 'Level 1 header' },
{ header: '## Legal Boilerplate', text: 'Level 2 header' },
{ header: '### Legal Boilerplate', text: 'Level 3 header' },
{ header: '#### Legal Boilerplate', text: 'Level 4 header' },
{ header: '##### Legal Boilerplate', text: 'Level 5 header' },
{ header: '###### Legal Boilerplate', text: 'Level 6 header' }
];
testCases.forEach(({ header, text }) => {
const template = `${header}\n${text}\n## Next Section`;
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes(header), `Should extract section with ${header}`);
assert.ok(result.includes(text), `Should include text for ${header}`);
assert.ok(!result.includes('Next Section'), `Should not include next section for ${header}`);
});
});
it('should handle case-insensitive matching', () => {
const templates = [
'### Legal Boilerplate\nContent',
'### legal boilerplate\nContent',
'### LEGAL BOILERPLATE\nContent',
'### Legal BOILERPLATE\nContent'
];
templates.forEach(template => {
const result = extractLegalBoilerplateSection(template);
assert.ok(result.length > 0, `Should extract from: ${template.split('\n')[0]}`);
assert.ok(result.includes('Content'), `Should include content from: ${template.split('\n')[0]}`);
});
});
it('should handle legal boilerplate with multiple paragraphs', () => {
const template = `
### Legal Boilerplate
First paragraph of legal text.
Second paragraph of legal text.
Third paragraph of legal text.
## Next Section
`;
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes('First paragraph'), 'Should include first paragraph');
assert.ok(result.includes('Second paragraph'), 'Should include second paragraph');
assert.ok(result.includes('Third paragraph'), 'Should include third paragraph');
assert.ok(!result.includes('Next Section'), 'Should not include next section');
});
it('should handle legal boilerplate at end of template', () => {
const template = `
# PR Template
## Description
Content
### Legal Boilerplate
Legal text at the end.
`;
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes('### Legal Boilerplate'), 'Should include header');
assert.ok(result.includes('Legal text at the end.'), 'Should include text');
});
it('should return empty string when no legal boilerplate section exists', () => {
const template = `
# Pull Request Template
## Description
Please describe your changes
## Checklist
- [ ] Tests added
`;
const result = extractLegalBoilerplateSection(template);
assert.strictEqual(result, '', 'Should return empty string when no legal section found');
});
it('should handle empty template', () => {
const result = extractLegalBoilerplateSection('');
assert.strictEqual(result, '', 'Should return empty string for empty template');
});
it('should handle template with only legal boilerplate section', () => {
const template = '### Legal Boilerplate\nThis is the only content.';
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes('### Legal Boilerplate'), 'Should include header');
assert.ok(result.includes('This is the only content.'), 'Should include content');
});
it('should handle legal boilerplate with special characters', () => {
const template = `
### Legal Boilerplate
Text with special chars: @#$%^&*()_+-={}[]|\\:";'<>?,./
And some unicode: 你好世界 🎉
## Next
`;
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes('special chars'), 'Should handle special characters');
assert.ok(result.includes('你好世界'), 'Should handle unicode');
assert.ok(result.includes('🎉'), 'Should handle emoji');
});
it('should handle legal boilerplate with code blocks', () => {
const template = `
### Legal Boilerplate
Some text with code:
\`\`\`javascript
const legal = true;
\`\`\`
More text.
## Next Section
`;
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes('const legal = true;'), 'Should include code blocks');
assert.ok(result.includes('More text.'), 'Should include text after code block');
assert.ok(!result.includes('Next Section'), 'Should not include next section');
});
it('should handle legal boilerplate with lists', () => {
const template = `
### Legal Boilerplate
You agree to:
- Item 1
- Item 2
- Item 3
## Other
`;
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes('- Item 1'), 'Should include list items');
assert.ok(result.includes('- Item 2'), 'Should include list items');
assert.ok(result.includes('- Item 3'), 'Should include list items');
});
it('should handle legal boilerplate with extra whitespace', () => {
const template = `
### Legal Boilerplate
Content with spaces.
## Next
`;
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes('Content with spaces.'), 'Should handle extra whitespace in header');
});
it('should stop at first subsequent header', () => {
const template = `
### Legal Boilerplate
First section content.
### Another Legal Boilerplate
This should not be included.
`;
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes('First section content.'), 'Should include first section');
assert.ok(!result.includes('This should not be included.'), 'Should stop at next header');
});
it('should handle blank lines within legal section', () => {
const template = `
### Legal Boilerplate
First paragraph.
Second paragraph with blank lines above.
## Next
`;
const result = extractLegalBoilerplateSection(template);
assert.ok(result.includes('First paragraph.'), 'Should include first paragraph');
assert.ok(result.includes('Second paragraph'), 'Should include second paragraph');
// Should preserve blank lines
const blankLineCount = (result.match(/\n\n/g) || []).length;
assert.ok(blankLineCount >= 1, 'Should preserve blank lines');
});
});
});