forked from finn-no/maven-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
477 lines (388 loc) · 16 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* globals describe, it, beforeEach, afterEach */
/*jshint expr: true*/
var assert = require('assert');
var path = require('path');
var sinon = require('sinon');
var extend = require('util-extend');
var proxyquire = require('proxyquire');
var fsMock = require('mock-fs');
var fsReal = require('fs');
var semver = require('semver');
var JSZip = require('jszip');
var bufferEqual = require('buffer-equal');
var maven, fs;
var BINARY_FILE = fsReal.readFileSync('px.png');
var lastCmd, cmdCallback;
const GROUP_ID = 'com.dummy',
TEST_CLASSIFIER = 'test',
DUMMY_REPO_SNAPSHOT = {
'id': 'dummy-repo',
'url': 'http://mavendummyrepo.com/dummy/'
},
DUMMY_REPO_RELEASE = {
'id': 'dummy-repo-release',
'url': 'http://mavendummyrepo.com/dummy-release/'
},
TEST_CONFIG = {
groupId: GROUP_ID,
repositories: [DUMMY_REPO_SNAPSHOT, DUMMY_REPO_RELEASE],
classifier: TEST_CLASSIFIER,
generatePom: false
},
TEST_CONFIG_WITH_SPACES = {
groupId: GROUP_ID,
repositories: [DUMMY_REPO_SNAPSHOT, DUMMY_REPO_RELEASE],
classifier: TEST_CLASSIFIER,
generatePom: false,
buildDir: 'dist with spaces' //build directory has spaces in it
},
TEST_PKG_JSON = {
name: 'test-pkg',
version: '1.0.0'
},
TEST_CONFIG_WITH_POM = {
groupId: GROUP_ID,
repositories: [DUMMY_REPO_SNAPSHOT, DUMMY_REPO_RELEASE],
classifier: TEST_CLASSIFIER,
generatePom: false,
pomFile: 'existing-pom.xml'
};
var childProcessMock;
var execSpy;
function createFakeFS () {
var fakeFS = fsMock.fs({
'package.json': JSON.stringify(TEST_PKG_JSON),
'dist': {
'js': {
'index.js': 'console.log("test")',
},
'README.md': '## README\nlorum ipsum',
'px.png': BINARY_FILE
},
//a directory with spaces in the path
'dist with spaces': {
'js': {
'index.js': 'console.log("test")',
},
'README.md': '## README\nlorum ipsum',
'px.png': BINARY_FILE
},
});
return fakeFS;
}
function npmVersion (next) {
var fileName = 'package.json';
var pkg = JSON.parse(fs.readFileSync(fileName), {encoding: 'utf-8'});
pkg.version = next;
fs.writeFileSync(fileName, JSON.stringify(pkg), {encoding: 'utf-8'});
}
function warFileInDist () {
return fs.readdirSync('./dist/').filter(function (fileName) {
return /\.war$/.test(fileName);
})[0];
}
function warFileInDistAsZip () {
var pathToWarFile = warFileInDist();
var context = fs.readFileSync('./dist/' + pathToWarFile);
return new JSZip(context);
}
function assertWarFileToEqual (expectedName) {
var warFile = warFileInDist();
assert.ok(warFile);
assert.equal(warFile, expectedName);
}
function arrayContains (arr, value) {
return arr.indexOf(value) !== -1;
}
function assertArgs (cmd, expectedArgs) {
var actualArgs = cmd.split(/([^"\s]*".*?"|[^"\s]+)+(?=\s*|\s*$)/g); //split based on spaces that are not wrapped in quotes
expectedArgs.forEach(function (expectedArg) {
assert.ok(arrayContains(actualArgs, expectedArg), expectedArg + ' should be part of the command: ' + cmd);
});
}
function assertNotArgs (cmd, unexpectedArgs) {
var actualArgs = cmd.split(/\s+/);
unexpectedArgs.forEach(function (expectedArg) {
assert.ok(!arrayContains(actualArgs, expectedArg), expectedArg + ' should not be part of the command: ' + cmd);
});
}
describe('maven-deploy', function () {
beforeEach(function () {
lastCmd = undefined;
cmdCallback = undefined;
childProcessMock = {
exec: sinon.spy(),
spawn: sinon.spy(),
fork: sinon.spy()
};
execSpy = childProcessMock.exec;
fs = createFakeFS();
maven = proxyquire('./index.js', {
'child_process': childProcessMock,
'fs': fs,
'fs-walk': proxyquire('fs-walk', {'fs': fs}),
'isbinaryfile': proxyquire('isbinaryfile', {'fs': fs})
});
});
afterEach(function () {
fs = null;
childProcessMock = null;
});
describe('config', function () {
it('should throw an error if groupId is missing', function () {
assert.throws(function () {
maven.config({
repositories: [DUMMY_REPO_SNAPSHOT]
});
});
});
});
describe('versioning', function() {
it('semver release type passed in for releases', function () {
const EXPECTED_VERSION = '1.0.1';
maven.config( extend({finalName: '{name}-{version}', semver: 'patch'}, TEST_CONFIG) );
maven.package();
assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});
it('semver release type passed in for snapshots', function () {
const EXPECTED_VERSION = '1.1.0-SNAPSHOT';
maven.config( extend({finalName: '{name}-{version}', semver: 'minor'}, TEST_CONFIG) );
maven.package(true);
assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});
it('semver prerelease types for releases', function () {
const EXPECTED_VERSION = '1.0.1-beta-0';
maven.config( extend({finalName: '{name}-{version}', semver: ['prerelease', 'beta']}, TEST_CONFIG) );
maven.package();
assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});
it('semver prerelease build number qualifiers', function () {
const EXPECTED_VERSION = '1.2.3-6';
npmVersion('1.2.3-5');
maven.config( extend({finalName: '{name}-{version}', semver: ['prerelease']}, TEST_CONFIG) );
maven.package();
assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});
it('semver replacing prerelease build numbers with snapshots', function () {
const EXPECTED_VERSION = '1.2.3-alpha-SNAPSHOT';
npmVersion('1.2.3-alpha.5');
maven.config( extend({finalName: '{name}-{version}', semver: ['prerelease', 'alpha']}, TEST_CONFIG) );
maven.package(true);
assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});
});
describe('package', function () {
it('should create an archive based on defaults', function () {
maven.config(TEST_CONFIG);
maven.package();
assertWarFileToEqual(TEST_PKG_JSON.name + '.war');
});
it('should have a fresh version number if the package version has changed after config(...)', function () {
const EXPECTED_VERSION = '1.2.3';
maven.config( extend({finalName: '{name}-{version}'}, TEST_CONFIG) );
npmVersion(EXPECTED_VERSION);
maven.package();
assertWarFileToEqual(TEST_PKG_JSON.name + '-' + EXPECTED_VERSION + '.war');
});
});
describe('install', function () {
it('should exec "mvn"', function () {
maven.config(TEST_CONFIG);
maven.install();
assert.ok(execSpy.calledOnce);
assert.ok(execSpy.calledWithMatch(/^mvn /));
});
it('should pass expected arguments to "mvn"', function () {
const EXPECTED_ARGS = [
'-B',
'install:install-file',
'"-Dpackaging=war"',
'"-Dfile=dist' + path.sep + TEST_PKG_JSON.name + '.war"',
'"-DgroupId=' + GROUP_ID + '"',
'"-DartifactId=' + TEST_PKG_JSON.name + '"',
'"-Dclassifier=' + TEST_CLASSIFIER + '"'
];
maven.config(TEST_CONFIG);
maven.install();
assertArgs(execSpy.args[0][0], EXPECTED_ARGS);
});
it('should pass pomFile to "mvn" when given', function () {
const EXPECTED_ARGS = [
'-B',
'install:install-file',
'"-Dpackaging=war"',
'"-Dfile=dist' + path.sep + TEST_PKG_JSON.name + '.war"',
'"-DgroupId=' + GROUP_ID + '"',
'"-DartifactId=' + TEST_PKG_JSON.name + '"',
'"-Dclassifier=' + TEST_CLASSIFIER + '"',
'"-DpomFile=' + TEST_CONFIG_WITH_POM.pomFile + '"'
];
maven.config(TEST_CONFIG_WITH_POM);
maven.install();
assertArgs(execSpy.args[0][0], EXPECTED_ARGS);
});
it('should filter undefined arguments', function () {
const UNEXPECTED_ARGS = [
'"-Dclassifier=undefined"'
];
maven.config({
groupId: GROUP_ID,
repositories: [DUMMY_REPO_SNAPSHOT]
});
maven.install();
assertNotArgs(execSpy.args[0][0], UNEXPECTED_ARGS);
});
it('should increase patch-version and add -SNAPSHOT to the version to follow Maven conventions', function () {
const EXPECTED_VERSION_ARG = '"-Dversion=' + semver.inc(TEST_PKG_JSON.version, 'patch') + '-SNAPSHOT"';
maven.config(TEST_CONFIG);
maven.install();
var cmd = childProcessMock.exec.args[0][0].split(/\s+/);
assert.ok(arrayContains(cmd, EXPECTED_VERSION_ARG), 'cmd should contain ' + EXPECTED_VERSION_ARG +
', but does not.\ncmd: ' + cmd);
});
it('should include -SNAPSHOT in the filename if finalName includes {version}', function () {
var EXPECTED_FILENAME = TEST_PKG_JSON.name + '-' + semver.inc(TEST_PKG_JSON.version, 'patch') +
'-SNAPSHOT.war';
var config = extend({}, TEST_CONFIG);
config.finalName = '{name}-{version}';
maven.config(config);
maven.install();
assertWarFileToEqual(EXPECTED_FILENAME);
});
it('should install file from arguments if specified', function () {
const CUSTOM_FILE = 'file-from-args.jar';
const EXPECTED_ARGS = ['"-Dfile='+CUSTOM_FILE + '"'];
var zip = new JSZip();
zip.file('test.txt', 'test');
fs.writeFileSync(CUSTOM_FILE, zip.generate({type:'nodebuffer', compression:'DEFLATE'}));
maven.config(TEST_CONFIG);
maven.install(CUSTOM_FILE);
assertArgs(execSpy.args[0][0], EXPECTED_ARGS);
});
it('should throw error if file from arguments does not exist', function () {
const CUSTOM_FILE = 'non-existing-file-from-args.jar';
maven.config(TEST_CONFIG);
assert.throws(function () {
maven.install(CUSTOM_FILE);
}, /ENOENT, no such file or directory/);
});
it('should call callback function when done successfully', function () {
var spy = sinon.spy();
maven.config(TEST_CONFIG);
maven.install(spy);
// fake successful exec
var execCallback = execSpy.args[0][1];
execCallback(null, 'stdout', null);
assert.ok(spy.calledOnce);
assert.equal(spy.args[0][1], 'stdout');
});
});
describe('deploy', function () {
it('should throw an error if repositories is empty', function () {
assert.throws(function () {
maven.config({
groupId: GROUP_ID
});
maven.deploy('dummy-repo', true);
});
assert.throws(function () {
maven.config({
groupId: GROUP_ID,
repositories: []
});
maven.deploy('dummy-repo', true);
});
});
it('should add correct repositoryId and url', function () {
const EXPECTED_ARGS = [
'"-DrepositoryId='+DUMMY_REPO_RELEASE.id + '"',
'"-Durl='+DUMMY_REPO_RELEASE.url + '"'
];
maven.config(TEST_CONFIG);
maven.deploy(DUMMY_REPO_RELEASE.id, false);
assertArgs(execSpy.args[0][0], EXPECTED_ARGS);
});
it('should add file argument', function () {
const EXPECTED_ARGS = ['"-Dfile=dist' + path.sep + 'test-pkg.war"'];
maven.config(TEST_CONFIG);
maven.deploy(DUMMY_REPO_RELEASE.id, false);
assertArgs(execSpy.args[0][0], EXPECTED_ARGS);
});
it('should add version argument', function () {
const EXPECTED_ARGS = ['"-Dversion=1.0.0"'];
maven.config(TEST_CONFIG);
maven.deploy(DUMMY_REPO_RELEASE.id, false);
assertArgs(execSpy.args[0][0], EXPECTED_ARGS);
});
it('should deploy file from arguments if specified', function () {
const CUSTOM_FILE = 'file-from-args.jar';
const EXPECTED_ARGS = [
'"-Dfile='+CUSTOM_FILE + '"',
'"-Dversion='+TEST_PKG_JSON.version + '"'
];
var zip = new JSZip();
zip.file('test.txt', 'test');
fs.writeFileSync(CUSTOM_FILE, zip.generate({type:'nodebuffer', compression:'DEFLATE'}));
maven.config(TEST_CONFIG);
maven.deploy(DUMMY_REPO_RELEASE, CUSTOM_FILE);
maven.deploy(DUMMY_REPO_RELEASE, CUSTOM_FILE, false);
maven.deploy(DUMMY_REPO_RELEASE, CUSTOM_FILE, false, sinon.spy());
maven.deploy(DUMMY_REPO_RELEASE, CUSTOM_FILE, sinon.spy());
assert.equal(execSpy.callCount, 4);
for (var i=0; i<4; i++) {
assertArgs(execSpy.args[i][0], EXPECTED_ARGS);
}
});
it('should call callback function when done successfully', function () {
var spy = sinon.spy();
maven.config(TEST_CONFIG);
maven.deploy(DUMMY_REPO_RELEASE.id, spy);
maven.deploy(DUMMY_REPO_RELEASE.id, false, spy);
maven.deploy(DUMMY_REPO_RELEASE.id, true, spy);
maven.deploy(DUMMY_REPO_RELEASE.id, 'package.json', spy);
maven.deploy(DUMMY_REPO_RELEASE.id, 'package.json', false, spy);
assert.equal(execSpy.callCount, 5);
for (var i=0; i<5; i++) {
// fake successful exec
execSpy.args[i][1](null, 'stdout', null);
}
assert.equal(spy.callCount, 5);
assert.equal(spy.args[0][1], 'stdout');
});
it('should not generate pom', function () {
const EXPECTED_ARGS = ['"-DgeneratePom=false"'];
maven.config(TEST_CONFIG);
maven.deploy(DUMMY_REPO_RELEASE.id, false);
assertArgs(execSpy.args[0][0], EXPECTED_ARGS);
});
it('should support spaces in file paths', function () {
const EXPECTED_ARGS = ['"-Dfile=dist with spaces' + path.sep + 'test-pkg.war"'];
maven.config(TEST_CONFIG_WITH_SPACES);
maven.deploy(DUMMY_REPO_RELEASE.id, false);
assertArgs(execSpy.args[0][0], EXPECTED_ARGS);
});
});
describe('archive', function () {
it('should zip file with unix-style path', function () {
maven.config(TEST_CONFIG);
maven.package();
var zip = warFileInDistAsZip();
assert.ok(zip.file('js/index.js'));
});
it('should zip file with non-virtual folders', function () {
maven.config(TEST_CONFIG);
maven.package();
var zip = warFileInDistAsZip();
assert.equal(zip.folder(/^js/).length, 1);
});
it('should save binary files correctly', function () {
maven.config(TEST_CONFIG);
maven.package();
var zip = warFileInDistAsZip();
var image = zip.file('px.png');
assert.ok(image, 'archive should contain px.png image');
var imageEqualOriginal = bufferEqual(new Buffer( new Uint8Array(image.asArrayBuffer()) ), BINARY_FILE);
assert.ok(imageEqualOriginal, 'image data equals the original');
});
});
});