-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdangerfile-utils.test.js
More file actions
184 lines (146 loc) · 7.11 KB
/
dangerfile-utils.test.js
File metadata and controls
184 lines (146 loc) · 7.11 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
const { describe, it } = require('node:test');
const assert = require('node:assert');
const { getFlavorConfig, extractPRFlavor, 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);
});
});
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', () => {
const flavor1 = extractPRFlavor('Simple title', null);
assert.strictEqual(flavor1, '');
const flavor2 = extractPRFlavor(null, 'simple-branch');
assert.strictEqual(flavor2, '');
const flavor3 = extractPRFlavor(null, null);
assert.strictEqual(flavor3, '');
});
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');
});
});
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');
}
});
});
});
});