/
platform.spec.js
471 lines (403 loc) · 19 KB
/
platform.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
/**
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.
*/
var helpers = require('./helpers'),
path = require('path'),
fs = require('fs'),
shell = require('shelljs'),
superspawn = require('cordova-common').superspawn,
config = require('../src/cordova/config'),
Q = require('q'),
events = require('cordova-common').events,
cordova = require('../src/cordova/cordova'),
plugman = require('../src/plugman/plugman'),
rewire = require('rewire'),
platform = rewire('../src/cordova/platform.js');
var projectRoot = 'C:\\Projects\\cordova-projects\\move-tracker';
var pluginsDir = path.join(__dirname, 'fixtures', 'plugins');
describe('platform end-to-end', function () {
var tmpDir = helpers.tmpDir('platform_test');
var project = path.join(tmpDir, 'project');
var results;
beforeEach(function() {
shell.rm('-rf', tmpDir);
// cp then mv because we need to copy everything, but that means it'll copy the whole directory.
// Using /* doesn't work because of hidden files.
shell.cp('-R', path.join(__dirname, 'fixtures', 'base'), tmpDir);
shell.mv(path.join(tmpDir, 'base'), project);
process.chdir(project);
// Now we load the config.json in the newly created project and edit the target platform's lib entry
// to point at the fixture version. This is necessary so that cordova.prepare can find cordova.js there.
var c = config.read(project);
c.lib[helpers.testPlatform].url = path.join(__dirname, 'fixtures', 'platforms', helpers.testPlatform + '-lib');
config.write(project, c);
// The config.json in the fixture project points at fake "local" paths.
// Since it's not a URL, the lazy-loader will just return the junk path.
spyOn(superspawn, 'spawn').andCallFake(function(cmd, args) {
if (cmd.match(/create\b/)) {
// This is a call to the bin/create script, so do the copy ourselves.
shell.cp('-R', path.join(__dirname, 'fixtures', 'platforms', 'android'), path.join(project, 'platforms'));
} else if(cmd.match(/version\b/)) {
return Q('3.3.0');
} else if(cmd.match(/update\b/)) {
fs.writeFileSync(path.join(project, 'platforms', helpers.testPlatform, 'updated'), 'I was updated!', 'utf-8');
}
return Q();
});
events.on('results', function(res) { results = res; });
});
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
});
// Factoring out some repeated checks.
function emptyPlatformList() {
return cordova.raw.platform('list').then(function() {
var installed = results.match(/Installed platforms:\n (.*)/);
expect(installed).toBeDefined();
expect(installed[1].indexOf(helpers.testPlatform)).toBe(-1);
});
}
function fullPlatformList() {
return cordova.raw.platform('list').then(function() {
var installed = results.match(/Installed platforms:\n (.*)/);
expect(installed).toBeDefined();
expect(installed[1].indexOf(helpers.testPlatform)).toBeGreaterThan(-1);
});
}
// The flows we want to test are add, rm, list, and upgrade.
// They should run the appropriate hooks.
// They should fail when not inside a Cordova project.
// These tests deliberately have no beforeEach and afterEach that are cleaning things up.
it('should successfully run', function(done) {
// Check there are no platforms yet.
emptyPlatformList().then(function() {
// Add the testing platform.
return cordova.raw.platform('add', [helpers.testPlatform]);
}).then(function() {
// Check the platform add was successful.
expect(path.join(project, 'platforms', helpers.testPlatform)).toExist();
expect(path.join(project, 'platforms', helpers.testPlatform, 'cordova')).toExist();
}).then(fullPlatformList) // Check for it in platform ls.
.then(function() {
// Try to update the platform.
return cordova.raw.platform('update', [helpers.testPlatform]);
}).then(function() {
// Our fake update script in the exec mock above creates this dummy file.
expect(path.join(project, 'platforms', helpers.testPlatform, 'updated')).toExist();
}).then(fullPlatformList) // Platform should still be in platform ls.
.then(function() {
// And now remove it.
return cordova.raw.platform('rm', [helpers.testPlatform]);
}).then(function() {
// It should be gone.
expect(path.join(project, 'platforms', helpers.testPlatform)).not.toExist();
}).then(emptyPlatformList) // platform ls should be empty too.
.fail(function(err) {
expect(err).toBeUndefined();
}).fin(done);
});
it('should install plugins correctly while adding platform', function(done) {
cordova.raw.plugin('add', path.join(pluginsDir, 'test'))
.then(function() {
return cordova.raw.platform('add', [helpers.testPlatform]);
})
.then(function() {
// Check the platform add was successful.
expect(path.join(project, 'platforms', helpers.testPlatform)).toExist();
// Check that plugin files exists in www dir
expect(path.join(project, 'platforms', helpers.testPlatform, 'assets/www/test.js')).toExist();
})
.fail(function(err) {
expect(err).toBeUndefined();
})
.fin(done);
});
it('should call prepare after plugins were installed into platform', function(done) {
var order = '';
var fail = jasmine.createSpy(fail);
spyOn(plugman.raw, 'install').andCallFake(function() { order += 'I'; });
spyOn(cordova.raw, 'prepare').andCallFake(function() { order += 'P'; });
cordova.raw.plugin('add', path.join(pluginsDir, 'test'))
.then(function() {
return cordova.raw.platform('add', [helpers.testPlatform]);
})
.fail(fail)
.fin(function() {
expect(order).toBe('IP'); // Install first, then prepare
expect(fail).not.toHaveBeenCalled();
done();
});
});
});
describe('add function', function () {
var opts;
var hooksRunnerMock;
beforeEach(function(){
opts = {};
hooksRunnerMock = {
fire: function () {
return Q();
}
};
});
it('throws if the target list is empty', function (done) {
var targets = [];
platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
expect(error.message).toBe('No platform specified. Please specify a platform to add. See `cordova platform list`.');
done();
});
});
it('throws if the target list is undefined or null', function (done) {
// case 1 : target list undefined
var targets; // = undefined;
platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
expect(error.message).toBe('No platform specified. Please specify a platform to add. See `cordova platform list`.');
});
// case 2 : target list null
targets = null;
platform.add(hooksRunnerMock, projectRoot, targets, opts).fail(function (error) {
expect(error.message).toBe('No platform specified. Please specify a platform to add. See `cordova platform list`.');
done();
});
});
});
describe('platform add plugin rm end-to-end', function () {
var tmpDir = helpers.tmpDir('plugin_rm_test');
var project = path.join(tmpDir, 'hello');
var pluginsDir = path.join(project, 'plugins');
beforeEach(function() {
process.chdir(tmpDir);
});
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
});
it('should remove dependency when removing parent plugin', function(done) {
cordova.raw.create('hello')
.then(function() {
process.chdir(project);
return cordova.raw.platform('add', 'ios@latest');
})
.then(function() {
return cordova.raw.plugin('add', 'cordova-plugin-media');
})
.then(function() {
expect(path.join(pluginsDir, 'cordova-plugin-media')).toExist();
expect(path.join(pluginsDir, 'cordova-plugin-file')).toExist();
return cordova.raw.platform('add', 'android@latest');
})
.then(function() {
expect(path.join(pluginsDir, 'cordova-plugin-media')).toExist();
expect(path.join(pluginsDir, 'cordova-plugin-file')).toExist();
return cordova.raw.plugin('rm', 'cordova-plugin-media');
})
.then(function() {
expect(path.join(pluginsDir, 'cordova-plugin-media')).not.toExist();
expect(path.join(pluginsDir, 'cordova-plugin-file')).not.toExist();
})
.fail(function(err) {
console.error(err);
expect(err).toBeUndefined();
})
.fin(done);
}, 20000);
});
describe('platform add and remove --fetch', function () {
var tmpDir = helpers.tmpDir('plat_add_remove_fetch_test');
var project = path.join(tmpDir, 'helloFetch');
var platformsDir = path.join(project, 'platforms');
var nodeModulesDir = path.join(project, 'node_modules');
beforeEach(function() {
process.chdir(tmpDir);
});
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
});
it('should add and remove platform from node_modules directory', function(done) {
cordova.raw.create('helloFetch')
.then(function() {
process.chdir(project);
return cordova.raw.platform('add', 'ios', {'fetch':true});
})
.then(function() {
expect(path.join(nodeModulesDir, 'cordova-ios')).toExist();
expect(path.join(platformsDir, 'ios')).toExist();
return cordova.raw.platform('add', 'android', {'fetch':true});
})
.then(function() {
expect(path.join(nodeModulesDir, 'cordova-android')).toExist();
expect(path.join(platformsDir, 'android')).toExist();
//Tests finish before this command finishes resolving
//return cordova.raw.platform('rm', 'ios', {'fetch':true});
})
.then(function() {
//expect(path.join(nodeModulesDir, 'cordova-ios')).not.toExist();
//expect(path.join(platformsDir, 'ios')).not.toExist();
//Tests finish before this command finishes resolving
//return cordova.raw.platform('rm', 'android', {'fetch':true});
})
.then(function() {
//expect(path.join(nodeModulesDir, 'cordova-android')).not.toExist();
//expect(path.join(platformsDir, 'android')).not.toExist();
})
.fail(function(err) {
console.error(err);
expect(err).toBeUndefined();
})
.fin(done);
}, 40000);
});
describe('plugin add and rm end-to-end --fetch', function () {
var tmpDir = helpers.tmpDir('plugin_rm_fetch_test');
var project = path.join(tmpDir, 'hello3');
var pluginsDir = path.join(project, 'plugins');
beforeEach(function() {
process.chdir(tmpDir);
});
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
});
it('should remove dependency when removing parent plugin', function(done) {
cordova.raw.create('hello3')
.then(function() {
process.chdir(project);
return cordova.raw.platform('add', 'ios', {'fetch': true});
})
.then(function() {
return cordova.raw.plugin('add', 'cordova-plugin-media', {'fetch': true});
})
.then(function() {
expect(path.join(pluginsDir, 'cordova-plugin-media')).toExist();
expect(path.join(pluginsDir, 'cordova-plugin-file')).toExist();
expect(path.join(pluginsDir, 'cordova-plugin-compat')).toExist();
expect(path.join(project, 'node_modules', 'cordova-plugin-media')).toExist();
expect(path.join(project, 'node_modules', 'cordova-plugin-file')).toExist();
expect(path.join(project, 'node_modules', 'cordova-plugin-compat')).toExist();
return cordova.raw.platform('add', 'android', {'fetch':true});
})
.then(function() {
expect(path.join(pluginsDir, 'cordova-plugin-media')).toExist();
expect(path.join(pluginsDir, 'cordova-plugin-file')).toExist();
return cordova.raw.plugin('rm', 'cordova-plugin-media', {'fetch':true});
})
.then(function() {
expect(path.join(pluginsDir, 'cordova-plugin-media')).not.toExist();
expect(path.join(pluginsDir, 'cordova-plugin-file')).not.toExist();
expect(path.join(pluginsDir, 'cordova-plugin-compat')).not.toExist();
//These don't work yet due to the tests finishing before the promise resolves.
//expect(path.join(project, 'node_modules', 'cordova-plugin-media')).not.toExist();
//expect(path.join(project, 'node_modules', 'cordova-plugin-file')).not.toExist();
//expect(path.join(project, 'node_modules', 'cordova-plugin-compat')).not.toExist();
})
.fail(function(err) {
console.error(err);
expect(err).toBeUndefined();
})
.fin(done);
}, 60000);
});
describe('cocoapod plugin add and rm end-to-end', function () {
var tmpDir = helpers.tmpDir('cocoapod_plugin_test');
var project = path.join(tmpDir, 'hello4');
var samplePlugin = path.join(__dirname, 'fixtures', 'plugins', 'sample-cordova-plugin-cocoapod-dependent');
var overlappingDependencyPlugin = path.join(__dirname, 'fixtures', 'plugins', 'sample-cocoapod-plugin-overlapping-dependency');
var AFNetworking = 'AFNetworking',
CWStatusBarNotification = 'CWStatusBarNotification';
var podfile, podsJSON, workspace;
beforeEach(function() {
process.chdir(tmpDir);
});
afterEach(function() {
process.chdir(path.join(__dirname, '..')); // Needed to rm the dir on Windows.
shell.rm('-rf', tmpDir);
});
it('installs and uninstalls plugin depending on new pod and existing pod', function(done) {
cordova.raw.create('hello4')
.then(function() {
process.chdir(project);
return cordova.raw.platform('add', 'https://git-wip-us.apache.org/repos/asf/cordova-ios.git');
})
.then(function() {
return cordova.raw.plugin('add', samplePlugin);
})
.then(function() {
podfile = path.resolve('./platforms/ios/Podfile');
podsJSON = path.resolve('./platforms/ios/pods.json');
workspace = path.resolve('./platforms/ios/HelloCordova.xcworkspace');
//podfile should have been created
fs.exists(podfile, function(podfileExists){
expect(podfileExists);
});
//pods.json should have been created
fs.exists(podsJSON, function(podsJSONExists){
expect(podsJSONExists);
});
//workspace should have been created
fs.exists(workspace, function(workspaceCreated){
expect(workspaceCreated);
});
var podfileContent = fs.readFileSync(podfile, {'encoding' : 'utf8'});
expect(podfileContent.indexOf(AFNetworking) !== -1 );
var podsJSONContent = require(podsJSON);
expect(podsJSONContent[AFNetworking] !== null);
return cordova.raw.plugin('add', overlappingDependencyPlugin);
})
.then(function() {
var podfileContent = fs.readFileSync(podfile, {'encoding' : 'utf8'});
var numberOfTimesAFNetworkingIsInPodfile = podfileContent.match(/AFNetworking/g || []).length;
expect(podfileContent.indexOf(CWStatusBarNotification) !== -1);
expect(numberOfTimesAFNetworkingIsInPodfile).toEqual(1);
delete require.cache[require.resolve(podsJSON)];
var podsJSONContent = require(podsJSON);
var countPropertyOfAFNetworkingInPodsJSON = podsJSONContent[AFNetworking].count;
var specPropertyOfAFNetworkingInPodsJSON = podsJSONContent[AFNetworking].spec;
expect(countPropertyOfAFNetworkingInPodsJSON).toEqual(2);
//spec property should not be changed because of overlapping dependency
expect(specPropertyOfAFNetworkingInPodsJSON).toEqual('~> 3.0');
return cordova.raw.plugin('rm','sample-cocoapod-plugin-overlapping-dependency');
})
.then(function() {
//expect only AFNetworking
var podfileContent = fs.readFileSync(podfile, {'encoding' : 'utf8'});
expect(podfileContent.indexOf(CWStatusBarNotification) === -1);
expect(podfileContent.indexOf(AFNetworking) !== -1);
delete require.cache[require.resolve(podsJSON)];
var podsJSONContent = require(podsJSON);
expect(podsJSONContent[AFNetworking]);
expect(podsJSONContent[CWStatusBarNotification] === undefined);
return cordova.raw.plugin('rm', 'sample-cordova-plugin-cocoapod-dependent');
})
.then(function() {
//expect no pods
delete require.cache[require.resolve(podfile)];
var podfileContent = fs.readFileSync(podfile, {'encoding' : 'utf8'});
expect(podfileContent.indexOf(CWStatusBarNotification) === -1);
expect(podfileContent.indexOf(AFNetworking) === -1);
delete require.cache[require.resolve(podsJSON)];
var podsJSONContent = require(podsJSON);
expect(podsJSONContent[AFNetworking] === undefined);
expect(podsJSONContent[CWStatusBarNotification] === undefined);
})
.fail(function(err) {
console.error(err);
expect(err).toBeUndefined();
})
.fin(done);
}, 60000);
});