-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcli.e2e.js
More file actions
490 lines (406 loc) Β· 16.6 KB
/
cli.e2e.js
File metadata and controls
490 lines (406 loc) Β· 16.6 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
const os = require('os')
const fs = require('fs')
const { removeSync } = require('fs-extra')
const path = require('path')
const simpleGit = require('simple-git/promise')
const childProcess = require('child_process')
expect.extend({
includes(str, items) {
const pass = items.every(item => str.includes(item))
return {
pass,
message: () => pass
? `Expected ${str} to not includes ${items.join(', ')}`
: `Expected ${str} to includes ${items.join(', ')}`,
}
},
})
expect.extend({
toDisplayError(str, message = '') {
const pass = str.includes('Error') && str.includes(message)
return {
pass,
message: () => pass ? 'It passes' : `Expected ${str} to includes Error and "${message}"`,
}
},
})
describe('generate changelog', () => {
let testDir
let repo
beforeEach(async () => {
testDir = path.join(os.tmpdir(), 'gitmoji-changelog')
if (fs.existsSync(testDir)) removeSync(testDir)
fs.mkdirSync(testDir)
repo = simpleGit(testDir)
await repo.init()
fs.copyFileSync(
path.join(__dirname, '..', '..', '..', 'misc', 'package.sample.json'),
path.join(testDir, 'package.json'),
)
})
afterEach(() => {
removeSync(testDir)
})
describe('presets', () => {
it('should throw an Error if the preset could not find configuration', () => {
fs.unlinkSync(path.join(testDir, 'package.json'))
const output = gitmojiChangelog()
expect(output.toString('utf8')).includes(['Cannot retrieve configuration'])
})
it('should throw an Error if the preset did not return a version', () => {
const pkg = path.join(testDir, 'package.json')
// eslint-disable-next-line global-require
const content = JSON.parse(fs.readFileSync(pkg).toString('utf8'))
delete content.version
fs.writeFileSync(pkg, JSON.stringify(content))
const output = gitmojiChangelog()
console.log(output.toString('utf8'))
expect(output.toString('utf8')).includes(['Cannot retrieve the version from your configuration'])
})
})
describe('init', () => {
it("should get a 1.0.0 version while initializing changelog by calling cli without arguments and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
expect(getChangelog()).includes(['1.0.0'])
})
it("should get a 1.0.0 version while initializing changelog by calling cli with explicit init argument and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog(['init'])
await commit(':bookmark: Version 1.0.0')
expect(getChangelog()).includes(['1.0.0'])
})
it('should use a custom commit mapping to create commit categories', async () => {
makeCustomConfig({
commitMapping: [
{ group: 'style', label: 'Style', emojis: ['lipstick'] },
{ group: 'changed', label: 'Changed', emojis: [] },
],
})
await makeChanges('file1')
await commit(':sparkles: Add some file')
await makeChanges('file2')
await commit(':lipstick: Add some style file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
expect(getChangelog()).includes(['### Style'])
expect(getChangelog()).not.includes(['### Changed'])
})
it("should get a 1.0.0 version while initializing changelog by calling cli with 1.0.0 and having package.json's version set to 0.0.1", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
gitmojiChangelog('1.0.0')
await commit(':bookmark: Version 1.0.0')
expect(getChangelog()).includes(['1.0.0'])
})
it("should get a next version while initializing changelog by calling cli without arguments and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog()
expect(getChangelog()).includes(['1.0.0', 'next'])
})
it("should get two versions 1.0.0 and 2.0.0 while initializing changelog by calling cli without and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 2.0.0')
expect(getChangelog()).includes(['1.0.0', '2.0.0'])
})
})
describe('update', () => {
it("should get a 1.0.0 version while initializing changelog by calling cli without arguments two times and having package.json's version set to 1.0.0", async () => {
await bumpVersion('1.0.0')
await makeChanges('file1')
await commit(':sparkles: Add some file')
gitmojiChangelog()
await makeChanges('file2')
await commit(':sparkles: Add a second file')
gitmojiChangelog()
expect(getChangelog()).includes(['second file'])
})
it('should use a custom commit mapping to create commit categories', async () => {
makeCustomConfig({
commitMapping: [
{ group: 'style', label: 'Style', emojis: ['lipstick'] },
{ group: 'changed', label: 'Changed', emojis: [] },
],
})
await bumpVersion('1.0.0')
await makeChanges('file1')
await commit(':sparkles: Add some file')
await makeChanges('file3')
await commit(':lipstick: Add some file')
gitmojiChangelog()
await makeChanges('file2')
await commit(':sparkles: Add a second file')
await makeChanges('file4')
await commit(':lipstick: Add some file')
gitmojiChangelog()
expect(getChangelog()).includes(['### Style'])
expect(getChangelog()).not.includes(['### Changed'])
})
it("should get two versions 1.0.0 and next while updating changelog by calling cli without arguments and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog()
expect(getChangelog()).includes(['1.0.0', 'next'])
})
it("should get two versions 1.0.0 and 2.0.0 while updating changelog by calling cli without arguments and having package.json's version set to 2.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
gitmojiChangelog()
expect(getChangelog()).includes(['1.0.0', '2.0.0'])
})
it('should get two versions 1.0.0 and 2.0.0 while updating changelog after tagging a version 2.0.0', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
await tag('2.0.0')
gitmojiChangelog()
expect(getChangelog()).includes(['1.0.0', '2.0.0'])
})
it('should get two versions 1.0.0-alpha.1 and 1.0.0-alpha.2 while updating changelog after tagging a version 1.0.0-alpha.2', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0-alpha.1')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0-alpha.1')
await tag('1.0.0-alpha.1')
await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('1.0.0-alpha.2')
await tag('1.0.0-alpha.2')
gitmojiChangelog()
expect(getChangelog()).includes(['1.0.0-alpha.1', '1.0.0-alpha.2'])
})
it("should get two versions 1.0.0 and 2.0.0 while updating changelog by calling cli with 2.0.0 and having package.json's version set to 1.0.0", async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog('2.0.0')
expect(getChangelog()).includes(['1.0.0', '2.0.0'])
})
it('should get three versions 1.0.0, 2.0.0, 3.0.0 while updating changelog by calling cli without arguments and skipping two tags creation 2.0.0 and 3.0.0', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
await commit(':bookmark: Version 2.0.0')
await tag('2.0.0')
await makeChanges('file3')
await commit(':sparkles: Add a third file')
await bumpVersion('3.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 3.0.0')
await tag('3.0.0')
expect(getChangelog()).includes(['1.0.0', '2.0.0', '3.0.0'])
})
it('should get three versions 1.0.0, 2.0.0, 3.0.0 and next while updating changelog by calling cli without arguments and skipping two tags creation 2.0.0 and 3.0.0', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
await commit(':bookmark: Version 2.0.0')
await tag('2.0.0')
await makeChanges('file3')
await commit(':sparkles: Add a third file')
await bumpVersion('3.0.0')
await commit(':bookmark: Version 3.0.0')
await tag('3.0.0')
await makeChanges('file4')
await commit(':sparkles: Add a fourth file')
gitmojiChangelog()
expect(getChangelog()).includes(['1.0.0', '2.0.0', '3.0.0', 'next'])
})
it('should get two versions 1.0.0, 2.0.0 and next while updating changelog by calling cli without arguments', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
await bumpVersion('2.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 2.0.0')
await tag('2.0.0')
await makeChanges('file4')
await commit(':sparkles: Add a fourth file')
gitmojiChangelog()
expect(getChangelog()).includes(['1.0.0', '2.0.0', 'next'])
})
it('shouldn\'t generate changelog when gimoji-changelog if there isn\'t any changes', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
const output = gitmojiChangelog()
expect(getChangelog()).includes(['1.0.0'])
expect(output.toString('utf8')).toDisplayError('No changes found.')
})
it('should get two versions 1.0.0 and next after two generation while updating changelog by calling cli without arguments', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog()
await makeChanges('file4')
await commit(':sparkles: Add a fourth file')
gitmojiChangelog()
expect(getChangelog()).includes(['1.0.0', 'next'])
})
it('should get two versions 1.0.0 and 1.1.0 after three generations while updating changelog by calling cli with version 1.1.0', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog()
await makeChanges('file4')
await commit(':sparkles: Add a fourth file')
gitmojiChangelog('1.1.0')
expect(getChangelog()).includes(['1.0.0', '1.1.0'])
})
it('should get two versions 1.0.0 and next after three generations while updating changelog by calling cli without arguments', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog('1.1.0')
gitmojiChangelog()
expect(getChangelog()).not.includes(['1.1.0'])
expect(getChangelog()).includes(['1.0.0', 'next'])
})
it('should get two versions 1.0.0 and 1.2.0 after three generations while updating changelog by calling cli without arguments', async () => {
await makeChanges('file1')
await commit(':sparkles: Add some file')
await bumpVersion('1.0.0')
gitmojiChangelog()
await commit(':bookmark: Version 1.0.0')
await tag('1.0.0')
await makeChanges('file2')
await commit(':sparkles: Add another file')
gitmojiChangelog('1.1.0')
gitmojiChangelog('1.2.0')
expect(getChangelog()).not.includes(['1.1.0'])
expect(getChangelog()).includes(['1.0.0', '1.2.0'])
})
it('should work by passing release as argument without package.json or configuration file', async () => {
const pkg = path.join(testDir, 'package.json')
const { version, ...content } = JSON.parse(fs.readFileSync(pkg).toString('utf8'))
fs.writeFileSync(pkg, JSON.stringify(content))
await makeChanges('file1')
await commit(':sparkles: Add some file')
const output = gitmojiChangelog('1.0.0')
console.log(output.toString('utf8'))
expect(getChangelog()).includes(['1.0.0'])
})
it('should display an error if requested version isn\'t semver', async () => {
const output = gitmojiChangelog('awesomeversion')
expect(output.toString('utf8')).toDisplayError()
})
})
async function makeChanges(fileName) {
fs.writeFileSync(path.join(testDir, fileName))
}
async function makeCustomConfig(config) {
fs.writeFileSync(path.join(testDir, '.gitmoji-changelogrc'), JSON.stringify(config, undefined, 2))
}
async function commit(message) {
await repo.add('.')
await repo.commit(message)
}
async function tag(version) {
await repo.addTag(`v${version}`)
}
function gitmojiChangelog(args = []) {
if (!Array.isArray(args)) {
// eslint-disable-next-line no-param-reassign
args = [args]
}
return childProcess.execFileSync('node', [path.join(__dirname, 'index.js'), ...args], { cwd: testDir })
}
function getChangelog() {
return fs.readFileSync(path.join(testDir, 'CHANGELOG.md')).toString('utf8')
}
function bumpVersion(to) {
const pkg = path.join(testDir, 'package.json')
// eslint-disable-next-line global-require
const content = fs.readFileSync(pkg).toString('utf8')
const { version } = JSON.parse(content)
const updatedContent = content.replace(version, to)
fs.writeFileSync(pkg, updatedContent)
}
/*
* This function is useful to print cli ouput when debugging tests
*/
// eslint-disable-next-line no-unused-vars
function logOutput(ouput) {
console.log(ouput.toString('utf8'))
}
})