-
Notifications
You must be signed in to change notification settings - Fork 321
/
Copy pathGren.spec.js
672 lines (568 loc) · 26.7 KB
/
Gren.spec.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
import { assert } from 'chai';
import chalk from 'chalk';
import fs from 'fs';
import Gren from '../lib/src/Gren.js';
import { requireConfig } from '../lib/src/_utils.js';
const TOKEN = process.env.GREN_GITHUB_TOKEN;
if (!TOKEN) {
console.log(chalk.blue('Token not present, skipping Gren tests.'));
// eslint-disable-next-line no-global-assign
describe = describe.skip;
}
describe('Gren', () => {
let gren;
before(() => {
gren = new Gren({
token: TOKEN,
username: 'github-tools',
repo: 'github-release-notes',
quiet: true
});
});
it('Should throw an error', () => {
process.env.GREN_GITHUB_TOKEN = null;
const grenWithoutToken = () => new Gren();
assert.throws(grenWithoutToken, chalk.red('You must provide the TOKEN'), 'No token passed');
});
it('Should generate the options', () => {
assert.isOk(gren.options, 'The options exist');
});
describe('_createReleaseRanges', () => {
const blocks = [
{
date: '2017-09-01T23:00:00.000Z'
},
{
date: '2016-09-01T23:00:00.000Z'
},
{
date: '2017-05-01T23:00:00.000Z'
},
{
date: '2017-10-01T23:00:00.000Z'
}
];
describe('_sortReleasesByDate', () => {
it('Should sort an Array by it\'s date property', () => {
const sortedBlocks = [
{
date: '2017-10-01T23:00:00.000Z'
},
{
date: '2017-09-01T23:00:00.000Z'
},
{
date: '2017-05-01T23:00:00.000Z'
},
{
date: '2016-09-01T23:00:00.000Z'
}
];
assert.deepEqual(gren._sortReleasesByDate(blocks), sortedBlocks, 'Given release blocks');
});
});
it('Should return ranges of Objects', () => {
const rangedBlocks = [
[
{
date: '2017-10-01T23:00:00.000Z'
},
{
date: '2017-09-01T23:00:00.000Z'
}
],
[
{
date: '2017-09-01T23:00:00.000Z'
},
{
date: '2017-05-01T23:00:00.000Z'
}
],
[
{
date: '2017-05-01T23:00:00.000Z'
},
{
date: '2016-09-01T23:00:00.000Z'
}
]
];
assert.deepEqual(gren._createReleaseRanges(blocks), rangedBlocks, 'Given release blocks');
gren.options.tags = 'all';
assert.deepEqual(gren._createReleaseRanges(blocks), rangedBlocks.concat([[
{
date: '2016-09-01T23:00:00.000Z'
},
{
id: 0,
date: new Date('1970-01-01T00:00:00.000Z')
}
]]), 'Given release blocks with all tags option in');
});
});
describe('Issues and PRs', () => {
let PRs, issues;
before(() => {
gren.options.template.issue = '{{name}}';
gren.options.template.group = '{{heading}}';
gren.options.template.noLabel = 'closed';
const issueFile = requireConfig(process.cwd() + '/test/data/issues.json');
PRs = requireConfig(process.cwd() + '/test/data/PRs.json');
issues = {
normal: issueFile.filter(({ id }) => id === 234567890),
noMilestone: issueFile.filter(({ id }) => id === 234567891),
noLabel: issueFile.filter(({ id }) => id === 234567891),
// eslint-disable-next-line camelcase
pullRequests: issueFile.filter(({ pull_request }) => pull_request)
};
});
describe('_getLastPage', () => {
it('Should return the number of the last page', () => {
const link = '<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=2>; rel="next", <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"';
assert.deepEqual(gren._getLastPage(link), 34, 'String of pages');
});
it('Should return false', () => {
assert.isNotOk(gren._getLastPage(), 'Nothing has been passed');
assert.isNotOk(gren._getLastPage('Lorem ipsum'), 'The string does not contain the page information');
assert.isNotOk(gren._getLastPage(false), 'False has been passed');
});
});
describe('_groupBy, _groupByLabel', () => {
it('Should return the just templated issues', () => {
const { normal, noLabel } = issues;
gren.options.groupBy = false;
assert.deepEqual(gren._groupBy(normal), [normal[0].title], 'The groupBy option is false');
assert.deepEqual(gren._groupBy(noLabel), [noLabel[0].title], 'The groupBy option is false');
});
it('Should return the group based on issue labels', () => {
const { normal, noLabel } = issues;
gren.options.groupBy = 'label';
assert.deepEqual(gren._groupBy(normal), [`${normal[0].labels[0].name}\n${normal[0].title}`], 'Group option is "label"');
assert.deepEqual(gren._groupBy(noLabel), [`closed\n${noLabel[0].title}`], 'Group option is "label" with no labels');
});
it('Should not return labels without labels', () => {
const { normal, noLabel } = issues;
gren.options.template.noLabel = false;
gren.options.groupBy = 'label';
assert.lengthOf(gren._groupBy(noLabel), 0, 'When the noLabel is false and only an issue with no labels has been passed');
assert.lengthOf(gren._groupBy(noLabel.concat(normal)), 1, 'When the noLabel is false and only one issue with labels has been passed');
});
it('Should throw an error', () => {
const error = chalk.red('The option for groupBy is invalid, please check the documentation');
gren.options.groupBy = 'an unrecognised string';
assert.throws(gren._groupBy.bind(gren, issues), error, 'Passing an unrecognised string');
gren.options.groupBy = [1, 2, 3];
assert.throws(gren._groupBy.bind(gren, issues), error, 'Passing an Array');
gren.options.groupBy = 123;
assert.throws(gren._groupBy.bind(gren, issues), error, 'Passing a number');
});
it('Should group the issues based on the option', () => {
const { normal, noLabel } = issues;
gren.options.template.noLabel = 'closed';
gren.options.groupBy = {
'Test': ['enhancement'],
'Others': ['closed']
};
assert.deepEqual(gren._groupBy(normal), [`Test\n${normal[0].title}`], 'Passing one heading for one issue');
assert.deepEqual(gren._groupBy(noLabel), [`Others\n${noLabel[0].title}`], 'Group option is "label" with no labels');
gren.options.groupBy = {
'No issues for this one': ['this does not exist']
};
assert.deepEqual(gren._groupBy(normal), [], 'Passing one heading that does not match any labels');
gren.options.groupBy = {
'All': ['...']
};
assert.deepEqual(gren._groupBy(normal), [`All\n${normal[0].title}`], 'The issue does not match any labels, and goes in the ... group');
});
it('Should format group using post formatter', () => {
const { normal, noLabel } = issues;
gren.options.groupBy = {
'Test:': ['enhancement'],
'Others:': ['closed']
};
gren.options.groupPostProcessor = (groupContent) => {
return groupContent.replace(0, groupContent.indexOf(':\n') + 3);
}
assert.deepEqual(gren._groupBy(normal), [`Test:`], 'Passing one heading for one issue');
assert.deepEqual(gren._groupBy(noLabel), [`Others:`], 'Group option is "label" with no labels');
});
});
describe('_filterIssue', () => {
it('Should not return the wrong issue', () => {
const { normal, noMilestone } = issues;
assert.isNotOk(gren._filterIssue(PRs[0]), 'PRs are not included');
gren.options.ignoreIssuesWith = ['enhancement'];
assert.isNotOk(gren._filterIssue(normal[0]), 'Issue contains an ignore label');
gren.options.ignoreIssuesWith = [];
gren.options.onlyMilestones = true;
assert.isNotOk(gren._filterIssue(noMilestone[0]), 'Issue without milestone, with onlyMilestone as true');
gren.options.onlyMilestones = false;
gren.options.dataSource = 'milestones';
assert.isNotOk(gren._filterIssue(noMilestone[0]), 'Issue without milestone, with dataSource as milestone');
gren.options.dataSource = 'prs';
assert.isNotOk(gren._filterIssue(normal[0]), 'Issue are not included, with dataSource as prs');
});
it('Should include pull requests', () => {
gren.options.dataSource = 'prs';
assert.isOk(gren._filterIssue(issues.pullRequests[0]), 'Pull Requests are included, with dataSource as prs');
});
});
describe('_templateBody', () => {
it('Should always return a string', () => {
const body = [
'First',
'Second',
'Third'
];
const rangeBody = 'This is one body';
assert.isString(gren._templateBody(body), 'Passing only the body');
assert.isString(gren._templateBody(false, rangeBody), 'Passing only the rangeBody');
assert.isString(gren._templateBody(), 'No parameters');
assert.isString(gren._templateBody('This is not an Array!'), 'No parameters');
});
});
describe('_lablesAreIgnored', () => {
it('Should return false on unsupported paramater', () => {
assert.isNotOk(gren._lablesAreIgnored([]), 'The variable is empty');
assert.isNotOk(gren._lablesAreIgnored(false), 'The variable is false');
assert.isNotOk(gren._lablesAreIgnored(true), 'The variable is true');
assert.isNotOk(gren._lablesAreIgnored({ a: 1, b: 2 }), 'The variable is an Object');
assert.isNotOk(gren._lablesAreIgnored({}), 'The variable is an empty Object');
assert.isNotOk(gren._lablesAreIgnored('string'), 'The variable is a string');
assert.isNotOk(gren._lablesAreIgnored([1, 2, 3]), 'The variable is a Array of invalid data');
});
it('Should return false if none of the label names are included', () => {
gren.options.ignoreIssuesWith = ['c'];
assert.isNotOk(gren._lablesAreIgnored([{ name: 'a' }, { name: 'b' }]), 'None of the names match');
assert.isNotOk(gren._lablesAreIgnored([{ name: 'ac' }, { name: 'b' }]), 'Part of the name is in a label name');
});
it('Should return true if it finds any label name', () => {
gren.options.ignoreIssuesWith = ['c', 'd'];
assert.isOk(gren._lablesAreIgnored([{ name: 'c' }, { name: 'd' }]), 'All the labels match');
assert.isOk(gren._lablesAreIgnored([{ name: 'c' }, { name: 'e' }]), 'Only one of the labels match');
assert.isOk(gren._lablesAreIgnored([{ name: 'c' }, 1]), 'Only one of the labels match and is valid data');
});
});
});
describe('Tags', () => {
let tags;
before(() => {
tags = requireConfig(process.cwd() + '/test/data/tags.json');
});
describe('_getSelectedTags', () => {
it('Should return all the tags', () => {
gren.options.tags = 'all';
assert.deepEqual(gren._getSelectedTags(tags), tags, 'The tags option is all');
});
it('Should return false', () => {
gren.options.tags = [];
assert.isNotOk(gren._getSelectedTags(tags), 'The tags option is an empty Array');
assert.isNotOk(gren._getSelectedTags(false), 'Passing false');
assert.isNotOk(gren._getSelectedTags([]), 'Passing empty Array');
});
it('Should return an Array of two/one tags', () => {
gren.options.tags = 'all';
assert.isArray(gren._getSelectedTags(tags), 'Passing all the tags');
gren.options.tags = ['0.9.0', '0.8.1'];
assert.lengthOf(gren._getSelectedTags(tags), 2, 'Passing all the tags');
assert.lengthOf(gren._getSelectedTags(tags.slice(0, 1)), 1, 'Passing just one tag');
assert.lengthOf(gren._getSelectedTags(tags.slice(0, 2)), 2, 'Passing two tags');
gren.options.tags = ['0.9.0'];
assert.lengthOf(gren._getSelectedTags(tags), 2, 'Passing all the tags');
assert.lengthOf(gren._getSelectedTags(tags.slice(0, 2)), 2, 'Passing two tags');
assert.lengthOf(gren._getSelectedTags(tags.slice(0, 1)), 1, 'Passing one tags');
});
it('Should filter the tags with the selected ones', () => {
const nine = tags.filter(({ name }) => name === '0.9.0')[0];
const eight = tags.filter(({ name }) => name === '0.8.1')[0];
const seven = tags.filter(({ name }) => name === '0.7.0')[0];
gren.options.tags = ['0.9.0', '0.7.0'];
assert.deepEqual(gren._getSelectedTags(tags), [nine, seven], 'Two tags as options');
gren.options.tags = ['0.9.0'];
assert.deepEqual(gren._getSelectedTags(tags), [nine, eight], 'One tag as options');
gren.options.tags = ['0.9.0', '0.7.0', '0.3.0'];
assert.deepEqual(gren._getSelectedTags(tags), [nine, seven], 'Three tags as options');
});
});
});
describe('_generateCommitsBody, _templateCommits, _filterCommit', () => {
let commitMessages;
before(() => {
gren.options.template.commit = '{{message}} - {{author}}';
commitMessages = [
{
commit: {
author: {
name: 'Alex Canessa'
},
message: 'First commit'
},
author: {
login: 'alexcanessa'
}
},
{
commit: {
author: {
name: 'Alex Canessa'
},
message: 'This is another commit'
},
author: {
login: 'alexcanessa'
}
},
{
commit: {
author: {
name: 'Alex Canessa'
},
message: 'Merge branch into master: Something else here to be tested'
},
author: {
login: 'alexcanessa'
}
},
{
commit: {
author: {
name: 'Alex Canessa'
},
message: 'This is the last one'
},
author: {
login: 'alexcanessa'
}
}
];
});
it('Should always return a string', () => {
assert.isString(gren._generateCommitsBody(commitMessages), 'Passing Array');
assert.isString(gren._generateCommitsBody([]), 'Passing empty Array');
assert.isString(gren._generateCommitsBody(false), 'Passing false');
assert.isString(gren._generateCommitsBody(), 'No parameters');
assert.isString(gren._generateCommitsBody(true), 'Passing true');
});
it('Should not return the last message', () => {
const lastMessage = commitMessages.slice(-1)[0];
assert.notInclude(gren._generateCommitsBody(commitMessages), `${lastMessage.commit.message} - ${lastMessage.author.login}`, 'Generate the messages');
assert.deepEqual(gren._generateCommitsBody([{
commit: {
author: {
name: 'Alex Canessa'
},
message: 'One message'
},
author: {
login: 'alexcanessa'
}
}]), 'One message - alexcanessa', 'One message passed');
assert.deepEqual(gren._generateCommitsBody([{
commit: {
author: {
name: 'Alex Canessa'
},
message: 'One'
},
author: {
login: 'alexcanessa'
}
},
{
commit: {
author: {
name: 'Alex Canessa'
},
message: 'Two'
},
author: {
login: 'alexcanessa'
}
}]), 'One - alexcanessa', 'Two message passed');
assert.deepEqual(gren._generateCommitsBody([{
commit: {
author: {
name: 'Alex Canessa'
},
message: 'One'
},
author: {
login: 'alexcanessa'
}
},
{
commit: {
author: {
name: 'Alex Canessa'
},
message: 'Two'
},
author: {
login: 'alexcanessa'
}
},
{
commit: {
author: {
name: 'Alex Canessa'
},
message: 'Three'
},
author: {
login: 'alexcanessa'
}
}]), 'One - alexcanessa\nTwo - alexcanessa', 'Three message passed');
});
it('Should only return the messages defined in the options', () => {
gren.options.includeMessages = 'commits';
const messages = msg => `${commitMessages[msg].commit.message} - ${commitMessages[msg].author.login}`;
assert.deepEqual(gren._generateCommitsBody(commitMessages), `${messages(0)}\n${messages(1)}`, 'Using commits as includeMessages');
gren.options.includeMessages = 'all';
assert.deepEqual(gren._generateCommitsBody(commitMessages), `${messages(0)}\n${messages(1)}\n${messages(2)}`, 'Using commits as includeMessages');
gren.options.includeMessages = 'merges';
assert.deepEqual(gren._generateCommitsBody(commitMessages), messages(2), 'Using commits as includeMessages');
});
it('Should not return commits with ignored words', () => {
gren.options.ignoreCommitsWith = ['another'];
const messages = msg => `${commitMessages[msg].commit.message} - ${commitMessages[msg].author.login}`;
assert.notInclude(commitMessages.filter(message => gren._filterCommit(message)), messages(1), 'Ignore another');
});
});
describe('_checkChangelogFile', () => {
before(() => {
gren.options.changelogFilename = 'test/.temp/CHANGELOG.md';
});
beforeEach(() => {
if (fs.existsSync(gren.options.changelogFilename)) {
fs.unlinkSync(gren.options.changelogFilename);
}
});
it('Should reject if the file does not exists ', () => {
fs.writeFileSync(gren.options.changelogFilename, 'Test');
const err = chalk.black(chalk.bgYellow('Looks like there is already a changelog, to override it use --override'));
assert.throws(() => gren._checkChangelogFile(), err, 'The function throws the error');
});
it('Should return the filepath if the file exists ', () => {
assert.isOk(gren._checkChangelogFile(), 'The file exists');
});
});
describe('_createChangelog', () => {
before(() => {
gren.options.changelogFilename = 'test/.temp/CHANGELOG.md';
});
it('Should create the changelog file', () => {
const changelogBody = 'This is the body.';
gren._createChangelog(changelogBody);
assert.isOk(fs.existsSync(gren.options.changelogFilename), 'Create the file');
assert.deepEqual(fs.readFileSync(gren.options.changelogFilename, 'utf-8'), gren.options.template.changelogTitle + changelogBody, 'Add the right body to the changelog file');
});
afterEach(() => {
if (fs.existsSync(gren.options.changelogFilename)) {
fs.unlinkSync(gren.options.changelogFilename);
}
});
});
describe('_transformTagsIntoReleaseObjects', () => {
it('Should transform tags into release objects', () => {
const tag = { 'tag': { 'name': 'tagName', 'number': 3 }, 'releaseId': '12315', 'date': '2020-02-20T13:40:16Z' };
const receivedObject = gren._transformTagsIntoReleaseObjects([tag]);
assert.equal(1, receivedObject.length)
assert.equal(tag.date, receivedObject[0].date);
assert.equal(tag.releaseId, receivedObject[0].id);
assert.equal(tag.tag.name, receivedObject[0].name);
});
});
describe('_validateRequiredTagsExists', () => {
it('should failed if one tag is missing', () => {
const existingTagName = 'existing_tag';
const existingTag = { tag: { name: existingTagName } };
const expectedException = chalk.red('\nThe following tag is not found in the repository: some_tag. please provide existing tags.');
assert.throw(() => gren._validateRequiredTagsExists([existingTag], ['some_tag', 'existing_tag']), expectedException);
});
it('should failed if the two input tags are missing', () => {
const expectedException = chalk.red('\nThe following tags are not found in the repository: some_tag,some_other_tag. please provide existing tags.');
assert.throw(() => gren._validateRequiredTagsExists([], ['some_tag', 'some_other_tag']), expectedException);
});
it('Should do nothing if requireTags=all', () => {
assert.doesNotThrow(() => gren._validateRequiredTagsExists([], 'all'));
});
});
describe('Tests that require network', () => {
before(function(done) {
gren._hasNetwork()
.then(isOnline => {
if (!isOnline) {
process.stdout.write(chalk.red('\nYou need network connectivity to run the following tests:\n'));
this.skip();
}
done();
})
.catch(err => done(err));
});
it('_listReleases', done => {
gren._listReleases({
per_page: 10
})
.then(({ data: releases }) => {
assert.lengthOf(releases, 10, 'The list of releases is the set one.');
done();
})
.catch(err => done(err));
});
it('_listTags', done => {
gren._listTags({
per_page: 10
})
.then(({ data: tags }) => {
assert.lengthOf(tags, 10, 'The list of tags is the set one.');
done();
})
.catch(err => done(err));
});
describe('_getLastTags', () => {
describe('with tags=all', () => {
describe('with ignoreTagsWith', () => {
it('should ignore the specific tag', done => {
gren.options.ignoreTagsWith = ['16'];
gren.options.tags = ['all'];
gren._getLastTags()
.then(tags => {
assert.notInclude(tags.map(({ name }) => name), '0.16.0', 'The ignored tag is not present');
done();
})
.catch(err => done(err));
});
});
});
});
describe('_getReleaseBlocks', () => {
it('more than one tag', done => {
gren.options.tags = ['0.17.2', '0.17.1'];
gren.options.dataSource = "commits";
gren._getReleaseBlocks()
.then(releaseBlocks => {
assert.isArray(releaseBlocks, 'The releaseBlocks is an Array');
releaseBlocks.forEach(block => {
assert.hasAllKeys(block, ['id', 'release', 'name', 'published_at', 'body']);
});
done();
})
.catch(err => done(err));
}).timeout(10000);
it('just one tag', done => {
gren.options.tags = ['0.17.2'];
gren.options.dataSource = "commits";
gren._getReleaseBlocks()
.then(releaseBlocks => {
assert.isArray(releaseBlocks, 'The releaseBlocks is an Array');
releaseBlocks.forEach(block => {
assert.hasAllKeys(block, ['id', 'release', 'name', 'published_at', 'body']);
});
done();
})
.catch(err => done(err));
}).timeout(10000);
});
});
});