-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.js
340 lines (252 loc) · 10.1 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
import fs from 'node:fs';
import path from 'node:path';
import { createRequire } from 'node:module';
import tmp from 'tmp';
import test from 'ava';
import { factory, runTasks } from 'release-it/test/util/index.js';
import Plugin from './index.js';
const EDITOR = process.env.EDITOR || null;
const PATH = process.env.PATH;
tmp.setGracefulCleanup();
const require = createRequire(import.meta.url);
const LERNA_PATH = require.resolve('lerna-changelog/bin/cli');
const namespace = 'release-it-lerna-changelog';
function resetPATH() {
process.env.PATH = PATH;
}
function resetEDITOR() {
if (EDITOR === null) {
delete process.env.EDITOR;
} else {
process.env.EDITOR = EDITOR;
}
}
async function buildEditorCommand(commandName = 'fake-editor') {
let tmpdir = tmp.dirSync().name;
let editor = `${tmpdir}/${commandName}`;
let output = tmp.fileSync().name;
let fakeCommand = `#!${process.execPath}
'use strict';
const fs = require('fs');
fs.writeFileSync('${output}', \`args: \${process.argv.slice(2).join(' ')}\`, 'utf-8');
`;
fs.writeFileSync(editor, fakeCommand, { encoding: 'utf-8' });
fs.chmodSync(editor, 0o755);
return { editor, output };
}
class TestPlugin extends Plugin {
constructor() {
super(...arguments);
this.responses = {
// always assume v1.0.0 unless specifically overridden
'git describe --tags --abbrev=0': 'v1.0.0',
[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`]:
'## Unreleased (2020-03-18)\n\nThe changelog',
};
this.commands = [];
this.shell.execFormattedCommand = async (command, options) => {
this.commands.push([command, options]);
if (this.responses[command]) {
let response = this.responses[command];
if (typeof response === 'string') {
return Promise.resolve(response);
} else if (typeof response === 'object' && response !== null && response.reject === true) {
return Promise.reject(response.value);
}
}
};
}
async _launchEditor(tmpFile) {
this.launchedTmpFile = tmpFile;
return super._launchEditor(tmpFile);
}
}
function buildPlugin(config = {}, _Plugin = TestPlugin) {
const options = { [namespace]: config };
const plugin = factory(_Plugin, { namespace, options });
return plugin;
}
test('it invokes lerna-changelog', async (t) => {
let plugin = buildPlugin();
await runTasks(plugin);
t.deepEqual(plugin.commands, [
['git describe --tags --abbrev=0', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`, { write: false }],
]);
});
test('it honors custom git.tagName formatting', async (t) => {
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile });
plugin.config.setContext({ git: { tagName: 'v${version}' } });
await runTasks(plugin);
t.deepEqual(plugin.commands, [
['git describe --tags --abbrev=0', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`, { write: false }],
]);
const changelog = fs.readFileSync(infile, { encoding: 'utf8' });
t.is(changelog, `## v1.0.1 (2020-03-18)\n\nThe changelog\n\n`);
});
test('it sets the changelog without version information onto the config', async (t) => {
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile });
await runTasks(plugin);
const { changelog } = plugin.config.getContext();
t.is(changelog, 'The changelog');
});
test('it prints something to CHANGELOG.md when lerna-changelog returns no content', async (t) => {
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile });
plugin.responses[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`] = '';
await runTasks(plugin);
const changelog = fs.readFileSync(infile, { encoding: 'utf8' });
t.is(changelog, `## 1.0.1 (${new Date().toISOString().slice(0, 10)})\n\n`);
});
test('it uses the first commit when no tags exist', async (t) => {
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile });
plugin.config.setContext({ git: { tagName: 'v${version}' } });
Object.assign(plugin.responses, {
'git describe --tags --abbrev=0': {
reject: true,
value: 'hahahahaah, does not exist',
},
'git rev-list --max-parents=0 HEAD': 'aabc',
[`${LERNA_PATH} --next-version=Unreleased --from=aabc`]: `## Unreleased\n\nThe changelog\n## v1.0.0\n\nThe old changelog`,
});
await runTasks(plugin);
t.deepEqual(plugin.commands, [
['git describe --tags --abbrev=0', { write: false }],
['git rev-list --max-parents=0 HEAD', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=aabc`, { write: false }],
]);
const changelog = fs.readFileSync(infile, { encoding: 'utf8' });
t.is(changelog.trim(), '## v1.0.1\n\nThe changelog\n## v1.0.0\n\nThe old changelog');
});
test('it writes the changelog to the specified file when it did not exist', async (t) => {
let infile = tmp.fileSync().name;
fs.unlinkSync(infile);
let plugin = buildPlugin({ infile });
plugin.config.setContext({ git: { tagName: 'v${version}' } });
Object.assign(plugin.responses, {
'git rev-list --max-parents=0 HEAD': 'aabc',
[`${LERNA_PATH} --next-version=Unreleased --from=aabc`]: `## Unreleased\n\nThe changelog\n## v1.0.0\n\nThe old changelog`,
});
await runTasks(plugin);
t.deepEqual(plugin.commands, [
['git describe --tags --abbrev=0', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`, { write: false }],
['git rev-list --max-parents=0 HEAD', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=aabc`, { write: false }],
[`git add ${infile}`, {}],
]);
const changelog = fs.readFileSync(infile, { encoding: 'utf8' });
t.is(changelog.trim(), '## v1.0.1\n\nThe changelog\n## v1.0.0\n\nThe old changelog');
});
test('prepends the changelog to the existing file', async (t) => {
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile });
plugin.config.setContext({ git: { tagName: 'v${version}' } });
fs.writeFileSync(infile, 'Old contents', { encoding: 'utf8' });
await runTasks(plugin);
const changelog = fs.readFileSync(infile);
t.is(changelog.toString().trim(), '## v1.0.1 (2020-03-18)\n\nThe changelog\n\nOld contents');
});
test('adds the changelog after an existing first level heading', async (t) => {
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile });
plugin.config.setContext({ git: { tagName: 'v${version}' } });
fs.writeFileSync(infile, '# Changelog\n\n## v1.0.0\n\nThe old changelog', { encoding: 'utf8' });
await runTasks(plugin);
const changelog = fs.readFileSync(infile);
t.is(
changelog.toString().trim(),
'# Changelog\n\n## v1.0.1 (2020-03-18)\n\nThe changelog\n\n## v1.0.0\n\nThe old changelog'
);
});
test('uses launchEditor command', async (t) => {
let infile = tmp.fileSync().name;
let { editor, output } = await buildEditorCommand();
let plugin = buildPlugin({ infile, launchEditor: `${editor} \${file}` });
await runTasks(plugin);
t.is(fs.readFileSync(output, 'utf-8'), `args: ${plugin.launchedTmpFile}`);
});
test('does not launch the editor for dry-run', async (t) => {
let infile = tmp.fileSync().name;
let { editor, output } = await buildEditorCommand();
let plugin = buildPlugin({ infile, launchEditor: `${editor} \${file}` });
plugin.config.options['dry-run'] = true;
await runTasks(plugin);
t.is(fs.readFileSync(output, 'utf-8'), ``);
});
test('detects default editor from $EDITOR if launchEditor is `true`', async (t) => {
let infile = tmp.fileSync().name;
let { editor, output } = await buildEditorCommand();
let plugin = buildPlugin({ infile, launchEditor: true });
try {
process.env.EDITOR = editor;
await runTasks(plugin);
t.is(fs.readFileSync(output, 'utf-8'), `args: ${plugin.launchedTmpFile}`);
} finally {
resetEDITOR();
}
});
test('detects default editor via `editor` if launchEditor is `true`', async (t) => {
let infile = tmp.fileSync().name;
let { editor, output } = await buildEditorCommand('editor');
let plugin = buildPlugin({ infile, launchEditor: true });
try {
delete process.env.EDITOR;
process.env.PATH = `${path.dirname(editor)}:${process.env.PATH}`;
await runTasks(plugin);
t.is(fs.readFileSync(output, 'utf-8'), `args: ${plugin.launchedTmpFile}`);
} finally {
resetPATH();
resetEDITOR();
}
});
test('throws if launchEditor is `true`, no $EDITOR present, and `editor` is not found on $PATH', async (t) => {
let infile = tmp.fileSync().name;
let plugin = buildPlugin({ infile, launchEditor: true });
try {
delete process.env.EDITOR;
process.env.PATH = '';
await runTasks(plugin);
} catch (error) {
t.deepEqual(plugin.commands, [
['git describe --tags --abbrev=0', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=v1.0.0`, { write: false }],
]);
t.is(
error.message,
`release-it-lerna-changelog configured to launch your editor but no editor was found (tried $EDITOR and searching $PATH for \`editor\`).`
);
} finally {
resetEDITOR();
resetPATH();
}
});
test('launches configured editor, updates infile, and propogates changes to context', async (t) => {
let fakeEditorFile = tmp.fileSync().name;
// using a function here so it is easier to author (vs a giant string interpolation)
function fakeEditor() {
let fs = require('fs');
let fileName = process.argv[2];
let contents = fs.readFileSync(fileName, { encoding: 'utf8' });
fs.writeFileSync(fileName, `${contents}\nExtra stuff!`, { encoding: 'utf8' });
}
fs.writeFileSync(fakeEditorFile, `${fakeEditor.toString()}\nfakeEditor();`, { encoding: 'utf8' });
let infile = tmp.fileSync().name;
let plugin = buildPlugin({
infile,
launchEditor: `${process.execPath} ${fakeEditorFile} \${file}`,
});
plugin.config.setContext({ git: { tagName: 'v${version}' } });
await runTasks(plugin);
const changelogFileContents = fs.readFileSync(infile);
t.is(
changelogFileContents.toString().trim(),
'## v1.0.1 (2020-03-18)\n\nThe changelog\nExtra stuff!'
);
const { changelog } = plugin.config.getContext();
t.is(changelog, 'The changelog\nExtra stuff!');
});