/
pkgJson.spec.js
483 lines (420 loc) · 22.3 KB
/
pkgJson.spec.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
478
479
480
481
482
483
/**
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
const path = require('path');
const fs = require('fs-extra');
const semver = require('semver');
const { listPlatforms, requireNoCache } = require('../src/cordova/util');
const { tmpDir: getTmpDir, testPlatform, setDefaultTimeout } = require('../spec/helpers');
const projectTestHelpers = require('../spec/project-test-helpers');
const cordova = require('../src/cordova/cordova');
describe('pkgJson', function () {
const TIMEOUT = 150 * 1000;
setDefaultTimeout(TIMEOUT);
const fixturesPath = path.join(__dirname, '../spec/cordova/fixtures');
let tmpDir, project, pkgJsonPath;
const {
getPkgJsonPath, setupBaseProject, getCfg, getPkgJson, setPkgJson
} = projectTestHelpers(() => project);
beforeEach(() => {
tmpDir = getTmpDir('pkgJson');
project = path.join(tmpDir, 'project');
pkgJsonPath = getPkgJsonPath();
delete process.env.PWD;
});
afterEach(() => {
process.chdir(__dirname); // Needed to rm the dir on Windows.
fs.removeSync(tmpDir);
});
// Copies a fixture to temp dir to avoid modifiying it as they get installed as symlinks
function copyFixture (fixtureRelativePath) {
const fixturePath = path.join(fixturesPath, fixtureRelativePath);
const tmpPath = path.join(tmpDir, path.basename(fixtureRelativePath));
fs.copySync(fixturePath, tmpPath);
return tmpPath;
}
function installedPlatforms () {
// Sort platform list to allow for easy pseudo set equality
return listPlatforms(project).sort();
}
function platformVersion (platformName) {
const p = path.join(project, 'platforms', platformName, 'cordova/version');
expect(p).toExist();
return requireNoCache(p).version;
}
function pluginVersion (pluginName) {
const p = path.join(project, 'plugins', pluginName, 'package.json');
expect(p).toExist();
return fs.readJsonSync(p).version;
}
function specSatisfiedBy (version) {
return {
asymmetricMatch: spec => semver.satisfies(version, spec),
jasmineToString: _ => `<specSatisfiedBy(${version})>`
};
}
function specWithMinSatisfyingVersion (version) {
return {
asymmetricMatch: spec =>
!semver.intersects(spec, `<${version}`) &&
semver.intersects(spec, `>=${version}`),
jasmineToString: _ => `<specWithMinSatisfyingVersion(${version})>`
};
}
const customMatchers = {
toSatisfy: () => ({ compare (version, spec) {
const pass = semver.satisfies(version, spec);
const expectation = (pass ? 'not ' : '') + 'to satisfy';
return {
pass, message: `expected ${version} ${expectation} ${spec}`
};
} }),
tohaveMinSatisfyingVersion: () => ({ compare (spec, version) {
const pass = specWithMinSatisfyingVersion(version).asymmetricMatch(spec);
const expectation = (pass ? 'not ' : '') + 'to have minimal satisfying version';
return {
pass, message: `expected ${spec} ${expectation} ${version}`
};
} })
};
// Add our custom matchers
beforeEach(() => jasmine.addMatchers(customMatchers));
// This group of tests checks if plugins are added and removed as expected from package.json.
describe('plugin end-to-end', function () {
const pluginId = 'cordova-plugin-device';
beforeEach(function () {
setupBaseProject();
// Copy some platform to avoid working on a project with no platforms.
// FIXME Use a fixture that is properly promisified. This one
// causes spurious test failures when tests reuse the project path.
fs.copySync(path.join(__dirname, '../spec/plugman/projects', testPlatform), path.join(project, 'platforms', testPlatform));
});
it('Test#001 : should successfully add and remove a plugin with save and correct spec', function () {
// No plugins in config or pkg.json yet.
expect(getCfg().getPluginIdList()).toEqual([]);
expect(getPkgJson('cordova')).toBeUndefined();
// Add the plugin with --save.
return cordova.plugin('add', `${pluginId}@1.1.2`, { save: true })
.then(function () {
// Check that the plugin and spec add was successful to pkg.json.
expect(getPkgJson('cordova.plugins')[pluginId]).toBeDefined();
expect(getPkgJson('dependencies')[pluginId]).tohaveMinSatisfyingVersion('1.1.2');
expect(getCfg().getPluginIdList()).toEqual([]);
}).then(function () {
// And now remove it with --save.
return cordova.plugin('rm', pluginId, { save: true });
}).then(function () {
// Expect plugin to be removed from pkg.json.
expect(getPkgJson('cordova.plugins')[pluginId]).toBeUndefined();
expect(getPkgJson('dependencies')[pluginId]).toBeUndefined();
});
});
it('Test#002 : should NOT add a plugin to package.json if --save is not used', function () {
const SAVED_PLUGIN = 'cordova-plugin-geolocation';
expect(pkgJsonPath).toExist();
// Add the geolocation plugin with --save.
return cordova.plugin('add', SAVED_PLUGIN, { save: true })
.then(function () {
// Add a second plugin without save.
return cordova.plugin('add', pluginId);
}).then(function () {
// Expect that only the plugin that had --save was added.
expect(getPkgJson('cordova.plugins')).toEqual({
[SAVED_PLUGIN]: {}
});
});
});
it('Test#003 : should NOT remove plugin from package.json if there is no --save', function () {
expect(pkgJsonPath).toExist();
// Add the plugin with --save.
return cordova.plugin('add', pluginId, { save: true })
.then(function () {
expect(getPkgJson('cordova.plugins')).toEqual({
[pluginId]: {}
});
}).then(function () {
// And now remove it, but without --save.
return cordova.plugin('rm', pluginId);
}).then(function () {
// The plugin should still be in package.json.
expect(getPkgJson('cordova.plugins')).toEqual({
[pluginId]: {}
});
});
});
it('Test#004 : should successfully add and remove a plugin with variables and save to package.json', function () {
expect(pkgJsonPath).toExist();
// Add the plugin with --save.
return cordova.plugin('add', pluginId, { save: true, cli_variables: { someKey: 'someValue' } })
.then(function () {
// Check the plugin add was successful and that variables have been added too.
expect(getPkgJson('cordova.plugins')).toEqual({
[pluginId]: { someKey: 'someValue' }
});
}).then(function () {
// And now remove it with --save.
return cordova.plugin('rm', pluginId, { save: true });
}).then(function () {
// Checking that the plugin and variables were removed successfully.
expect(getPkgJson('cordova.plugins')).toEqual({});
});
});
it('Test#005 : should successfully add and remove multiple plugins with save & fetch', function () {
const OTHER_PLUGIN = 'cordova-plugin-device-motion';
expect(pkgJsonPath).toExist();
// Add the plugin with --save.
return cordova.plugin('add', [pluginId, OTHER_PLUGIN], { save: true })
.then(function () {
// Check that the plugin add was successful.
expect(getPkgJson('cordova.plugins')).toEqual({
[pluginId]: {}, [OTHER_PLUGIN]: {}
});
expect(getPkgJson('dependencies')).toEqual({
[pluginId]: jasmine.any(String),
[OTHER_PLUGIN]: jasmine.any(String)
});
}).then(function () {
// And now remove it with --save.
return cordova.plugin('rm', [pluginId, OTHER_PLUGIN], { save: true });
}).then(function () {
// Checking that the plugin removed is in not in the platforms.
expect(getPkgJson('cordova.plugins')).toEqual({});
expect(getPkgJson('dependencies')).toEqual({});
});
});
// Test #023 : if pkg.json and config.xml have no platforms/plugins/spec.
// and --save --fetch is called, use the pinned version or plugin pkg.json version.
it('Test#023 : use pinned/lastest version if there is no platform/plugin version passed in and no platform/plugin versions in pkg.json or config.xml', function () {
const PLATFORM = 'ios';
const PLUGIN = 'cordova-plugin-geolocation';
// Pkg.json has no platform or plugin or specs.
expect(getPkgJson('cordova')).toBeUndefined();
expect(getPkgJson('dependencies')).toBeUndefined();
// Config.xml has no platform or plugin or specs.
expect(getCfg().getEngines()).toEqual([]);
expect(getCfg().getPluginIdList()).toEqual([]);
return cordova.platform('add', PLATFORM, { save: true })
.then(function () {
expect(getPkgJson('cordova.platforms')).toEqual([PLATFORM]);
}).then(function () {
return cordova.plugin('add', PLUGIN, { save: true });
}).then(function () {
const iosJson = fs.readJsonSync(path.join(project, 'platforms/ios/ios.json'));
expect(iosJson.installed_plugins[PLUGIN]).toBeDefined();
// Check that installed version satisfies the dependency spec
const version = pluginVersion(PLUGIN);
expect(version).toSatisfy(getPkgJson(`dependencies.${PLUGIN}`));
});
});
// Test#025: has a pkg.json. Checks if local path is added to pkg.json for platform and plugin add.
it('Test#025 : if you add a platform/plugin with local path, pkg.json gets updated', function () {
const PLATFORM = 'browser';
const PLUGIN = 'cordova-lib-test-plugin';
const platformPath = copyFixture(`platforms/cordova-${PLATFORM}`);
const pluginPath = copyFixture(path.join('plugins', PLUGIN));
return cordova.platform('add', platformPath, { save: true })
.then(function () {
// Pkg.json has platform
expect(getPkgJson('cordova.platforms')).toEqual([PLATFORM]);
expect(getPkgJson(`dependencies.cordova-${PLATFORM}`)).toBeDefined();
}).then(function () {
// Run cordova plugin add local path --save --fetch.
return cordova.plugin('add', pluginPath, { save: true });
}).then(function () {
// Pkg.json has test plugin.
expect(getPkgJson(`cordova.plugins.${PLUGIN}`)).toBeDefined();
expect(getPkgJson(`dependencies.${PLUGIN}`)).toBeDefined();
});
});
});
// This group of tests checks if platforms are added and removed as expected from package.json.
describe('platform end-to-end with --save', function () {
beforeEach(() => setupBaseProject());
it('Test#006 : platform is added and removed correctly with --save', function () {
expect(pkgJsonPath).toExist();
expect(installedPlatforms()).toEqual([]);
// Add the testing platform with --save.
return cordova.platform('add', testPlatform, { save: true }).then(function () {
// Check the platform add was successful.
expect(installedPlatforms()).toEqual([testPlatform]);
expect(getPkgJson('cordova.platforms')).toEqual([testPlatform]);
}).then(function () {
// And now remove it with --save.
return cordova.platform('rm', testPlatform, { save: true });
}).then(function () {
// Checking that the platform removed is in not in the platforms key.
expect(getPkgJson('cordova.platforms')).toEqual([]);
});
});
it('Test#007 : should not remove platforms from package.json when removing without --save', function () {
expect(pkgJsonPath).toExist();
expect(installedPlatforms()).toEqual([]);
// Add the testing platform with --save.
return cordova.platform('add', testPlatform, { save: true }).then(function () {
// Check the platform add was successful.
expect(installedPlatforms()).toEqual([testPlatform]);
expect(getPkgJson('cordova.platforms')).toEqual([testPlatform]);
}).then(function () {
// And now remove it without --save.
return cordova.platform('rm', testPlatform);
}).then(function () {
// Check that the removed platform is still in cordova.platforms...
expect(getPkgJson('cordova.platforms')).toEqual([testPlatform]);
// ... but is not actually installed any longer
expect(installedPlatforms()).toEqual([]);
});
});
it('Test#008 : should not add platform to package.json when adding without --save', function () {
expect(getPkgJson('cordova')).toBeUndefined();
// Add platform without --save.
return cordova.platform('add', testPlatform)
.then(function () {
// Test platform should have been installed but not added to pkg.json
expect(installedPlatforms()).toEqual([testPlatform]);
expect(getPkgJson('cordova')).toBeUndefined();
});
});
it('Test#009 : should only add the platform to package.json with --save', function () {
const platformNotToAdd = 'browser';
expect(pkgJsonPath).toExist();
// Add a platform without --save.
return cordova.platform('add', platformNotToAdd)
.then(function () {
// And now add another platform with --save.
return cordova.platform('add', testPlatform, { save: true });
}).then(function () {
// Check that only the platform added with --save was added to package.json.
expect(getPkgJson('cordova.platforms')).toEqual([testPlatform]);
});
});
it('Test#010 : two platforms are added and removed correctly with --save --fetch', function () {
// No platforms installed nor saved in config or pkg.json yet.
expect(getPkgJson('cordova')).toBeUndefined();
expect(getCfg().getEngines()).toEqual([]);
expect(installedPlatforms()).toEqual([]);
// Add the testing platform with --save and add specific version to android platform.
return cordova.platform('add', ['android@7.0.0', 'browser@5.0.1'], { save: true }).then(function () {
expect(installedPlatforms()).toEqual(['android', 'browser']);
// Check the platform add was successful in platforms list and
// dependencies should have specific version from add.
expect(getPkgJson('cordova.platforms')).toEqual(['android', 'browser']);
expect(getPkgJson('dependencies')).toEqual({
'cordova-android': specWithMinSatisfyingVersion('7.0.0'),
'cordova-browser': specWithMinSatisfyingVersion('5.0.1')
});
expect(getCfg().getEngines()).toEqual([]);
}).then(function () {
// And now remove it with --save.
return cordova.platform('rm', ['android', 'browser'], { save: true });
}).then(function () {
// Expect platforms to be uninstalled & removed from config files
expect(getPkgJson('cordova.platforms')).toEqual([]);
expect(getPkgJson('dependencies')).toEqual({});
expect(getCfg().getEngines()).toEqual([]);
expect(installedPlatforms()).toEqual([]);
});
});
});
// Test #020 : pkg.json contains platform/spec and plugin/spec and config.xml does not
describe('with differing config files', function () {
beforeEach(() => setupBaseProject());
/** Test#020 will check that pkg.json, config.xml, platforms.json, and cordova platform ls
* are updated with the correct (platform and plugin) specs from pkg.json.
*/
it('Test#020 : During add, if pkg.json has a spec, use that one.', function () {
const PLATFORM = 'ios';
const PLUGIN = 'cordova-plugin-splashscreen';
setPkgJson('cordova.platforms', [ PLATFORM ]);
setPkgJson('dependencies', {
[PLUGIN]: '^3.2.2',
[`cordova-${PLATFORM}`]: '^4.5.4'
});
// config.xml has no platforms or plugins yet.
expect(getCfg().getEngines()).toEqual([]);
expect(getCfg().getPluginIdList()).toEqual([]);
expect(installedPlatforms()).toEqual([]);
return cordova.platform('add', PLATFORM, { save: true }).then(function () {
// No change to pkg.json platforms or spec for ios.
expect(getPkgJson('cordova.platforms')).toEqual([ PLATFORM ]);
// Config.xml and ios/cordova/version check.
const version = platformVersion(PLATFORM);
// Check that pkg.json and ios/cordova/version versions "satisfy" each other.
const pkgSpec = getPkgJson(`dependencies.cordova-${PLATFORM}`);
expect(version).toSatisfy(pkgSpec);
}).then(function () {
return cordova.plugin('add', PLUGIN, { save: true });
}).then(function () {
// Check that installed version satisfies the dependency spec
expect(pluginVersion(PLUGIN)).toSatisfy(getPkgJson(`dependencies.${PLUGIN}`));
});
}, TIMEOUT * 2);
/** Test#021 during add, this test will check that pkg.json, config.xml, platforms.json,
* and cordova platform ls are updated with the correct platform/plugin spec from config.xml.
*/
it('Test#021 : If config.xml has a spec (and none was specified and pkg.json does not have one), use config.', function () {
const PLATFORM = 'ios';
const PLUGIN = 'cordova-plugin-splashscreen';
getCfg()
.addEngine(PLATFORM, '~4.2.1')
.addPlugin({ name: PLUGIN, spec: '~3.2.2' })
.write();
expect(installedPlatforms()).toEqual([]);
// Remove for testing purposes so platform is not pre-installed.
return cordova.platform('rm', PLATFORM, { save: true }).then(function () {
return cordova.platform('add', PLATFORM, { save: true });
}).then(function () {
// pkg.json has new platform.
expect(getPkgJson('cordova.platforms')).toEqual([PLATFORM]);
}).then(function () {
return cordova.plugin('add', PLUGIN, { save: true });
}).then(function () {
expect(getCfg().getPlugins()).toEqual([{
name: PLUGIN,
spec: specSatisfiedBy(pluginVersion(PLUGIN)),
variables: {}
}]);
});
});
/** Test#022 : when adding with a specific platform version, always use that one
* regardless of what is in package.json or config.xml.
*/
it('Test#022 : when adding with a specific platform version, always use that one.', function () {
const PLATFORM = 'ios';
const PLUGIN = 'cordova-plugin-splashscreen';
setPkgJson('cordova.platforms', [ PLATFORM ]);
setPkgJson('dependencies', {
[`cordova-${PLATFORM}`]: '^4.2.1',
[PLUGIN]: '^3.2.2'
});
getCfg()
.addEngine(PLATFORM, '~4.2.1')
.addPlugin({ name: PLUGIN, spec: '~3.2.1' })
.write();
expect(installedPlatforms()).toEqual([]);
return cordova.platform('add', `${PLATFORM}@4.5.4`, { save: true }).then(function () {
// Pkg.json has ios.
expect(getPkgJson('cordova.platforms')).toEqual([ PLATFORM ]);
}).then(function () {
return cordova.plugin('add', `${PLUGIN}@4.0.0`, { save: true });
}).then(function () {
// Check that installed version satisfies the dependency spec
const version = pluginVersion(PLUGIN);
expect(version).toSatisfy(getCfg().getPlugin(PLUGIN).spec);
expect(version).toSatisfy(getPkgJson(`dependencies.${PLUGIN}`));
});
});
});
});