forked from ygrishajev/conventional-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
385 lines (325 loc) · 11.9 KB
/
test.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
import test from 'bron';
import { strict as assert } from 'assert';
import fs from 'fs';
import path from 'path';
import { EOL } from 'os';
import sh from 'shelljs';
import tmp from 'tmp';
import semver from 'semver';
import runTasks from 'release-it';
sh.config.silent = true;
try {
fs.unlinkSync('CHANGES.md');
} catch (error) {}
const noop = () => {};
const log = {
log: noop,
error: noop,
verbose: noop,
info: noop,
obtrusive: noop,
exec: noop,
warn: noop,
preview: noop
};
const namespace = 'conventional-changelog';
const { pathname } = new URL('./index.js', import.meta.url);
const preset = { name: 'angular' };
const getOptions = (options, git = { commit: false, tag: false }) => [
{
ci: true,
git: { commit: git.commit, tag: git.tag, push: false, requireUpstream: false },
plugins: { [pathname]: [namespace, options] }
},
{ log }
];
const mkTmpDir = () => {
const dir = tmp.dirSync({ prefix: namespace });
return dir.name;
};
const add = (type, file, opts = { breaking: false }) => {
sh.ShellString(file).toEnd(file);
sh.exec(`git add ${file}`);
sh.exec(`git commit -m "${type}(${file})${opts.breaking ? '!' : ''}: ${type} ${file}"`);
};
const setup = () => {
const dir = mkTmpDir();
sh.pushd(dir);
sh.exec(`git init .`);
add('fix', 'foo');
return { dir };
};
const date = /\([0-9]{4}-[0-9]{2}-[0-9]{2}\)/.source;
const sha = /[0-9a-f]{7}/.source;
const level = (from, to) => `${/patch/.test(semver.diff(from, to)) ? '##' : '#'}`;
const header = (from, to, suffix = '') =>
`${level(from, to)} \\[${to}\\]\\(/compare/${from}${suffix}...${to}${suffix}\\) ${date}`;
const features = EOL + EOL + EOL + '### Features' + EOL;
const fixes = EOL + EOL + EOL + '### Bug Fixes' + EOL;
const commit = (type, name) => EOL + `\\* \\*\\*${name}:\\*\\* ${type} ${name} ${sha}`;
const nl = value => value.split(/\r\n|\r|\n/g).join(EOL);
test('should generate changelog using recommended bump (minor)', async () => {
setup();
sh.exec(`git tag 1.0.0`);
add('fix', 'bar');
add('feat', 'baz');
const options = getOptions({ preset });
const { changelog } = await runTasks(...options);
const title = header('1.0.0', '1.1.0');
const bar = commit('fix', 'bar');
const baz = commit('feat', 'baz');
assert.match(nl(changelog), new RegExp('^' + title + fixes + bar + features + baz + '$'));
});
test('should generate changelog using recommended bump (patch)', async () => {
setup();
sh.exec(`git tag 1.0.0`);
add('fix', 'bar');
add('fix', 'baz');
const options = getOptions({ preset });
const { changelog } = await runTasks(...options);
const title = header('1.0.0', '1.0.1');
const bar = commit('fix', 'bar');
const baz = commit('fix', 'baz');
assert.match(nl(changelog), new RegExp('^' + title + fixes + bar + baz + '$'));
});
test('should support tag suffix', async () => {
setup();
const latestVersion = '2.0.0';
const suffix = '-next';
const latestTag = latestVersion + suffix;
sh.exec(`git tag ${latestTag}`);
add('fix', 'bar');
const [config, container] = getOptions({ preset });
config.git.tagName = `\${version}${suffix}`;
const { changelog, version } = await runTasks(config, container);
assert.match(
nl(changelog),
// release-it supports tag suffix/template, but conventional-changelog does not so the title will not contain it:
/^## \[2\.0\.1\]\(\/compare\/2\.0\.0-next\.\.\.2\.0\.1-next\) \([0-9]{4}-[0-9]{2}-[0-9]{2}\)\s*### Bug Fixes\s*\* \*\*bar:\*\* fix bar [0-9a-f]{7}$/
);
const title = header(latestVersion, version, suffix);
const bar = commit('fix', 'bar');
assert.match(nl(changelog), new RegExp('^' + title + fixes + bar + '$'));
});
test('should respect --no-increment and return previous, identical changelog', async () => {
setup();
sh.exec(`git tag 1.0.0`);
add('feat', 'bar');
add('fix', 'baz');
sh.exec(`git tag 1.1.0`);
add('fix', 'bar');
add('feat', 'baz');
sh.exec(`git tag 1.2.0`);
const [config, container] = getOptions({ preset });
config.increment = false;
const { changelog } = await runTasks(config, container);
const title = header('1.1.0', '1.2.0');
const bar = commit('fix', 'bar');
const baz = commit('feat', 'baz');
assert.match(nl(changelog), new RegExp('^' + title + fixes + bar + features + baz + '$'));
});
test('should ignore recommended bump (option)', async () => {
setup();
sh.exec(`git tag 1.0.0`);
add('feat', 'baz');
const options = getOptions({ preset, ignoreRecommendedBump: true });
const { version } = await runTasks(...options);
assert.equal(version, '1.0.1');
});
test('should use provided pre-release id', async t => {
setup();
sh.exec(`git tag 1.0.0`);
add('feat', 'baz');
const [config, container] = getOptions({ preset });
config.preRelease = 'alpha';
const { version } = await runTasks(config, container);
assert.equal(version, '1.1.0-alpha.0');
});
test('should follow conventional commit strategy with prereleaase', async t => {
setup();
sh.exec(`git tag 1.2.1`);
add('feat', 'baz');
const [config, container] = getOptions({ preset: { name: 'conventionalcommits' } }, { commit: true, tag: true });
config.preRelease = 'alpha';
const { version: version1 } = await runTasks(config, container);
assert.equal(version1, '1.3.0-alpha.0');
add('fix', 'buz');
const { version: version2 } = await runTasks(config, container);
assert.equal(version2, '1.3.0-alpha.1');
add('feat', 'biz', { breaking: true });
const { version: version3 } = await runTasks(config, container);
assert.equal(version3, '2.0.0-alpha.0');
add('fix', 'boz');
const { version: version4 } = await runTasks(config, container);
assert.equal(version4, '2.0.0-alpha.1');
});
test('should use provided pre-release id (pre-release continuation)', async t => {
setup();
sh.exec(`git tag 1.0.1-alpha.0`);
add('feat', 'baz');
const [config, container] = getOptions({ preset });
config.preRelease = 'alpha';
const { version } = await runTasks(config, container);
assert.equal(version, '1.0.1-alpha.1');
});
test('should use provided pre-release id (next pre-release)', async t => {
setup();
sh.exec(`git tag 1.1.0-alpha.1`);
const [config, container] = getOptions({ preset });
config.preRelease = 'beta';
const { version } = await runTasks(config, container);
assert.equal(version, '1.1.0-beta.0');
});
test('should use recommended bump (after pre-rerelease)', async t => {
setup();
sh.exec(`git tag 1.0.1-beta.0`);
add('feat', 'baz');
const options = getOptions({ preset });
const { version } = await runTasks(...options);
assert.equal(version, '1.1.0');
});
test('should follow strict semver (pre-release continuation)', async t => {
setup();
sh.exec(`git tag 1.1.0-alpha.0`);
add('feat', 'baz');
const [config, container] = getOptions({ preset, strictSemVer: true });
config.preRelease = 'alpha';
const { version } = await runTasks(config, container);
assert.equal(version, '1.2.0-alpha.0');
});
test('should follow strict semver (pre-release continuation, conventionalcommits)', async t => {
setup();
sh.exec(`git tag 2.0.1-alpha.0`);
sh.ShellString('file').toEnd('file');
sh.exec(`git add file`);
sh.exec(`git commit -m "feat: new feature"`);
const [config, container] = getOptions({
preset: { name: 'conventionalcommits' },
strictSemVer: true,
writerOpts: {},
parserOpts: {}
});
config.preRelease = 'alpha';
const { version } = await runTasks(config, container);
assert.equal(version, '2.1.0-alpha.0');
});
test('should use provided increment', async () => {
setup();
sh.exec(`git tag 1.0.0`);
const [config, container] = getOptions({ preset });
config.increment = 'major';
const { version } = await runTasks(config, container);
assert.equal(version, '2.0.0');
});
test('should use provided version (ignore recommended bump)', async () => {
setup();
const [config, container] = getOptions({ preset });
config.increment = '1.2.3';
const { version } = await runTasks(config, container);
assert.equal(version, '1.2.3');
});
test('should not throw with Git plugin disabled', async () => {
setup();
const [config, container] = getOptions({ preset });
config.git = false;
const { version, changelog } = await runTasks(config, container);
assert.equal(version, '0.0.1');
const title = `## 0.0.1 ${date}`;
const fix = commit('fix', 'foo');
assert.match(nl(changelog), new RegExp(title + fixes + fix));
});
test(`should write and update infile`, async () => {
const { dir } = setup();
sh.exec(`git tag 1.0.0`);
add('fix', 'foo');
add('feat', 'bar');
const h = 'The header' + EOL + EOL + 'The subheader';
const infile = path.join(dir, 'CHANGES.md');
const [config, container] = getOptions({ preset, infile, header: h });
config.git.tag = true;
await runTasks(config, container);
const changelog = fs.readFileSync(infile).toString();
const title = header('1.0.0', '1.1.0');
const fix1 = commit('fix', 'foo');
const feat1 = commit('feat', 'bar');
const first = title + fixes + fix1 + features + feat1;
assert.match(nl(changelog), new RegExp('^' + h + EOL + EOL + first + EOL + '$'));
{
add('fix', 'bar');
add('fix', 'baz');
const options = getOptions({ preset, infile, header: h });
await runTasks(...options);
const changelog = fs.readFileSync(infile).toString();
const title2 = header('1.1.0', '1.1.1');
const fix2 = commit('fix', 'bar');
const fix3 = commit('fix', 'baz');
const second = title2 + fixes + fix2 + fix3;
assert.match(nl(changelog), new RegExp('^' + h + EOL + EOL + second + EOL + EOL + first + EOL + '$'));
}
});
test('should reject if conventional bump passes error', async () => {
setup();
const options = getOptions({ preset: 'what?' });
await assert.rejects(
runTasks(...options),
'Error: Unable to load the "what?" preset package. Please make sure it\'s installed.'
);
});
test('should reject if conventional changelog has error', async () => {
setup();
const options = getOptions({ preset: () => {} });
await assert.rejects(runTasks(...options), /preset must be string or object with property `name`/i);
});
test('should not write infile in dry run', async () => {
const { dir } = setup();
const infile = path.join(dir, 'DRYRUN.md');
const [config, container] = getOptions({ preset, infile });
config['dry-run'] = true;
await runTasks(config, container);
assert.throws(() => fs.readFileSync(infile), /no such file/);
});
test('should not write infile if set to false', async () => {
const { dir } = setup();
const infile = path.join(dir, 'DRYRUN.md');
const options = getOptions({ preset, infile: false });
const { version } = await runTasks(...options);
assert.throws(() => fs.readFileSync(infile), /no such file/);
assert.equal(version, '0.0.1');
});
test('should not bump when recommended bump returns null', async () => {
setup();
sh.exec(`git tag 1.0.0`);
add('fix', 'bar');
add('feat', 'baz');
{
const options = getOptions({ preset: 'angular' });
const { version } = await runTasks(...options);
assert.equal(version, '1.1.0');
}
add('blorp', 'faz');
add('faz', 'blorp');
{
const options = getOptions({ preset: 'angular' });
const { version } = await runTasks(...options);
assert.equal(version, '1.1.0'); // Incorrect result from conventional-recommended-bump
}
{
const whatBump = commits => ({ level: null, reason: 'Parsed commits do not warrant a version bump.' });
const options = getOptions({ whatBump });
const { version } = await runTasks(...options);
assert.equal(version, undefined);
}
});
// TODO Prepare test and verify results influenced by parserOpts and writerOpts
test.skip('should pass parserOpts and writerOpts', async t => {
setup();
const parserOpts = {
mergePattern: /^Merge pull request #(\d+) from (.*)$/,
mergeCorrespondence: ['id', 'source']
};
const writerOpts = {
groupBy: 'type'
};
const [config, container] = getOptions({ preset, parserOpts, writerOpts });
await runTasks(config, container);
});