-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathprepare.test.js
288 lines (248 loc) · 12.7 KB
/
prepare.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
const path = require('path');
const test = require('ava');
const {outputFile, remove} = require('fs-extra');
const {stub} = require('sinon');
const prepare = require('../lib/prepare.js');
const {gitRepo, gitGetCommits, gitCommitedFiles, gitAdd, gitCommits, gitPush} = require('./helpers/git-utils.js');
test.beforeEach((t) => {
// Stub the logger functions
t.context.log = stub();
t.context.logger = {log: t.context.log};
});
test('Commit CHANGELOG.md, package.json, package-lock.json, and npm-shrinkwrap.json if they exists and have been changed', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0', notes: 'Test release note'};
const changelogPath = path.resolve(cwd, 'CHANGELOG.md');
const pkgPath = path.resolve(cwd, 'package.json');
const pkgLockPath = path.resolve(cwd, 'package-lock.json');
const shrinkwrapPath = path.resolve(cwd, 'npm-shrinkwrap.json');
await outputFile(changelogPath, 'Initial CHANGELOG');
await outputFile(pkgPath, "{name: 'test-package'}");
await outputFile(pkgLockPath, "{name: 'test-package'}");
await outputFile(shrinkwrapPath, "{name: 'test-package'}");
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
// Verify the remote repo has a the version referencing the same commit sha at the local head
const [commit] = await gitGetCommits(undefined, {cwd, env});
// Verify the files that have been commited
t.deepEqual(
(await gitCommitedFiles('HEAD', {cwd, env})).sort(),
['CHANGELOG.md', 'package.json', 'package-lock.json', 'npm-shrinkwrap.json'].sort()
);
t.is(commit.subject, `chore(release): ${nextRelease.version} [skip ci]`);
t.is(commit.body, `${nextRelease.notes}\n`);
t.is(commit.gitTags, `(HEAD -> ${branch.name})`);
t.deepEqual(t.context.log.args[0], ['Found %d file(s) to commit', 4]);
t.deepEqual(t.context.log.args[1], ['Prepared Git release: %s', nextRelease.gitTag]);
});
test('Exclude CHANGELOG.md, package.json, package-lock.json, and npm-shrinkwrap.json if "assets" is defined without it', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {assets: []};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0'};
await outputFile(path.resolve(cwd, 'CHANGELOG.md'), 'Initial CHANGELOG');
await outputFile(path.resolve(cwd, 'package.json'), "{name: 'test-package'}");
await outputFile(path.resolve(cwd, 'package-lock.json'), "{name: 'test-package'}");
await outputFile(path.resolve(cwd, 'npm-shrinkwrap.json'), "{name: 'test-package'}");
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
// Verify no files have been commited
t.deepEqual(await gitCommitedFiles('HEAD', {cwd, env}), []);
});
test('Allow to customize the commit message', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {
message: `Release version \${nextRelease.version} from branch \${branch}
Last release: \${lastRelease.version}
\${nextRelease.notes}`,
};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {version: 'v1.0.0'};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0', notes: 'Test release note'};
await outputFile(path.resolve(cwd, 'CHANGELOG.md'), 'Initial CHANGELOG');
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
// Verify the files that have been commited
t.deepEqual(await gitCommitedFiles('HEAD', {cwd, env}), ['CHANGELOG.md']);
// Verify the commit message contains on the new release notes
const [commit] = await gitGetCommits(undefined, {cwd, env});
t.is(commit.subject, `Release version ${nextRelease.version} from branch ${branch.name}`);
t.is(commit.body, `Last release: ${lastRelease.version}\n${nextRelease.notes}\n`);
});
test('Commit files matching the patterns in "assets"', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {
assets: ['file1.js', '*1.js', ['dir/*.js', '!dir/*.css'], 'file5.js', 'dir2', ['**/*.js', '!**/*.js']],
};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0'};
// Create .gitignore to ignore file5.js
await outputFile(path.resolve(cwd, '.gitignore'), 'file5.js');
await outputFile(path.resolve(cwd, 'file1.js'), 'Test content');
await outputFile(path.resolve(cwd, 'dir/file2.js'), 'Test content');
await outputFile(path.resolve(cwd, 'dir/file3.css'), 'Test content');
await outputFile(path.resolve(cwd, 'file4.js'), 'Test content');
await outputFile(path.resolve(cwd, 'file5.js'), 'Test content');
await outputFile(path.resolve(cwd, 'dir2/file6.js'), 'Test content');
await outputFile(path.resolve(cwd, 'dir2/file7.css'), 'Test content');
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
// Verify file2 and file1 have been commited
// file4.js is excluded as no glob matching
// file3.css is ignored due to the negative glob '!dir/*.css'
// file5.js is not ignored even if it's in the .gitignore
// file6.js and file7.css are included because dir2 is expanded
t.deepEqual(
(await gitCommitedFiles('HEAD', {cwd, env})).sort(),
['dir/file2.js', 'dir2/file6.js', 'dir2/file7.css', 'file1.js', 'file5.js'].sort()
);
t.deepEqual(t.context.log.args[0], ['Found %d file(s) to commit', 5]);
});
test('Commit no files when "assets" is false', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {
assets: false,
};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0'};
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
t.deepEqual((await gitCommitedFiles('HEAD', {cwd, env})).sort(), []);
});
test('Commit files matching the patterns in "assets" as Objects', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {
assets: ['file1.js', {path: ['dir/*.js', '!dir/*.css']}, {path: 'file5.js'}, 'dir2'],
};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0'};
// Create .gitignore to ignore file5.js
await outputFile(path.resolve(cwd, '.gitignore'), 'file5.js');
await outputFile(path.resolve(cwd, 'file1.js'), 'Test content');
await outputFile(path.resolve(cwd, 'dir/file2.js'), 'Test content');
await outputFile(path.resolve(cwd, 'dir/file3.css'), 'Test content');
await outputFile(path.resolve(cwd, 'file4.js'), 'Test content');
await outputFile(path.resolve(cwd, 'file5.js'), 'Test content');
await outputFile(path.resolve(cwd, 'dir2/file6.js'), 'Test content');
await outputFile(path.resolve(cwd, 'dir2/file7.css'), 'Test content');
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
// Verify file2 and file1 have been commited
// file4.js is excluded as no glob matching
// file3.css is ignored due to the negative glob '!dir/*.css'
// file5.js is not ignored even if it's in the .gitignore
// file6.js and file7.css are included because dir2 is expanded
t.deepEqual(
(await gitCommitedFiles('HEAD', {cwd, env})).sort(),
['dir/file2.js', 'dir2/file6.js', 'dir2/file7.css', 'file1.js', 'file5.js'].sort()
);
t.deepEqual(t.context.log.args[0], ['Found %d file(s) to commit', 5]);
});
test('Commit files matching the patterns in "assets" as single glob', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {assets: 'dist/**/*.js'};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0'};
await outputFile(path.resolve(cwd, 'dist/file1.js'), 'Test content');
await outputFile(path.resolve(cwd, 'dist/file2.css'), 'Test content');
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
t.deepEqual(await gitCommitedFiles('HEAD', {cwd, env}), ['dist/file1.js']);
t.deepEqual(t.context.log.args[0], ['Found %d file(s) to commit', 1]);
});
test('Commit files matching the patterns in "assets", including dot files', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {assets: 'dist'};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0'};
await outputFile(path.resolve(cwd, 'dist/.dotfile'), 'Test content');
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
t.deepEqual(await gitCommitedFiles('HEAD', {cwd, env}), ['dist/.dotfile']);
t.deepEqual(t.context.log.args[0], ['Found %d file(s) to commit', 1]);
});
test('Include deleted files in release commit', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {
assets: ['file1.js'],
};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0'};
await outputFile(path.resolve(cwd, 'file1.js'), 'Test content');
await outputFile(path.resolve(cwd, 'file2.js'), 'Test content');
await gitAdd(['file1.js', 'file2.js'], {cwd, env});
await gitCommits(['Add file1.js and file2.js'], {cwd, env});
await gitPush(repositoryUrl, 'master', {cwd, env});
await remove(path.resolve(cwd, 'file1.js'));
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
t.deepEqual((await gitCommitedFiles('HEAD', {cwd, env})).sort(), ['file1.js'].sort());
t.deepEqual(t.context.log.args[0], ['Found %d file(s) to commit', 1]);
});
test('Set the commit author and committer name/email based on environment variables', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {
GIT_AUTHOR_NAME: 'author name',
GIT_AUTHOR_EMAIL: 'author email',
GIT_COMMITTER_NAME: 'committer name',
GIT_COMMITTER_EMAIL: 'committer email',
};
const lastRelease = {version: 'v1.0.0'};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0', notes: 'Test release note'};
await outputFile(path.resolve(cwd, 'CHANGELOG.md'), 'Initial CHANGELOG');
await prepare({}, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
// Verify the files that have been commited
t.deepEqual(await gitCommitedFiles('HEAD', {cwd, env}), ['CHANGELOG.md']);
// Verify the commit message contains on the new release notes
const [commit] = await gitGetCommits(undefined, {cwd, env});
t.is(commit.author.name, 'author name');
t.is(commit.author.email, 'author email');
t.is(commit.committer.name, 'committer name');
t.is(commit.committer.email, 'committer email');
});
test('Skip negated pattern if its alone in its group', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {assets: ['!**/*', 'file.js']};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0'};
await outputFile(path.resolve(cwd, 'file.js'), 'Test content');
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
t.deepEqual(await gitCommitedFiles('HEAD', {cwd, env}), ['file.js']);
t.deepEqual(t.context.log.args[0], ['Found %d file(s) to commit', 1]);
});
test('Skip commit if there is no files to commit', async (t) => {
const {cwd, repositoryUrl} = await gitRepo(true);
const pluginConfig = {};
const branch = {name: 'master'};
const options = {repositoryUrl};
const env = {};
const lastRelease = {};
const nextRelease = {version: '2.0.0', gitTag: 'v2.0.0', notes: 'Test release note'};
await prepare(pluginConfig, {cwd, env, options, branch, lastRelease, nextRelease, logger: t.context.logger});
// Verify the files that have been commited
t.deepEqual(await gitCommitedFiles('HEAD', {cwd, env}), []);
});