-
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathcli-test.js
More file actions
362 lines (299 loc) · 10.7 KB
/
cli-test.js
File metadata and controls
362 lines (299 loc) · 10.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
import { test } from 'tap'
import { readFileSync } from 'node:fs'
import { spawn } from 'node:child_process'
import subsystems from '../lib/rules/subsystem.js'
test('Test cli flags', (t) => {
t.test('test list-subsystems', (tt) => {
const ls = spawn('./bin/cmd.js', ['--list-subsystems'], {
env: { ...process.env, FORCE_COLOR: 0 }
})
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stderr.on('data', (data) => {
tt.fail('This should not happen')
})
ls.on('close', (code) => {
// Get the list of subsytems as an Array.
// Need to match words that also have the "-" in them
const subsystemsFromOutput = compiledData.match(/[\w'-]+/g)
const defaultSubsystems = subsystems.defaults.subsystems
tt.equal(subsystemsFromOutput.length,
defaultSubsystems.length,
'Should have the same length')
// Loop through the output list and compare with the real list
// to make sure they are all there
const missing = []
subsystemsFromOutput.forEach((sub) => {
if (!defaultSubsystems.find((x) => { return x === sub })) {
missing.push(sub)
}
})
tt.equal(missing.length, 0, 'Should have no missing subsystems')
tt.end()
})
})
t.test('test help output', (tt) => {
const usage = readFileSync('bin/usage.txt', { encoding: 'utf8' })
const ls = spawn('./bin/cmd.js', ['--help'])
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stderr.on('data', (data) => {
tt.fail('This should not happen')
})
ls.on('close', (code) => {
tt.equal(compiledData.trim(),
usage.trim(),
'--help output is as expected')
tt.end()
})
})
t.test('test sha', (tt) => {
const ls = spawn('./bin/cmd.js', ['--no-validate-metadata', '2b98d02b52'])
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stderr.on('data', (data) => {
tt.fail('This should not happen')
})
ls.on('close', (code) => {
tt.match(compiledData.trim(),
/2b98d02b52/,
'output is as expected')
tt.end()
})
})
t.test('test tap output', (tt) => {
// Use a commit from this repository that does not follow the guidelines.
const ls = spawn('./bin/cmd.js', ['--no-validate-metadata', '--tap', '69435db261'])
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stderr.on('data', (data) => {
tt.fail(`Unexpected stderr output ${data.toString()}`)
})
ls.on('close', (code) => {
const output = compiledData.trim()
tt.match(output,
/# 69435db261/,
'TAP output contains the sha of the commit being linted')
tt.match(output,
/not ok \d+ subsystem: Invalid subsystem: "chore" \(chore: update tested node release lines \(#94\)\)/,
'TAP output contains failure for subsystem')
tt.match(output,
/# fail\s+\d+/,
'TAP output contains total failures')
tt.match(output,
/# Please review the commit message guidelines:\s# https:\/\/github.com\/nodejs\/node\/blob\/HEAD\/doc\/contributing\/pull-requests.md#commit-message-guidelines/,
'TAP output contains pointer to commit message guidelines')
tt.equal(code, 1, 'CLI exits with non-zero code on failure')
tt.end()
})
})
t.test('test url', (tt) => {
const ls = spawn('./bin/cmd.js', ['--no-validate-metadata', 'https://api.github.com/repos/nodejs/core-validate-commit/commits/2b98d02b52'])
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stderr.on('data', (data) => {
tt.fail('This should not happen')
})
ls.on('close', (code) => {
tt.match(compiledData.trim(),
/2b98d02b52/,
'output is as expected')
tt.end()
})
})
t.test('test version flag', (tt) => {
const ls = spawn('./bin/cmd.js', ['--version'])
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stderr.on('data', (data) => {
tt.fail('This should not happen')
})
ls.on('close', async (code) => {
const pkgJsonPath = new URL('../package.json', import.meta.url)
const pkgJson = readFileSync(pkgJsonPath, { encoding: 'utf8' })
const { version } = JSON.parse(pkgJson)
tt.equal(compiledData.trim(),
`core-validate-commit v${version}`,
'output is equal')
tt.end()
})
})
t.test('test stdin with valid JSON', (tt) => {
const validCommit = {
id: '2b98d02b52',
message: 'stream: make null an invalid chunk to write in object mode\n\nthis harmonizes behavior between readable, writable, and transform\nstreams so that they all handle nulls in object mode the same way by\nconsidering them invalid chunks.\n\nPR-URL: https://github.com/nodejs/node/pull/6170\nReviewed-By: James M Snell <jasnell@gmail.com>\nReviewed-By: Matteo Collina <matteo.collina@gmail.com>'
}
const input = JSON.stringify([validCommit])
const ls = spawn('./bin/cmd.js', ['-'])
let compiledData = ''
let errorData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stderr.on('data', (data) => {
errorData += data
})
ls.stdin.write(input)
ls.stdin.end()
ls.on('close', (code) => {
tt.equal(code, 0, 'CLI exits with zero code on success')
tt.match(compiledData, /[^0-9a-f]2b98d02b52[^0-9a-f]/, 'output contains commit id')
tt.equal(errorData, '', 'no error output')
tt.end()
})
})
t.test('test stdin with invalid commit (missing subsystem)', (tt) => {
const invalidCommit = {
id: 'def456',
message: 'this is a bad commit message without subsystem\n\nPR-URL: https://github.com/nodejs/node/pull/1234\nReviewed-By: Someone <someone@example.com>'
}
const input = JSON.stringify([invalidCommit])
const ls = spawn('./bin/cmd.js', ['-'])
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stdin.write(input)
ls.stdin.end()
ls.on('close', (code) => {
tt.notEqual(code, 0, 'CLI exits with non-zero code on failure')
tt.match(compiledData, /def456/, 'output contains commit id')
tt.match(compiledData, /title-format/, 'output mentions the rule violation')
tt.end()
})
})
t.test('test stdin with multiple commits', (tt) => {
const commits = [
{
id: 'commit1',
message: 'doc: update README\n\nPR-URL: https://github.com/nodejs/node/pull/1111\nReviewed-By: Someone <someone@example.com>'
},
{
id: 'commit2',
message: 'test: add new test case\n\nPR-URL: https://github.com/nodejs/node/pull/2222\nReviewed-By: Someone <someone@example.com>'
}
]
const input = JSON.stringify(commits)
const ls = spawn('./bin/cmd.js', ['-'])
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stdin.write(input)
ls.stdin.end()
ls.on('close', (code) => {
tt.equal(code, 0, 'CLI exits with zero code on success')
tt.match(compiledData, /commit1/, 'output contains first commit id')
tt.match(compiledData, /commit2/, 'output contains second commit id')
tt.end()
})
})
t.test('test stdin with TAP output', (tt) => {
const validCommit = {
id: '69435db261',
message: 'chore: update tested node release lines (#94)'
}
const input = JSON.stringify([validCommit])
const ls = spawn('./bin/cmd.js', ['--no-validate-metadata', '--tap', '-'])
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stdin.write(input)
ls.stdin.end()
ls.on('close', (code) => {
const output = compiledData.trim()
tt.match(output,
/# 69435db261/,
'TAP output contains the sha of the commit being linted')
tt.match(output,
/not ok \d+ subsystem: Invalid subsystem: "chore" \(chore: update tested node release lines \(#94\)\)/,
'TAP output contains failure for subsystem')
tt.match(output,
/# fail\s+\d+/,
'TAP output contains total failures')
tt.match(output,
/# Please review the commit message guidelines:\s# https:\/\/github.com\/nodejs\/node\/blob\/HEAD\/doc\/contributing\/pull-requests.md#commit-message-guidelines/,
'TAP output contains pointer to commit message guidelines')
tt.equal(code, 1, 'CLI exits with non-zero code on failure')
tt.end()
})
})
t.test('test stdin with invalid JSON', (tt) => {
const input = 'this is not valid JSON'
const ls = spawn('./bin/cmd.js', ['-'])
let errorData = ''
ls.stderr.on('data', (data) => {
errorData += data
})
ls.stdin.write(input)
ls.stdin.end()
ls.on('close', (code) => {
tt.equal(code, 1, 'CLI exits with non-zero code on error')
tt.match(errorData, /Error parsing JSON input/, 'error message is shown')
tt.end()
})
})
t.test('test stdin with non-array JSON', (tt) => {
const input = JSON.stringify({ id: 'test', message: 'test' })
const ls = spawn('./bin/cmd.js', ['-'])
let errorData = ''
ls.stderr.on('data', (data) => {
errorData += data
})
ls.stdin.write(input)
ls.stdin.end()
ls.on('close', (code) => {
tt.equal(code, 1, 'CLI exits with non-zero code on error')
tt.match(errorData, /Input must be an array/, 'error message is shown')
tt.end()
})
})
t.test('test stdin with missing properties', (tt) => {
const input = JSON.stringify([{ id: 'test' }]) // missing 'message'
const ls = spawn('./bin/cmd.js', ['-'])
let errorData = ''
ls.stderr.on('data', (data) => {
errorData += data
})
ls.stdin.write(input)
ls.stdin.end()
ls.on('close', (code) => {
tt.equal(code, 1, 'CLI exits with non-zero code on error')
tt.match(errorData, /must have "id" and "message" properties/, 'error message is shown')
tt.end()
})
})
t.test('test stdin with --no-validate-metadata', (tt) => {
const commit = {
id: 'novalidate',
message: 'doc: update README\n\nThis commit has no PR-URL or reviewers'
}
const input = JSON.stringify([commit])
const ls = spawn('./bin/cmd.js', ['--no-validate-metadata', '-'])
let compiledData = ''
ls.stdout.on('data', (data) => {
compiledData += data
})
ls.stdin.write(input)
ls.stdin.end()
ls.on('close', (code) => {
tt.equal(code, 0, 'CLI exits with zero code when metadata validation is disabled')
tt.match(compiledData, /novalidate/, 'output contains commit id')
tt.end()
})
})
t.end()
})