forked from nodejs/core-validate-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.js
348 lines (306 loc) · 12.2 KB
/
validator.js
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
'use strict'
const test = require('tap').test
const Validator = require('../')
// Note, these are not necessarily all real commit messages
const str = `commit e7c077c610afa371430180fbd447bfef60ebc5ea
Author: Calvin Metcalf <cmetcalf@appgeo.com>
AuthorDate: Tue Apr 12 15:42:23 2016 -0400
Commit: James M Snell <jasnell@gmail.com>
CommitDate: Wed Apr 20 13:28:35 2016 -0700
stream: make null an invalid chunk to write in object mode
this harmonizes behavior between readable, writable, and transform
streams so that they all handle nulls in object mode the same way by
considering them invalid chunks.
PR-URL: https://github.com/nodejs/node/pull/6170
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
`
const str2 = `commit b6475b9a9d0da0971eec7eb5559dff4d18a0e721
Author: Evan Lucas <evanlucas@me.com>
Date: Tue Mar 29 08:09:37 2016 -0500
Revert "tty: don't read from console stream upon creation"
This reverts commit 461138929498f31bd35bea61aa4375a2f56cceb7.
The offending commit broke certain usages of piping from stdin.
Fixes: https://github.com/nodejs/node/issues/5927
PR-URL: https://github.com/nodejs/node/pull/5947
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Alexis Campailla <orangemocha@nodejs.org>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
`
/* eslint-disable */
const str3 = `commit 75487f0db80e70a3e27fabfe323a33258dfbbea8
Author: Michaël Zasso <targos@protonmail.com>
Date: Fri Apr 15 13:32:36 2016 +0200
module: fix resolution of filename with trailing slash - make this tile too long
A recent optimization of module loading performance [1] forgot to check that
extensions were set in a certain code path.
[1] https://github.com/nodejs/node/pull/5172/commits/ae18bbef48d87d9c641df85369f62cfd5ed8c250
Fixes: https://github.com/nodejs/node/issues/6214
PR-URL: https://github.com/nodejs/node/pull/6215
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>`
/* eslint-enable */
const str4 = `commit 7d3a7ea0d7df9b6f11df723dec370f49f4f87e99
Author: Wyatt Preul <wpreul@gmail.com>
Date: Thu Mar 3 10:10:46 2016 -0600
check memoryUsage properties
The properties on memoryUsage were not checked before,
this commit checks them.
PR-URL: #5546
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>`
const str5 = `commit 7d3a7ea0d7df9b6f11df723dec370f49f4f87e99
Author: Wyatt Preul <wpreul@gmail.com>
Date: Thu Mar 3 10:10:46 2016 -0600
test: check memoryUsage properties
The properties on memoryUsage were not checked before,
this commit checks them.`
/* eslint-disable */
const str6 = {
"sha": "c5545f2c63fe30b0cfcdafab18c26df8286881d0",
"url": "https://api.github.com/repos/nodejs/node/git/commits/c5545f2c63fe30b0cfcdafab18c26df8286881d0",
"html_url": "https://github.com/nodejs/node/commit/c5545f2c63fe30b0cfcdafab18c26df8286881d0",
"author": {
"name": "Anna Henningsen",
"email": "anna@addaleax.net",
"date": "2016-09-13T10:57:49Z"
},
"committer": {
"name": "Anna Henningsen",
"email": "anna@addaleax.net",
"date": "2016-09-19T12:50:57Z"
},
"tree": {
"sha": "b505c0ffa0555730e9f4cdb391d1ebeb48bb2f59",
"url": "https://api.github.com/repos/nodejs/node/git/trees/b505c0ffa0555730e9f4cdb391d1ebeb48bb2f59"
},
"message": "fs: fix handling of `uv_stat_t` fields\n\n`FChown` and `Chown` test that the `uid` and `gid` parameters\nthey receive are unsigned integers, but `Stat()` and `FStat()`\nwould return the corresponding fields of `uv_stat_t` as signed\nintegers. Applications which pass those these values directly\nto `Chown` may fail\n(e.g. for `nobody` on OS X, who has an `uid` of `-2`, see e.g.\nhttps://github.com/nodejs/node-v0.x-archive/issues/5890).\n\nThis patch changes the `Integer::New()` call for `uid` and `gid`\nto `Integer::NewFromUnsigned()`.\n\nAll other fields are kept as they are, for performance, but\nstrictly speaking the respective sizes of those\nfields aren’t specified, either.\n\nRef: https://github.com/npm/npm/issues/13918\nPR-URL: https://github.com/nodejs/node/pull/8515\nReviewed-By: Ben Noordhuis <info@bnoordhuis.nl>\nReviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>\nReviewed-By: James M Snell <jasnell@gmail.com>\n\nundo accidental change to other fields of uv_fs_stat",
"parents": [
{
"sha": "4e76bffc0c7076a5901179e70c7b8a8f9fcd22e4",
"url": "https://api.github.com/repos/nodejs/node/git/commits/4e76bffc0c7076a5901179e70c7b8a8f9fcd22e4",
"html_url": "https://github.com/nodejs/node/commit/4e76bffc0c7076a5901179e70c7b8a8f9fcd22e4"
}
]
}
/* eslint-enable */
const str7 = `commit 7d3a7ea0d7df9b6f11df723dec370f49f4f87e99
Author: Wyatt Preul <wpreul@gmail.com>
Date: Thu Mar 3 10:10:46 2016 -0600
test: check memoryUsage properties.
`
const str8 = `commit 7d3a7ea0d7df9b6f11df723dec370f49f4f87e99
Author: Wyatt Preul <wpreul@gmail.com>
Date: Thu Mar 3 10:10:46 2016 -0600
test: Check memoryUsage properties
`
const str9 = `commit 7d3a7ea0d7df9b6f11df723dec370f49f4f87e99
Author: Wyatt Preul <wpreul@gmail.com>
Date: Thu Mar 3 10:10:46 2016 -0600
test: Check memoryUsage properties.
`
test('Validator - misc', (t) => {
const v = new Validator()
t.throws(() => {
v.disableRule('biscuits')
}, /Invalid rule: "biscuits"/)
v.disableRule('line-length')
t.equal(v.rules.get('line-length').disabled, true, 'disabled')
v.rules.get('line-length').disabled = false
t.end()
})
test('Validator - real commits', (t) => {
t.test('basic', (tt) => {
tt.plan(21)
const v = new Validator()
// run against the output of git show --quiet
// run against the output of github's get commit api request
// run against the output of github's list commits for pr api request
v.lint(str)
v.lint(require('./fixtures/commit'))
v.lint(require('./fixtures/pr'))
v.on('commit', (data) => {
const c = data.commit
tt.equal(c.sha, 'e7c077c610afa371430180fbd447bfef60ebc5ea', 'sha')
tt.deepEqual(c.subsystems, ['stream'], 'subsystems')
tt.equal(c.prUrl, 'https://github.com/nodejs/node/pull/6170', 'pr')
const msgs = data.messages
const failed = msgs.filter((item) => {
return item.level === 'fail'
})
tt.equal(failed.length, 0, 'failed.length')
const warned = msgs.filter((item) => {
return item.level === 'warn'
})
tt.equal(warned.length, 3, 'warned.length')
tt.equal(warned[0].level, 'warn')
tt.equal(warned[0].id, 'title-length')
})
})
t.test('basic revert', (tt) => {
const v = new Validator()
v.lint(str2)
v.on('commit', (data) => {
const c = data.commit.toJSON()
tt.equal(c.sha, 'b6475b9a9d0da0971eec7eb5559dff4d18a0e721', 'sha')
tt.equal(c.date, 'Tue Mar 29 08:09:37 2016 -0500', 'date')
tt.deepEqual(c.subsystems, ['tty'], 'subsystems')
tt.equal(c.prUrl, 'https://github.com/nodejs/node/pull/5947', 'pr')
tt.equal(c.revert, true, 'revert')
const msgs = data.messages
const filtered = msgs.filter((item) => {
return item.level === 'warn'
})
tt.equal(filtered.length, 1, 'messages.length')
tt.equal(filtered[0].level, 'warn')
tt.equal(filtered[0].id, 'title-length')
tt.end()
})
})
t.test('more basic', (tt) => {
const v = new Validator()
v.lint(str3)
v.on('commit', (data) => {
const c = data.commit.toJSON()
tt.equal(c.sha, '75487f0db80e70a3e27fabfe323a33258dfbbea8', 'sha')
tt.equal(c.date, 'Fri Apr 15 13:32:36 2016 +0200', 'date')
tt.deepEqual(c.subsystems, ['module'], 'subsystems')
tt.equal(c.prUrl, 'https://github.com/nodejs/node/pull/6215', 'pr')
tt.equal(c.revert, false, 'revert')
const msgs = data.messages
const filtered = msgs.filter((item) => {
return item.level === 'fail'
})
tt.equal(filtered.length, 2, 'messages.length')
const ids = filtered.map((item) => {
return item.id
})
const exp = ['line-length', 'title-length']
tt.deepEqual(ids.sort(), exp.sort(), 'message ids')
tt.end()
})
})
t.test('invalid pr-url, missing subsystem', (tt) => {
const v = new Validator()
v.lint(str4)
v.on('commit', (data) => {
const c = data.commit.toJSON()
tt.equal(c.sha, '7d3a7ea0d7df9b6f11df723dec370f49f4f87e99', 'sha')
tt.equal(c.date, 'Thu Mar 3 10:10:46 2016 -0600', 'date')
tt.deepEqual(c.subsystems, [], 'subsystems')
tt.equal(c.prUrl, '#5546', 'pr')
tt.equal(c.revert, false, 'revert')
const msgs = data.messages
msgs.sort((a, b) => {
return a.id < b.id
? -1
: a.id > b.id
? 1
: 0
})
const filtered = msgs.filter((item) => {
return item.level === 'fail'
})
tt.equal(filtered.length, 3, 'messages.length')
tt.equal(filtered[0].id, 'line-after-title', 'message id')
tt.equal(filtered[1].id, 'pr-url', 'message id')
tt.equal(filtered[1].string, '#5546', 'message string')
tt.equal(filtered[2].id, 'subsystem', 'message id')
tt.equal(filtered[2].line, 0, 'line')
tt.equal(filtered[2].column, 0, 'column')
tt.end()
})
})
t.test('invalid pr-url, missing subsystem no meta', (tt) => {
const v = new Validator({
'validate-metadata': false
})
v.lint(str5)
v.on('commit', (data) => {
const c = data.commit.toJSON()
tt.equal(c.sha, '7d3a7ea0d7df9b6f11df723dec370f49f4f87e99', 'sha')
tt.equal(c.date, 'Thu Mar 3 10:10:46 2016 -0600', 'date')
tt.deepEqual(c.subsystems, ['test'], 'subsystems')
tt.equal(c.prUrl, null, 'pr')
tt.equal(c.revert, false, 'revert')
const msgs = data.messages
const filtered = msgs.filter((item) => {
return item.level === 'fail'
})
tt.equal(filtered.length, 0, 'messages.length')
tt.end()
})
})
t.test('non empty lines after metadata', (tt) => {
const v = new Validator()
v.lint(str6)
v.on('commit', (data) => {
const c = data.commit.toJSON()
tt.equal(c.sha, 'c5545f2c63fe30b0cfcdafab18c26df8286881d0', 'sha')
tt.equal(c.date, '2016-09-13T10:57:49Z', 'date')
tt.deepEqual(c.subsystems, ['fs'], 'subsystems')
tt.equal(c.prUrl, 'https://github.com/nodejs/node/pull/8515', 'pr')
tt.equal(c.revert, false, 'revert')
const msgs = data.messages
const filtered = msgs.filter((item) => {
return item.level === 'fail'
})
tt.equal(filtered.length, 1, 'messages.length')
const item = filtered[0]
tt.equal(item.id, 'metadata-end', 'id')
tt.equal(item.message, 'commit metadata at end of message', 'message')
tt.equal(item.line, 22, 'line')
tt.equal(item.column, 0, 'column')
tt.end()
})
})
t.test('trailing punctuation in title line', (tt) => {
const v = new Validator({
'validate-metadata': false
})
v.lint(str7)
v.on('commit', (data) => {
const msgs = data.messages
const filtered = msgs.filter((item) => {
return item.level === 'fail'
})
tt.equal(filtered.length, 1, 'messages.length')
tt.equal(filtered[0].message,
'Do not use punctuation at end of title.',
'message')
tt.end()
})
})
t.test('first word is lowercase in title line', (tt) => {
const v = new Validator({
'validate-metadata': false
})
v.lint(str8)
v.on('commit', (data) => {
const msgs = data.messages
const filtered = msgs.filter((item) => {
return item.level === 'fail'
})
tt.equal(filtered.length, 1, 'messages.length')
tt.equal(filtered[0].message,
'First word after subsystem(s) in title should be lowercase.',
'message')
tt.equal(filtered[0].column, 7, 'column')
tt.end()
})
})
t.test('more than one formatting error in title line', (tt) => {
const v = new Validator({
'validate-metadata': false
})
v.lint(str9)
v.on('commit', (data) => {
const msgs = data.messages
const filtered = msgs.filter((item) => {
return item.level === 'fail'
})
tt.equal(filtered.length, 2, 'messages.length')
tt.end()
})
})
t.end()
})