forked from release-it/conventional-changelog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
107 lines (94 loc) · 3.77 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
const test = require('bron');
const assert = require('assert');
const { EOL } = require('os');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const stream = require('stream');
const fs = require('fs');
const { factory, runTasks } = require('release-it/test/util');
const conventionalRecommendedBump = sinon.stub().callsFake((options, cb) => {
if (options.preset === 'angular') return cb(null, { releaseType: 'minor' });
cb(new Error('Something went wrong'));
});
const conventionalChangelog = sinon.stub().callsFake(options => {
const s = new stream.Readable();
s._read = () => {};
process.nextTick(() => {
s.emit('data', 'The changelog');
if (options.releaseCount < 0) s.emit('error', new Error('Something went wrong'));
s.emit('end');
});
return s;
});
const Plugin = proxyquire('.', {
'conventional-recommended-bump': conventionalRecommendedBump,
'conventional-changelog': conventionalChangelog
});
const namespace = 'conventional-changelog';
const preset = 'angular';
const infile = 'CHANGES.md';
test('should not throw', async t => {
const options = { [namespace]: { preset } };
const plugin = factory(Plugin, { namespace, options });
await assert.doesNotReject(runTasks(plugin));
});
test('should set changelog', async t => {
const options = { [namespace]: { preset } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
const { changelog } = plugin.config.getContext();
assert.strictEqual(changelog, 'The changelog');
});
test('should use recommended bump', async t => {
const options = { [namespace]: { preset } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
const { version } = plugin.config.getContext();
assert.strictEqual(version, '1.1.0');
});
test('should use provided increment', async t => {
const options = { increment: 'major', [namespace]: { preset } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
const { version } = plugin.config.getContext();
assert.strictEqual(version, '2.0.0');
});
test('should use provided version', async t => {
const options = { increment: '1.2.3', [namespace]: { preset } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
const { version } = plugin.config.getContext();
assert.strictEqual(version, '1.2.3');
});
test(`should write and update infile (${infile})`, async t => {
const options = { [namespace]: { preset, infile } };
const plugin = factory(Plugin, { namespace, options });
await runTasks(plugin);
const changelog = fs.readFileSync(infile);
assert.strictEqual(changelog.toString().trim(), 'The changelog');
{
await runTasks(plugin);
const changelog = fs.readFileSync(infile);
assert.strictEqual(changelog.toString().trim(), `The changelog${EOL}${EOL}The changelog`);
}
fs.unlinkSync(infile);
});
test('should reject if conventional bump passes error', async t => {
const options = { [namespace]: { preset: 'what?' } };
const plugin = factory(Plugin, { namespace, options });
await assert.rejects(runTasks(plugin), /Something went wrong/);
});
test('should reject if conventional changelog has error', async t => {
const options = { [namespace]: { preset, releaseCount: -1 } };
const plugin = factory(Plugin, { namespace, options });
await assert.rejects(runTasks(plugin), /Something went wrong/);
});
test('should not write infile in dry run', async t => {
const infile = 'DRYRUN.md';
const options = { [namespace]: { preset, infile } };
const plugin = factory(Plugin, { namespace, options, global: { isDryRun: true } });
const spy = sinon.spy(plugin, 'writeChangelog');
await runTasks(plugin);
assert.strictEqual(spy.callCount, 0);
assert.throws(() => fs.readFileSync(infile), /no such file/);
});