@j-ulrich/release-it-regex-bumper 3.0.1 → 3.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test.js DELETED
@@ -1,686 +0,0 @@
1
- const fs = require( 'fs' );
2
- const _ = require( 'lodash' );
3
- const dateFns = require( 'date-fns' );
4
- const crypto = require( 'crypto' );
5
- const assert = require( 'assert' ).strict;
6
- const test = require( 'bron' );
7
- const mockFs = require( 'mock-fs' );
8
- const rewiremock = require( 'rewiremock/node' );
9
- const { factory, runTasks } = require( 'release-it/test/util' );
10
- const semver = require( 'semver' );
11
- const releaseItVersion = semver.parse( require( 'release-it/package.json' ).version );
12
- const PluginWithoutDiff = rewiremock.proxy ( () => require( '.' ), mock => {
13
- return {
14
- 'diff': mock.with( null )
15
- };
16
- } );
17
- const Plugin = require( '.' );
18
-
19
- const namespace = '@j-ulrich/release-it-regex-bumper';
20
-
21
- mockFs();
22
-
23
- const readFile = ( file, encoding ) => fs.readFileSync( file ).toString( encoding );
24
- const writeFile = ( file, content, encoding ) => fs.writeFileSync( file, content, { encoding } );
25
-
26
-
27
- const setupTestDir = () => {
28
- const sizeOf32BitInt = 4; // 32 / 8
29
- const dirName = 'testDir_' + crypto.randomBytes( sizeOf32BitInt ).readUInt32LE( 0 );
30
- fs.mkdirSync( dirName );
31
- writeFile( dirName + '/versions.txt', 'some: 1.0.0\nthis: 1.0.1\nother: 2.0.0\n' );
32
- writeFile( dirName + '/VERSION', '1.0.1' );
33
- writeFile( dirName + '/copyright.txt', 'Copyright (c) 2019 Foo Bar' );
34
- writeFile( dirName + '/unrelated.txt', 'nothing to see here.' );
35
- return dirName;
36
- };
37
-
38
- const suitePrefixes = [];
39
-
40
- const describe = ( description, suiteFunc ) => {
41
- suitePrefixes.push( description );
42
- suiteFunc();
43
- suitePrefixes.pop();
44
- };
45
-
46
-
47
- const it = ( description, testFunc, options = {} ) => {
48
- const suitePrefix = suitePrefixes.length > 0 ? suitePrefixes.join( ' ' ) + ' ' : '';
49
- options.only = _.isNil( options.only ) ? false : options.only;
50
- options.skip = _.isNil( options.ski ) ? false : options.ski;
51
- const bronTestFunc = options.only ? test.only : options.skip ? test.skip : test;
52
- bronTestFunc( `${suitePrefix}${description}`, async () => {
53
- const testDir = setupTestDir();
54
- try {
55
- const result = await testFunc( testDir );
56
- return result;
57
- }
58
- catch( e ) {
59
- if ( e instanceof SkipException ) {
60
- console.warn( `⚠️ Skipped test '${description}'\n Reason: ${e.message}\n ${e.stack}` );
61
- return null;
62
- }
63
- throw e;
64
- }
65
- } );
66
- };
67
-
68
- const skip = ( message ) => {
69
- const skippedFunctionStackIndex = 2;
70
- const stackEntry = new Error().stack.split( '\n' )[ skippedFunctionStackIndex ]; // eslint-disable-line security/detect-object-injection
71
- throw new SkipException( message, stackEntry );
72
- };
73
-
74
- class SkipException {
75
- constructor( message, stack ) {
76
- this.message = message;
77
- this.stack = stack;
78
- }
79
- }
80
-
81
-
82
- const setupPlugin = ( pluginOptions, generalOptions, pluginModule = Plugin ) => {
83
- const options = Object.assign( { [namespace]: pluginOptions }, generalOptions );
84
- let container = {};
85
- const plugin = factory( pluginModule, { namespace, options, container } );
86
- return { plugin, container };
87
- };
88
-
89
- const assertLogMessage = ( logType, messageRegEx, failMessage ) => {
90
- assert( logType.args.findIndex( args => messageRegEx.test( args[ 0 ] ) ) > -1, failMessage );
91
- };
92
-
93
-
94
- //####### GetLatestVersion (Input) Tests #######
95
-
96
- describe( 'GetLatestVersion (Input)', () => {
97
-
98
- describe( 'error handling', () => {
99
-
100
- it( 'should throw if in file is not specified', async () => {
101
- const pluginOptions = { in: {} };
102
- const { plugin } = setupPlugin( pluginOptions );
103
- await assert.rejects( plugin.getLatestVersion(), new Error( "Missing 'file' property in 'in' options" ) );
104
- } );
105
-
106
- it( 'should throw if in file cannot be read', async () => {
107
- const pluginOptions = { in: 'file_which_does_not_exist.txt' };
108
- const { plugin } = setupPlugin( pluginOptions );
109
- await assert.rejects( plugin.getLatestVersion(), Error );
110
- } );
111
-
112
- it( 'should throw if version cannot be extracted', async ( testDir ) => {
113
- const pluginOptions = { in: testDir+'/unrelated.txt' };
114
- const { plugin } = setupPlugin( pluginOptions );
115
- await assert.rejects( plugin.getLatestVersion(), err => {
116
- assert( /could not extract version/i.test( err.message ) );
117
- return true;
118
- } );
119
- } );
120
-
121
- } );
122
-
123
- const testGetLatestVersion = async ( pluginOptions, expectedVersion ) => {
124
- const { plugin } = setupPlugin( pluginOptions );
125
- const actualVersion = await plugin.getLatestVersion();
126
- assert.equal( actualVersion, expectedVersion );
127
- };
128
-
129
- it( 'should return latest version from file', async ( testDir ) => {
130
- const options = { in: testDir+'/versions.txt' };
131
- await testGetLatestVersion( options, '1.0.0' );
132
- } );
133
-
134
- it( 'should return latest version from file using custom pattern', async ( testDir ) => {
135
- const options = { in: { file: testDir+'/versions.txt', search: 'this: ([0-9.]+)' } };
136
- await testGetLatestVersion( options, '1.0.1' );
137
- } );
138
-
139
- it( 'should return latest version from file using custom pattern with null version capture group', async ( testDir ) => {
140
- const options = { in: { file: testDir+'/versions.txt', search: { pattern: 'this: ([0-9.]+)', versionCaptureGroup: null } } };
141
- await testGetLatestVersion( options, '1.0.1' );
142
- } );
143
-
144
- it( 'should return latest version from file using custom pattern with null flags', async ( testDir ) => {
145
- const options = { in: { file: testDir+'/versions.txt', search: { pattern: 'this: ([0-9.]+)', flags: null } } };
146
- await testGetLatestVersion( options, '1.0.1' );
147
- } );
148
-
149
- it( 'should return latest version from file using null search', async ( testDir ) => {
150
- const options = { in: { file: testDir+'/versions.txt', search: null } };
151
- await testGetLatestVersion( options, '1.0.0' );
152
- } );
153
-
154
- it( 'should return latest version from file using custom pattern with named capturing group', async ( testDir ) => {
155
- const options = { in: { file: testDir+'/versions.txt', search: '(this): (?<version>[0-9.]+)' } };
156
- await testGetLatestVersion( options, '1.0.1' );
157
- } );
158
-
159
- it( 'should return latest version from file using custom pattern with multiple capturing groups', async ( testDir ) => {
160
- const options = { in: { file: testDir+'/versions.txt', search: { pattern: '(this): ([0-9.]+)', versionCaptureGroup: 2 } } };
161
- await testGetLatestVersion( options, '1.0.1' );
162
- } );
163
-
164
- it( 'should return latest version from file using custom pattern and versionCaptureGroup 0', async ( testDir ) => {
165
- const options = { in: { file: testDir+'/versions.txt', search: { pattern: '([0-9]+)\\.([0-9]+)\\.([0-9]+)', versionCaptureGroup: 0 } } };
166
- await testGetLatestVersion( options, '1.0.0' );
167
- } );
168
-
169
- it( 'should return latest version from file using custom pattern and named versionCaptureGroup', async ( testDir ) => {
170
- const options = { in: { file: testDir+'/versions.txt', search: { pattern: '(this): (?<special>[0-9.]+)', versionCaptureGroup: 'special' } } };
171
- await testGetLatestVersion( options, '1.0.1' );
172
- } );
173
-
174
- it( 'should return latest version from file using custom pattern with flags', async ( testDir ) => {
175
- const options = { in: { file: testDir+'/versions.txt', search: { pattern: 'THIS: ([0-9.]+)', flags: 'i' } } };
176
- await testGetLatestVersion( options, '1.0.1' );
177
- } );
178
-
179
- it( 'should return latest version from file using global pattern with flags', async ( testDir ) => {
180
- const options = { search: { pattern: 'THIS: ([0-9.]+)', flags: 'i' }, in: testDir+'/versions.txt' };
181
- await testGetLatestVersion( options, '1.0.1' );
182
- } );
183
-
184
- it( 'should return latest version from file with given encoding', async ( testDir ) => {
185
- writeFile( testDir+'/version.txt', '1.0.1', 'ucs2' );
186
- const options = { in: { file: testDir+'/version.txt', encoding: 'ucs2' } };
187
- await testGetLatestVersion( options, '1.0.1' );
188
- } );
189
-
190
- describe( 'search placeholders', () => {
191
-
192
- it( 'should find the current date', async ( testDir ) => {
193
- writeFile( testDir+'/version.txt', `1.0.1 - [${dateFns.format( new Date(), 'yyyy-MM-dd' )}]` );
194
- const options = { in: { file: testDir+'/version.txt', search: '([0-9.]+) - \\[{{now:yyyy-MM-dd}}\\]' } };
195
- await testGetLatestVersion( options, '1.0.1' );
196
- } );
197
-
198
- it( 'should find a semantic version', async ( testDir ) => {
199
- writeFile( testDir+'/version.txt', '2.2 - 1.0.1' );
200
- const options = { in: { file: testDir+'/version.txt', search: '{{semver}}' } };
201
- await testGetLatestVersion( options, '1.0.1' );
202
- } );
203
-
204
- it( 'should find a literal placeholder', async ( testDir ) => {
205
- writeFile( testDir+'/version.txt', '{{foo}} - 1.0.1' );
206
- const options = { in: { file: testDir+'/version.txt', search: '{{{}}{foo}} - ([0-9.]+)' } };
207
- await testGetLatestVersion( options, '1.0.1' );
208
- } );
209
-
210
- it( 'should throw if there are unknown placeholders', async ( testDir ) => {
211
- const options = { in: { file: testDir+'/versions.txt', search: '{{version}}' } };
212
- await assert.rejects( testGetLatestVersion( options, '1.0.1' ),
213
- new Error( 'Unknown placeholder encountered: {{version}}' ) );
214
- } );
215
-
216
- it( 'should throw if there are malicious placeholders', async ( testDir ) => {
217
- const options = { in: { file: testDir+'/versions.txt', search: '{{constructor}}' } };
218
- await assert.rejects( testGetLatestVersion( options, '1.0.1' ),
219
- new Error( 'Unknown placeholder encountered: {{constructor}}' ) );
220
- } );
221
-
222
- it( 'should throw if the format is missing in "now" placeholder', async ( testDir ) => {
223
- const options = { in: { file: testDir+'/versions.txt', search: '{{now}}' } };
224
- await assert.rejects( testGetLatestVersion( options, '1.0.1' ),
225
- new Error( "Missing required parameter 'format' for placeholder {{now}}" ) );
226
- } );
227
-
228
- } );
229
-
230
- } );
231
-
232
-
233
-
234
- //####### Bump (Output) Tests #######
235
-
236
- describe( 'Bump (Output)', () => {
237
-
238
- const testBump = async ( testDir, pluginOptions, expectedContent, newVersion='1.2.3' ) => {
239
- const { plugin } = setupPlugin( pluginOptions );
240
- await plugin.bump( newVersion );
241
- assert.equal( readFile( testDir+'/versions.txt' ), expectedContent );
242
- };
243
-
244
- it( 'should write version to file', async ( testDir ) => {
245
- const pluginOptions = { out: testDir+'/versions.txt' };
246
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.0.1\nother: 2.0.0\n' );
247
- } );
248
-
249
- it( 'should write version to file using null search and replace', async ( testDir ) => {
250
- const pluginOptions = { out: { file: testDir+'/versions.txt', search: null, replace: null } };
251
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.0.1\nother: 2.0.0\n' );
252
- } );
253
-
254
- it( 'should throw if out file cannot be read', async () => {
255
- const pluginOptions = { out: 'file_which_does_not_exist.txt' };
256
- const { plugin } = setupPlugin( pluginOptions );
257
- await assert.rejects( plugin.bump( '1.2.3' ), Error );
258
- } );
259
-
260
- it( 'should warn if out file did not change', async ( testDir ) => {
261
- const pluginOptions = { out: testDir+'/unrelated.txt' };
262
- const { plugin, container } = setupPlugin( pluginOptions );
263
- await assert.doesNotReject( plugin.bump( '1.2.3' ) );
264
- assert.equal( readFile( testDir+'/unrelated.txt' ), 'nothing to see here.' );
265
- assert( container.log.warn.called, 'No warning was logged' );
266
- assertLogMessage( container.log.warn, /\/unrelated\.txt" did not change/, 'warning regarding unchanged file was not logged' );
267
- } );
268
-
269
-
270
- const testBumpThisVersion = async ( testDir, pluginOptions ) => {
271
- await testBump( testDir, pluginOptions, 'some: 1.0.0\nthis: 1.2.3\nother: 2.0.0\n' );
272
- };
273
-
274
- describe( 'global options', () => {
275
-
276
- it( 'should replace matches using global search pattern and overridden search flags', async ( testDir ) => {
277
- const pluginOptions = { out: { file: testDir+'/versions.txt', search: { flags: 'g' } } };
278
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.2.3\nother: 1.2.3\n' );
279
- } );
280
-
281
- it( 'should replace matches using global search flags', async ( testDir ) => {
282
- const pluginOptions = { out: { file: testDir+'/versions.txt' }, search: { flags: 'g' } };
283
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.2.3\nother: 1.2.3\n' );
284
- } );
285
-
286
- it( 'should replace matches using overridden search flags', async ( testDir ) => {
287
- const pluginOptions = { out: { file: testDir+'/versions.txt', search: { flags: '' } }, search: { flags: 'g' } };
288
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.0.1\nother: 2.0.0\n' );
289
- } );
290
-
291
- it( 'should write version to file using global pattern', async ( testDir ) => {
292
- const pluginOptions = { search: '(?<=this: )([0-9.]+)', out: { file: testDir+'/versions.txt' } };
293
- await testBumpThisVersion( testDir, pluginOptions );
294
- } );
295
-
296
- it( 'should write version to file using global pattern with flags', async ( testDir ) => {
297
- const pluginOptions = { search: { pattern: '(?<=THIS: )([0-9.]+)', flags: 'i' }, out: testDir+'/versions.txt' };
298
- await testBumpThisVersion( testDir, pluginOptions );
299
- } );
300
-
301
- it( 'should write version to file using global pattern and global replace', async ( testDir ) => {
302
- const pluginOptions = { search: '(this:) ([0-9.]+)', replace: '$1 {{version}}', out: testDir+'/versions.txt' };
303
- await testBumpThisVersion( testDir, pluginOptions );
304
- } );
305
-
306
- } );
307
-
308
- describe( 'out options', () => {
309
-
310
- it( 'should write version to file using custom pattern', async ( testDir ) => {
311
- const pluginOptions = { out: { file: testDir+'/versions.txt', search: '(?<=this: )([0-9.]+)' } };
312
- await testBumpThisVersion( testDir, pluginOptions );
313
- } );
314
-
315
- it( 'should write version to file using custom pattern with flags', async ( testDir ) => {
316
- const pluginOptions = { out: { file: testDir+'/versions.txt', search: { pattern: '(?<=THIS: )([0-9.]+)', flags: 'i' } } };
317
- await testBumpThisVersion( testDir, pluginOptions );
318
- } );
319
-
320
- it( 'should write version to file using custom pattern and custom replace', async ( testDir ) => {
321
- const pluginOptions = { out: { file: testDir+'/versions.txt', search: 'this: [0-9.]+', replace: 'this: {{version}}' } };
322
- await testBumpThisVersion( testDir, pluginOptions );
323
- } );
324
-
325
- it( 'should write version to file using custom pattern and custom replace with capturing group', async ( testDir ) => {
326
- const pluginOptions = { out: { file: testDir+'/versions.txt', search: '(this:) [0-9.]+', replace: '$1 {{version}}' } };
327
- await testBumpThisVersion( testDir, pluginOptions );
328
- } );
329
-
330
- it( 'should write version to file using custom pattern and custom replace with named capturing group', async ( testDir ) => {
331
- const pluginOptions = { out: { file: testDir+'/versions.txt', search: '(?<prefix>this:) [0-9.]+', replace: '${prefix} {{version}}' } };
332
- await testBumpThisVersion( testDir, pluginOptions );
333
- } );
334
-
335
- it( 'should write version to all matches in file', async ( testDir ) => {
336
- const pluginOptions = { out: { file: testDir+'/versions.txt', search: { pattern: '([0-9.]+)', flags: 'g' } } };
337
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.2.3\nother: 1.2.3\n' );
338
- } );
339
-
340
- it( 'should write version to different matches in same file', async ( testDir ) => {
341
- const pluginOptions = { out: [ { file: testDir+'/versions.txt', search: '(?<=this: )([0-9.]+)' }, { file: testDir+'/versions.txt', search: '(?<=other: )([0-9.]+)' } ] };
342
- await testBump( testDir, pluginOptions, 'some: 1.0.0\nthis: 1.2.3\nother: 1.2.3\n' );
343
- } );
344
-
345
- it( 'should write version to multiple files', async ( testDir ) => {
346
- const pluginOptions = { out: [ testDir+'/versions.txt', testDir+'/VERSION' ] };
347
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.0.1\nother: 2.0.0\n' );
348
- assert.equal( readFile( testDir+'/VERSION' ), '1.2.3' );
349
- } );
350
-
351
- it( 'should write version to multiple files using glob pattern', async ( testDir ) => {
352
- writeFile( testDir + '/version.txt', '1.0.1' );
353
- const pluginOptions = { out: [ testDir+'/version*.txt', testDir+'/VERSION' ] };
354
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.0.1\nother: 2.0.0\n' );
355
- assert.equal( readFile( testDir+'/version.txt' ), '1.2.3' );
356
- assert.equal( readFile( testDir+'/VERSION' ), '1.2.3' );
357
- } );
358
-
359
- it( 'should write version to multiple files using same options', async ( testDir ) => {
360
- const pluginOptions = { out: { files: [ testDir+'/versions.txt', testDir+'/VERSION' ], replace: '{{version}}-dev' } };
361
- await testBump( testDir, pluginOptions, 'some: 1.2.3-dev\nthis: 1.0.1\nother: 2.0.0\n' );
362
- assert.equal( readFile( testDir+'/VERSION' ), '1.2.3-dev' );
363
- } );
364
-
365
- it( 'should write version to multiple files using glob pattern and same options', async ( testDir ) => {
366
- writeFile( testDir + '/version.txt', '1.0.1' );
367
- const pluginOptions = { out: { files: [ testDir+'/version*.txt', testDir+'/VERSION' ], replace: '{{version}}-dev' } };
368
- await testBump( testDir, pluginOptions, 'some: 1.2.3-dev\nthis: 1.0.1\nother: 2.0.0\n' );
369
- assert.equal( readFile( testDir+'/version.txt' ), '1.2.3-dev' );
370
- assert.equal( readFile( testDir+'/VERSION' ), '1.2.3-dev' );
371
- } );
372
-
373
- it( 'should write version to out.file and out.files', async ( testDir ) => {
374
- const pluginOptions = { out: { file: testDir+'/versions.txt', files: testDir+'/VERSION' } };
375
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.0.1\nother: 2.0.0\n' );
376
- assert.equal( readFile( testDir+'/VERSION' ), '1.2.3' );
377
- } );
378
-
379
- it( 'should write version to out.file if out.files is null', async ( testDir ) => {
380
- const pluginOptions = { out: { file: testDir+'/versions.txt', files: null } };
381
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.0.1\nother: 2.0.0\n' );
382
- } );
383
-
384
- it( 'should write version to out.files if out.file is null', async ( testDir ) => {
385
- const pluginOptions = { out: { file: null, files: testDir+'/versions.txt' } };
386
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.0.1\nother: 2.0.0\n' );
387
- } );
388
-
389
- it( 'should write version to multiple files using different patterns', async ( testDir ) => {
390
- const pluginOptions = { out: [ { file: testDir+'/versions.txt', search: '(?<=this: )([0-9.]+)' }, testDir+'/VERSION' ] };
391
- await testBump( testDir, pluginOptions, 'some: 1.0.0\nthis: 1.2.3\nother: 2.0.0\n' );
392
- assert.equal( readFile( testDir+'/VERSION' ), '1.2.3' );
393
- } );
394
-
395
- } );
396
-
397
- describe( 'search placeholders', () => {
398
-
399
- it( 'should find the current version', async ( testDir ) => {
400
- const pluginOptions = { latestVersion: '1.0.1', out: { file: testDir+'/versions.txt', search: '(?<=this: ){{version}}' } };
401
- await testBumpThisVersion( testDir, pluginOptions );
402
- } );
403
-
404
- it( 'should find main version parts', async ( testDir ) => {
405
- const pluginOptions = { latestVersion: '1.0.1', out: { file: testDir+'/versions.txt', search: '{{major}}\\.{{minor}}\\.{{patch}}' } };
406
- await testBumpThisVersion( testDir, pluginOptions );
407
- } );
408
-
409
- it( 'should find secondary version parts', async ( testDir ) => {
410
- const pluginOptions = { latestVersion: '1.0.1-alpha.3+build.12', out: { file: testDir+'/versions.txt',
411
- search: '{{major}}\\.{{minor}}\\.{{patch}}-{{prerelease}}\\+{{build}}' } };
412
- writeFile( testDir + '/versions.txt', `some: 1.0.0\nthis: ${pluginOptions.latestVersion}\nother: 2.0.0\n` );
413
- await testBumpThisVersion( testDir, pluginOptions );
414
- } );
415
-
416
- it( 'should find prefixed secondary version parts', async ( testDir ) => {
417
- const pluginOptions = { latestVersion: '1.0.1-alpha.3+build.12', out: { file: testDir+'/versions.txt',
418
- search: '{{major}}\\.{{minor}}\\.{{patch}}{{prefixedPrerelease}}{{prefixedBuild}}' } };
419
- writeFile( testDir + '/versions.txt', `some: 1.0.0\nthis: ${pluginOptions.latestVersion}\nother: 2.0.0\n` );
420
- await testBumpThisVersion( testDir, pluginOptions );
421
- } );
422
-
423
- it( 'should find custom version representations', async ( testDir ) => {
424
- const pluginOptions = { latestVersion: '1.0.1-alpha.3+build.12', out: { file: testDir+'/versions.txt',
425
- search: '{{versionWithoutPrerelease}}/{{versionWithoutBuild}}' } };
426
- writeFile( testDir + '/versions.txt', 'some: 1.0.0\nthis: 1.0.1/1.0.1-alpha.3\nother: 2.0.0\n' );
427
- await testBumpThisVersion( testDir, pluginOptions );
428
- } );
429
-
430
- it( 'should find a literal placeholder', async ( testDir ) => {
431
- const pluginOptions = { latestVersion: '1.0.1', out: { file: testDir+'/versions.txt',
432
- search: '{{{}}{placeholder}}' } };
433
- writeFile( testDir + '/versions.txt', 'some: 1.0.0\nthis: {{placeholder}}\nother: 2.0.0\n' );
434
- await testBumpThisVersion( testDir, pluginOptions );
435
- } );
436
-
437
- it( 'should find the current date', async ( testDir ) => {
438
- const pluginOptions = { latestVersion: '1.0.1', out: { file: testDir+'/versions.txt',
439
- search: '{{now:yyyy-MM-dd}}' } };
440
- writeFile( testDir + '/versions.txt', `some: 1.0.0\nthis: ${dateFns.format( new Date(), 'yyyy-MM-dd' )}\nother: 2.0.0\n` );
441
- await testBumpThisVersion( testDir, pluginOptions );
442
- } );
443
-
444
- it( 'should find a semantic version', async ( testDir ) => {
445
- const pluginOptions = { latestVersion: '1.0.1', out: { file: testDir+'/versions.txt', search: '{{semver}}' } };
446
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.0.1\nother: 2.0.0\n' );
447
- } );
448
-
449
- it( 'should find the new version', async ( testDir ) => {
450
- const pluginOptions = { latestVersion: '1.0.1', out: { file: testDir+'/versions.txt',
451
- search: '{{newVersion}}' } };
452
- writeFile( testDir + '/versions.txt', 'some: 1.0.0\nthis: 1.2.3\nother: 2.0.0\n' );
453
- await testBumpThisVersion( testDir, pluginOptions );
454
- } );
455
-
456
- it( 'should find the new version', async ( testDir ) => {
457
- const pluginOptions = { latestVersion: '1.0.1', latestTag: 'foo', out: { file: testDir+'/versions.txt',
458
- search: '{{tag}}' } };
459
- writeFile( testDir + '/versions.txt', 'some: 1.0.0\nthis: foo\nother: 2.0.0\n' );
460
- await testBumpThisVersion( testDir, pluginOptions );
461
- } );
462
-
463
- } );
464
-
465
- describe( 'replace placeholders', () => {
466
-
467
- it( 'should write date to file', async ( testDir ) => {
468
- const pluginOptions = { out: { file: testDir+'/copyright.txt', search: '\\d{4}', replace: '{{now}}' } };
469
- const { plugin } = setupPlugin( pluginOptions );
470
- await plugin.bump( '1.2.3' );
471
- const writeTime = new Date();
472
- const fileContent = await readFile( testDir+'/copyright.txt' );
473
- const fileContentMatch = /^Copyright \(c\) (.+) Foo Bar$/.exec( fileContent );
474
- assert( fileContentMatch );
475
- assert( fileContentMatch[ 1 ] );
476
- const parsedDate = dateFns.parseISO( fileContentMatch[ 1 ] );
477
- assert( dateFns.isValid( parsedDate ) );
478
- const dateThresholdSeconds = 5;
479
- assert( dateFns.differenceInSeconds( writeTime, parsedDate ) < dateThresholdSeconds );
480
- } );
481
-
482
- it( 'should write date to file using format', async ( testDir ) => {
483
- const pluginOptions = { out: { file: testDir+'/copyright.txt', search: '\\d{4}', replace: '{{now:yyyy}}' } };
484
- const { plugin } = setupPlugin( pluginOptions );
485
- await plugin.bump( '1.2.3' );
486
- assert.equal( readFile( testDir+'/copyright.txt' ), `Copyright (c) ${dateFns.format( new Date(), 'yyyy' )} Foo Bar` );
487
- } );
488
-
489
- it( 'should throw when using Moment.js date format', async ( testDir ) => {
490
- const pluginOptions = { out: { file: testDir+'/copyright.txt', search: '\\d{4}', replace: '{{now:YYYY}}' } };
491
- const { plugin } = setupPlugin( pluginOptions );
492
- await assert.rejects( plugin.bump( '1.2.3' ), RangeError );
493
- } );
494
-
495
- it( 'should write main version placeholders to file', async ( testDir ) => {
496
- const pluginOptions = { out: { file: testDir+'/versions.txt', replace: '{{major}}.{{minor}}.{{patch}}' } };
497
- await testBump( testDir, pluginOptions, 'some: 1.2.3\nthis: 1.0.1\nother: 2.0.0\n', '1.2.3-alpha.1+build.17' );
498
- } );
499
-
500
- it( 'should write secondary version placeholders to file', async ( testDir ) => {
501
- const pluginOptions = { out: { file: testDir+'/versions.txt', replace: '-{{prerelease}}+{{build}}' } };
502
- await testBump( testDir, pluginOptions, 'some: -alpha.1+build.17\nthis: 1.0.1\nother: 2.0.0\n', '1.2.3-alpha.1+build.17' );
503
- } );
504
-
505
- it( 'should write prefixed secondary version placeholders to file', async ( testDir ) => {
506
- const pluginOptions = { out: { file: testDir+'/versions.txt', replace: '{{prefixedPrerelease}}{{prefixedBuild}}' } };
507
- await testBump( testDir, pluginOptions, 'some: -alpha.1+build.17\nthis: 1.0.1\nother: 2.0.0\n', '1.2.3-alpha.1+build.17' );
508
- } );
509
-
510
- it( 'should write custom version placeholders to file', async ( testDir ) => {
511
- const pluginOptions = { out: { file: testDir+'/versions.txt', replace: '{{versionWithoutPrerelease}}/{{versionWithoutBuild}}' } };
512
- await testBump( testDir, pluginOptions, 'some: 1.2.3/1.2.3-alpha.1\nthis: 1.0.1\nother: 2.0.0\n', '1.2.3-alpha.1+build.17' );
513
- } );
514
-
515
- it( 'should write empty strings for empty placeholders to file', async ( testDir ) => {
516
- const pluginOptions = { out: { file: testDir+'/versions.txt', replace: '-{{prerelease}}+{{build}}/{{prefixedPrerelease}}{{prefixedBuild}}' } };
517
- await testBump( testDir, pluginOptions, 'some: -+/\nthis: 1.0.1\nother: 2.0.0\n' );
518
- } );
519
-
520
- it( 'should throw if there are unknown placeholders', async ( testDir ) => {
521
- const pluginOptions = { out: { file: testDir+'/versions.txt', replace: '{{foo}}.{{bar}}' } };
522
- const { plugin } = setupPlugin( pluginOptions );
523
- await assert.rejects( plugin.bump( '1.2.3' ), new Error( 'Unknown placeholder encountered: {{foo}}' ) );
524
- } );
525
-
526
- it( 'should throw if there are malicious placeholders', async ( testDir ) => {
527
- const pluginOptions = { out: { file: testDir+'/versions.txt', replace: '{{constructor}}' } };
528
- const { plugin } = setupPlugin( pluginOptions );
529
- await assert.rejects( plugin.bump( '1.2.3' ), new Error( 'Unknown placeholder encountered: {{constructor}}' ) );
530
- } );
531
-
532
- it( 'should write literal curly brace to file', async ( testDir ) => {
533
- const pluginOptions = { out: { file: testDir+'/versions.txt', replace: '{{{}}{foo}}' } };
534
- await testBump( testDir, pluginOptions, 'some: {{foo}}\nthis: 1.0.1\nother: 2.0.0\n', '1.2.3-alpha.1+build.17' );
535
- } );
536
-
537
- it( 'should write literal curly brace to file', async ( testDir ) => {
538
- const pluginOptions = { out: { file: testDir+'/versions.txt', replace: '{{{}}{foo}}' } };
539
- await testBump( testDir, pluginOptions, 'some: {{foo}}\nthis: 1.0.1\nother: 2.0.0\n', '1.2.3-alpha.1+build.17' );
540
- } );
541
-
542
- } );
543
-
544
- describe( 'encoding option', () => {
545
-
546
- it( 'should write version to file with given encoding', async ( testDir ) => {
547
- writeFile( testDir+'/version.txt', '1.0.1', 'ucs2' );
548
- const pluginOptions = { out: { file: testDir+'/version.txt', encoding: 'ucs2' } };
549
- const { plugin } = setupPlugin( pluginOptions );
550
- await plugin.bump( '1.2.3' );
551
- assert.equal( readFile( testDir+'/version.txt', 'ucs2' ), '1.2.3' );
552
- } );
553
-
554
- } );
555
-
556
- describe( 'dry run', () => {
557
-
558
- const testDryRunBump = async ( testDir, pluginOptions ) => {
559
- const { plugin, container } = setupPlugin( pluginOptions, { 'dry-run': true } );
560
- await assert.doesNotReject( plugin.bump( '1.2.3' ) );
561
- assert.equal( readFile( testDir+'/versions.txt' ), 'some: 1.0.0\nthis: 1.0.1\nother: 2.0.0\n' );
562
- assert.equal( readFile( testDir+'/VERSION' ), '1.0.1' );
563
- assert.equal( readFile( testDir+'/unrelated.txt' ), 'nothing to see here.' );
564
- return container;
565
- };
566
-
567
- it( 'should not write', async ( testDir ) => {
568
- const pluginOptions = { out: testDir+'/versions.txt' };
569
- const container = await testDryRunBump( testDir, pluginOptions );
570
- assert( !container.log.warn.called, `Unexpected warnings: ${container.log.warn.args}` );
571
- assert( container.log.exec.called, 'no diff was logged' );
572
- assertLogMessage( container.log.exec, /-some: 1\.0\.0/ );
573
- assertLogMessage( container.log.exec, /\+some: 1\.2\.3/ );
574
- } );
575
-
576
- it( 'should report all changes', async ( testDir ) => {
577
- const pluginOptions = { search: { pattern: '([0-9.]+)', flags: 'g' }, out: [ testDir+'/versions.txt', testDir+'/VERSION' ] };
578
- const container = await testDryRunBump( testDir, pluginOptions );
579
- assert( container.log.exec.called, 'no diff was logged' );
580
- assertLogMessage( container.log.info, /Updating .*\/versions.txt/ );
581
- assertLogMessage( container.log.exec, /-some: 1\.0\.0/ );
582
- assertLogMessage( container.log.exec, /\+some: 1\.2\.3/ );
583
- assertLogMessage( container.log.exec, /-this: 1\.0\.1/ );
584
- assertLogMessage( container.log.exec, /\+this: 1\.2\.3/ );
585
- assertLogMessage( container.log.exec, /-other: 2\.0\.0/ );
586
- assertLogMessage( container.log.exec, /\+other: 1\.2\.3/ );
587
- assertLogMessage( container.log.info, /Updating .*\/VERSION/ );
588
- assertLogMessage( container.log.exec, /-1\.0\.1/ );
589
- assertLogMessage( container.log.exec, /\+1\.2\.3/ );
590
- } );
591
-
592
- it( 'should warn in dry run if out file would not change', async ( testDir ) => {
593
- const pluginOptions = { out: testDir+'/unrelated.txt' };
594
- const container = await testDryRunBump( testDir, pluginOptions );
595
- assert( container.log.warn.called, 'no warnings were logged' );
596
- assertLogMessage( container.log.warn, /\/unrelated\.txt" did not change/, 'warning regarding unchanged file was not logged' );
597
- } );
598
-
599
- it( 'should report all changes in dry run without diff', async ( testDir ) => {
600
- const pluginOptions = { search: { pattern: '([0-9.]+)', flags: 'g' }, out: [ testDir+'/versions.txt', testDir+'/VERSION' ] };
601
- const { plugin, container } = setupPlugin( pluginOptions, { 'dry-run': true }, PluginWithoutDiff );
602
- await plugin.bump( '1.2.3' );
603
- assert.equal( readFile( testDir+'/versions.txt' ), 'some: 1.0.0\nthis: 1.0.1\nother: 2.0.0\n' );
604
- assert.equal( readFile( testDir+'/VERSION' ), '1.0.1' );
605
- assert( container.log.exec.called, 'no diff was logged' );
606
- assertLogMessage( container.log.info, /Updating .*\/versions.txt/ );
607
- assertLogMessage( container.log.exec, /.*line 1[\s\S]*1\.0\.0/ );
608
- assertLogMessage( container.log.exec, /.*line 2[\s\S]*1\.0\.1/ );
609
- assertLogMessage( container.log.exec, /.*line 3[\s\S]*2\.0\.0/ );
610
- assertLogMessage( container.log.info, /Updating .*\/VERSION/ );
611
- assertLogMessage( container.log.exec, /.*line 1[\s\S]*1\.0\.1/ );
612
- } );
613
-
614
- it( 'should warn in dry run without diff if out file would not change', async ( testDir ) => {
615
- const pluginOptions = { out: testDir+'/unrelated.txt' };
616
- const { plugin, container } = setupPlugin( pluginOptions, { 'dry-run': true }, PluginWithoutDiff );
617
- await assert.doesNotReject( plugin.bump( '1.2.3' ) );
618
- assert.equal( readFile( testDir+'/unrelated.txt' ), 'nothing to see here.' );
619
- assert( container.log.warn.called, 'no warnings were logged' );
620
- assertLogMessage( container.log.warn, /\/unrelated\.txt" did not change/, 'warning regarding unchanged file was not logged' );
621
- } );
622
-
623
- } );
624
-
625
- } );
626
-
627
-
628
- describe( 'End-to-End', () => {
629
-
630
- const runPlugin = pluginOptions => {
631
- const { plugin } = setupPlugin( pluginOptions );
632
- return runTasks( plugin );
633
- };
634
-
635
- it( 'should not throw if nothing is configured', async () => {
636
- const pluginOptions = {};
637
- await assert.doesNotReject( runPlugin( pluginOptions ) );
638
- } );
639
-
640
- it( 'should not throw if in and out is null', async () => {
641
- const pluginOptions = { in: null, out: null };
642
- await assert.doesNotReject( runPlugin( pluginOptions ) );
643
- } );
644
-
645
- it( 'should read and write same file', async ( testDir ) => {
646
- const pluginOptions = { in: testDir+'/VERSION', out: testDir+'/VERSION' };
647
- await runPlugin( pluginOptions );
648
- assert.equal( readFile( testDir+'/VERSION' ), '1.0.2' );
649
- } );
650
-
651
- it( 'should read and write different files', async ( testDir ) => {
652
- const pluginOptions = { in: testDir+'/VERSION', out: testDir+'/versions.txt' };
653
- await runPlugin( pluginOptions );
654
- assert.equal( readFile( testDir+'/VERSION' ), '1.0.1' );
655
- assert.equal( readFile( testDir+'/versions.txt' ), 'some: 1.0.2\nthis: 1.0.1\nother: 2.0.0\n' );
656
- } );
657
-
658
- it( 'should read and write multiple files', async ( testDir ) => {
659
- const pluginOptions = { in: testDir+'/VERSION', out: [ testDir+'/VERSION', { file: testDir+'/versions.txt', search: 'this: [0-9.]+', replace: 'this: {{version}}' } ] };
660
- await runPlugin( pluginOptions );
661
- assert.equal( readFile( testDir+'/VERSION' ), '1.0.2' );
662
- assert.equal( readFile( testDir+'/versions.txt' ), 'some: 1.0.0\nthis: 1.0.2\nother: 2.0.0\n' );
663
- } );
664
-
665
- it( 'should write latest version to file', async ( testDir ) => {
666
- const pluginOptions = { in: testDir+'/VERSION', out: { file: testDir+'/versions.txt', replace: '{{latestVersion}}' } };
667
- await runPlugin( pluginOptions );
668
- assert.equal( readFile( testDir+'/versions.txt' ), 'some: 1.0.1\nthis: 1.0.1\nother: 2.0.0\n' );
669
- } );
670
-
671
- it( 'should write latest tag to file', async ( testDir ) => {
672
- if( semver.lt( releaseItVersion, '13.5.8' ) ) {
673
- skip( '`latestTag` is not available in tests before release-it 13.5.8' );
674
- }
675
- const pluginOptions = { in: testDir+'/VERSION', out: { file: testDir+'/versions.txt', replace: '{{latestTag}}' } };
676
- await runPlugin( pluginOptions );
677
- assert.equal( readFile( testDir+'/versions.txt' ), 'some: 1.0.1\nthis: 1.0.1\nother: 2.0.0\n' );
678
- } );
679
-
680
- it( 'should replace the current version', async ( testDir ) => {
681
- const pluginOptions = { in: testDir+'/VERSION', out: { file: testDir+'/versions.txt', search: '{{version}}' } };
682
- await runPlugin( pluginOptions );
683
- assert.equal( readFile( testDir+'/versions.txt' ), 'some: 1.0.0\nthis: 1.0.2\nother: 2.0.0\n' );
684
- } );
685
-
686
- } );