generated from release-it/plugin-starterkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
225 lines (205 loc) · 8.47 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
import assert from 'assert/strict';
import test from 'bron';
import fs from 'fs';
import { patchFs } from 'fs-monkey';
import { vol } from 'memfs';
import os from 'os';
import path from 'path';
import { factory, runTasks } from 'release-it/test/util/index.js';
import sinon from 'sinon';
import NoPreReleaseDependenciesPlugin from './index.js';
const namespace = JSON.parse(fs.readFileSync('./package.json')).name;
const workingDir = process.cwd();
const testDir = fs.mkdtempSync(path.join(os.tmpdir(), 'rit-plugin-test-'));
process.chdir(testDir);
process.addListener('exit', () => {
process.chdir(workingDir);
fs.rmSync(testDir, { recursive: true, force: true });
});
patchFs(vol);
const initVolume = (volumeJson) => {
vol.reset();
vol.fromJSON(volumeJson);
};
test('isEnabled true with manifest and modules', () => {
initVolume({
'./package.json': JSON.stringify({ dependencies: {}, devDependencies: {} }),
'./node_modules': null
});
assert.equal(NoPreReleaseDependenciesPlugin.isEnabled(), true);
});
test('isEnabled false without manifest', () => {
initVolume({
'./node_modules': null
});
assert.equal(NoPreReleaseDependenciesPlugin.isEnabled(), false);
});
test('isEnabled false without modules', () => {
initVolume({
'./package.json': JSON.stringify({ dependencies: {}, devDependencies: {} })
});
assert.equal(NoPreReleaseDependenciesPlugin.isEnabled(), false);
});
test('isEnabled false if options=false', () => {
initVolume({
'./package.json': JSON.stringify({ dependencies: {}, devDependencies: {} }),
'./node_modules': null
});
assert.equal(NoPreReleaseDependenciesPlugin.isEnabled(false), false);
});
test('getInitialOptions without includes-excludes options', () => {
const options = { [namespace]: {} };
const plugin = factory(NoPreReleaseDependenciesPlugin, { namespace, options });
assert.deepEqual(plugin.options.includes, []);
assert.deepEqual(plugin.options.excludes, []);
});
test('getInitialOptions with includes-excludes options', () => {
const options = { [namespace]: { includes: ['^foo-', '^@bar/'], excludes: ['baz$'] } };
const plugin = factory(NoPreReleaseDependenciesPlugin, { namespace, options });
assert.deepEqual(plugin.options.includes, [/^foo-/, /^@bar\//]);
assert.deepEqual(plugin.options.excludes, [/baz$/]);
});
test('should not throw if no preRelease dependencies', async () => {
const plugin = factory(NoPreReleaseDependenciesPlugin);
sinon.stub(plugin, 'hasPreReleaseDependencies').returns(false);
await assert.doesNotReject(runTasks(plugin));
});
test('should throw if preRelease dependencies', async () => {
const plugin = factory(NoPreReleaseDependenciesPlugin);
sinon.stub(plugin, 'hasPreReleaseDependencies').returns(true);
sinon.stub(plugin, 'getContext').returns({
preReleaseDependencies: {
dependencies: {},
devDependencies: {
[DEP_INSTALLED_PRE.name]: { range: DEP_INSTALLED_PRE.range, installed: DEP_INSTALLED_PRE.installed }
}
}
});
await assert.rejects(runTasks(plugin));
});
test('isPreRelease', () => {
const plugin = factory(NoPreReleaseDependenciesPlugin);
assert.equal(plugin.isPreRelease('1.0.0-beta.0'), true);
assert.equal(plugin.isPreRelease('1.0.0-beta'), true);
assert.equal(plugin.isPreRelease('1.0.0-0'), true);
assert.equal(plugin.isPreRelease('1.0.0'), false);
assert.equal(plugin.isPreRelease(null), false);
assert.equal(plugin.isPreRelease(undefined), false);
});
const DEP_INSTALLED = { name: 'dep-installed', range: '^1.0.0', installed: '1.1.1' };
const DEP_INSTALLED_PRE = { name: 'dep-installed-pre', range: '^3.0.0-pre', installed: '3.0.0-beta.3' };
const DEP_NOT_INSTALLED = { name: 'dep-not-installed', range: '*', installed: undefined };
test('hasPreReleaseDependencies false', () => {
initVolume({
'./package.json': JSON.stringify({ dependencies: { [DEP_INSTALLED.name]: DEP_INSTALLED.range } })
});
const plugin = factory(NoPreReleaseDependenciesPlugin);
sinon
.stub(plugin, 'getPreReleaseDependenciesInfo')
.withArgs({ [DEP_INSTALLED.name]: DEP_INSTALLED.range })
.returns({})
.withArgs({})
.returns({});
const spySetContext = sinon.spy(plugin, 'setContext');
const hasPreReleaseDeps = plugin.hasPreReleaseDependencies();
assert.equal(hasPreReleaseDeps, false);
assert.equal(spySetContext.callCount, 1);
assert.deepEqual(spySetContext.getCall(0).args, [
{
preReleaseDependencies: {
dependencies: {},
devDependencies: {}
}
}
]);
});
test('hasPreReleaseDependencies true', () => {
initVolume({
'./package.json': JSON.stringify({ devDependencies: { [DEP_INSTALLED_PRE.name]: DEP_INSTALLED_PRE.range } })
});
const plugin = factory(NoPreReleaseDependenciesPlugin);
sinon
.stub(plugin, 'getPreReleaseDependenciesInfo')
.withArgs({})
.returns({})
.withArgs({ [DEP_INSTALLED_PRE.name]: DEP_INSTALLED_PRE.range })
.returns({ [DEP_INSTALLED_PRE.name]: { range: DEP_INSTALLED_PRE.range, installed: DEP_INSTALLED_PRE.installed } });
const spySetContext = sinon.spy(plugin, 'setContext');
const hasPreReleaseDeps = plugin.hasPreReleaseDependencies();
assert.equal(hasPreReleaseDeps, true);
assert.equal(spySetContext.callCount, 1);
assert.deepEqual(spySetContext.getCall(0).args, [
{
preReleaseDependencies: {
dependencies: {},
devDependencies: {
[DEP_INSTALLED_PRE.name]: { range: DEP_INSTALLED_PRE.range, installed: DEP_INSTALLED_PRE.installed }
}
}
}
]);
});
const stubAndCallGetPreReleaseDependenciesInfo = (plugin) => {
sinon
.stub(plugin, 'getDependencyEntryVersions')
.withArgs([DEP_INSTALLED.name, DEP_INSTALLED.range])
.returns([DEP_INSTALLED.name, { range: DEP_INSTALLED.range, installed: DEP_INSTALLED.installed }])
.withArgs([DEP_INSTALLED_PRE.name, DEP_INSTALLED_PRE.range])
.returns([DEP_INSTALLED_PRE.name, { range: DEP_INSTALLED_PRE.range, installed: DEP_INSTALLED_PRE.installed }])
.withArgs([DEP_NOT_INSTALLED.name, DEP_NOT_INSTALLED.range])
.returns([DEP_NOT_INSTALLED.name, { range: DEP_NOT_INSTALLED.range, installed: DEP_NOT_INSTALLED.installed }]);
sinon
.stub(plugin, 'isPreRelease')
.withArgs(DEP_INSTALLED.installed)
.returns(false)
.withArgs(DEP_INSTALLED_PRE.installed)
.returns(true);
return plugin.getPreReleaseDependenciesInfo({
[DEP_INSTALLED.name]: DEP_INSTALLED.range,
[DEP_INSTALLED_PRE.name]: DEP_INSTALLED_PRE.range,
[DEP_NOT_INSTALLED.name]: DEP_NOT_INSTALLED.range
});
};
test('getPreReleaseDependenciesInfo found', () => {
const plugin = factory(NoPreReleaseDependenciesPlugin);
const preReleaseInfo = stubAndCallGetPreReleaseDependenciesInfo(plugin);
assert.deepEqual(preReleaseInfo, {
[DEP_INSTALLED_PRE.name]: { range: DEP_INSTALLED_PRE.range, installed: DEP_INSTALLED_PRE.installed }
});
});
test('getPreReleaseDependenciesInfo but not included', () => {
const options = { [namespace]: { includes: [`^${DEP_INSTALLED.name}$`, `^${DEP_NOT_INSTALLED.name}$`] } };
const plugin = factory(NoPreReleaseDependenciesPlugin, { namespace, options });
const preReleaseInfo = stubAndCallGetPreReleaseDependenciesInfo(plugin);
assert.deepEqual(preReleaseInfo, {});
});
test('getPreReleaseDependenciesInfo but excluded', () => {
const options = { [namespace]: { excludes: [`^${DEP_INSTALLED_PRE.name}$`] } };
const plugin = factory(NoPreReleaseDependenciesPlugin, { namespace, options });
const preReleaseInfo = stubAndCallGetPreReleaseDependenciesInfo(plugin);
assert.deepEqual(preReleaseInfo, {});
});
test('getDependencyEntryVersions', () => {
initVolume({
'./package.json': JSON.stringify({
dependencies: { [DEP_INSTALLED.name]: DEP_INSTALLED.range },
devDependencies: { [DEP_NOT_INSTALLED.name]: DEP_NOT_INSTALLED.range }
}),
[`./node_modules/${DEP_INSTALLED.name}/package.json`]: JSON.stringify({
name: DEP_INSTALLED.name,
version: DEP_INSTALLED.installed
})
// no DEP_NOT_INSTALLED on purpose
});
const plugin = factory(NoPreReleaseDependenciesPlugin);
const depInstalledVersions = plugin.getDependencyEntryVersions([DEP_INSTALLED.name, DEP_INSTALLED.range]);
const depNotInstalledVersions = plugin.getDependencyEntryVersions([DEP_NOT_INSTALLED.name, DEP_NOT_INSTALLED.range]);
assert.deepEqual(depInstalledVersions, [
DEP_INSTALLED.name,
{ range: DEP_INSTALLED.range, installed: DEP_INSTALLED.installed }
]);
assert.deepEqual(depNotInstalledVersions, [
DEP_NOT_INSTALLED.name,
{ range: DEP_NOT_INSTALLED.range, installed: DEP_NOT_INSTALLED.installed }
]);
});